Deleted StdDescriptors.c, renamed USB_GetDescriptor() to CALLBACK_USB_GetDescriptor...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.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
33 #if defined(USB_CAN_BE_DEVICE)
34
35 #define INCLUDE_FROM_ENDPOINT_C
36 #include "Endpoint.h"
37
38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39 uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40 #endif
41
42 uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size)
43 {
44 return Endpoint_BytesToEPSizeMask(Size);
45 }
46
47 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData)
48 {
49 Endpoint_SelectEndpoint(Number);
50 Endpoint_EnableEndpoint();
51
52 UECFG1X = 0;
53
54 UECFG0X = UECFG0XData;
55 UECFG1X = UECFG1XData;
56
57 return Endpoint_IsConfigured();
58 }
59
60 void Endpoint_ClearEndpoints(void)
61 {
62 UEINT = 0;
63
64 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
65 {
66 Endpoint_SelectEndpoint(EPNum);
67 UEIENX = 0;
68 UEINTX = 0;
69 Endpoint_DeallocateMemory();
70 Endpoint_DisableEndpoint();
71 }
72 }
73
74 #if !defined(CONTROL_ONLY_DEVICE)
75 uint8_t Endpoint_WaitUntilReady(void)
76 {
77 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
78 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
79 #else
80 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
81 #endif
82
83 USB_INT_Clear(USB_INT_SOFI);
84
85 for (;;)
86 {
87 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
88 {
89 if (Endpoint_IsINReady())
90 return ENDPOINT_READYWAIT_NoError;
91 }
92 else
93 {
94 if (Endpoint_IsOUTReceived())
95 return ENDPOINT_READYWAIT_NoError;
96 }
97
98 if (!(USB_IsConnected))
99 return ENDPOINT_READYWAIT_DeviceDisconnected;
100 else if (Endpoint_IsStalled())
101 return ENDPOINT_READYWAIT_EndpointStalled;
102
103 if (USB_INT_HasOccurred(USB_INT_SOFI))
104 {
105 USB_INT_Clear(USB_INT_SOFI);
106
107 if (!(TimeoutMSRem--))
108 return ENDPOINT_READYWAIT_Timeout;
109 }
110 }
111 }
112
113 uint8_t Endpoint_Discard_Stream(uint16_t Length
114 #if !defined(NO_STREAM_CALLBACKS)
115 , StreamCallbackPtr_t Callback
116 #endif
117 )
118 {
119 uint8_t ErrorCode;
120
121 if ((ErrorCode = Endpoint_WaitUntilReady()))
122 return ErrorCode;
123
124 while (Length)
125 {
126 if (!(Endpoint_IsReadWriteAllowed()))
127 {
128 Endpoint_ClearOUT();
129
130 #if !defined(NO_STREAM_CALLBACKS)
131 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
132 return ENDPOINT_RWSTREAM_CallbackAborted;
133 #endif
134
135 if ((ErrorCode = Endpoint_WaitUntilReady()))
136 return ErrorCode;
137 }
138 else
139 {
140 Endpoint_Discard_Byte();
141 Length--;
142 }
143 }
144
145 return ENDPOINT_RWSTREAM_NoError;
146 }
147
148 uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
149 #if !defined(NO_STREAM_CALLBACKS)
150 , StreamCallbackPtr_t Callback
151 #endif
152 )
153 {
154 uint8_t* DataStream = (uint8_t*)Buffer;
155 uint8_t ErrorCode;
156
157 if ((ErrorCode = Endpoint_WaitUntilReady()))
158 return ErrorCode;
159
160 while (Length)
161 {
162 if (!(Endpoint_IsReadWriteAllowed()))
163 {
164 Endpoint_ClearIN();
165
166 #if !defined(NO_STREAM_CALLBACKS)
167 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
168 return ENDPOINT_RWSTREAM_CallbackAborted;
169 #endif
170
171 if ((ErrorCode = Endpoint_WaitUntilReady()))
172 return ErrorCode;
173 }
174 else
175 {
176 Endpoint_Write_Byte(*(DataStream++));
177 Length--;
178 }
179 }
180
181 return ENDPOINT_RWSTREAM_NoError;
182 }
183
184 uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
185 #if !defined(NO_STREAM_CALLBACKS)
186 , StreamCallbackPtr_t Callback
187 #endif
188 )
189 {
190 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
191 uint8_t ErrorCode;
192
193 if ((ErrorCode = Endpoint_WaitUntilReady()))
194 return ErrorCode;
195
196 while (Length)
197 {
198 if (!(Endpoint_IsReadWriteAllowed()))
199 {
200 Endpoint_ClearIN();
201
202 #if !defined(NO_STREAM_CALLBACKS)
203 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
204 return ENDPOINT_RWSTREAM_CallbackAborted;
205 #endif
206
207 if ((ErrorCode = Endpoint_WaitUntilReady()))
208 return ErrorCode;
209 }
210 else
211 {
212 Endpoint_Write_Byte(*(DataStream--));
213 Length--;
214 }
215 }
216
217 return ENDPOINT_RWSTREAM_NoError;
218 }
219
220 uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
221 #if !defined(NO_STREAM_CALLBACKS)
222 , StreamCallbackPtr_t Callback
223 #endif
224 )
225 {
226 uint8_t* DataStream = (uint8_t*)Buffer;
227 uint8_t ErrorCode;
228
229 if ((ErrorCode = Endpoint_WaitUntilReady()))
230 return ErrorCode;
231
232 while (Length)
233 {
234 if (!(Endpoint_IsReadWriteAllowed()))
235 {
236 Endpoint_ClearOUT();
237
238 #if !defined(NO_STREAM_CALLBACKS)
239 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
240 return ENDPOINT_RWSTREAM_CallbackAborted;
241 #endif
242
243 if ((ErrorCode = Endpoint_WaitUntilReady()))
244 return ErrorCode;
245 }
246 else
247 {
248 *(DataStream++) = Endpoint_Read_Byte();
249 Length--;
250 }
251 }
252
253 return ENDPOINT_RWSTREAM_NoError;
254 }
255
256 uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
257 #if !defined(NO_STREAM_CALLBACKS)
258 , StreamCallbackPtr_t Callback
259 #endif
260 )
261 {
262 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
263 uint8_t ErrorCode;
264
265 if ((ErrorCode = Endpoint_WaitUntilReady()))
266 return ErrorCode;
267
268 while (Length)
269 {
270 if (!(Endpoint_IsReadWriteAllowed()))
271 {
272 Endpoint_ClearOUT();
273
274 #if !defined(NO_STREAM_CALLBACKS)
275 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
276 return ENDPOINT_RWSTREAM_CallbackAborted;
277 #endif
278
279 if ((ErrorCode = Endpoint_WaitUntilReady()))
280 return ErrorCode;
281 }
282 else
283 {
284 *(DataStream--) = Endpoint_Read_Byte();
285 Length--;
286 }
287 }
288
289 return ENDPOINT_RWSTREAM_NoError;
290 }
291 #endif
292
293 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
294 {
295 uint8_t* DataStream = (uint8_t*)Buffer;
296 bool LastPacketFull = false;
297
298 if (Length > USB_ControlRequest.wLength)
299 Length = USB_ControlRequest.wLength;
300
301 while (Length && !(Endpoint_IsOUTReceived()))
302 {
303 while (!(Endpoint_IsINReady()));
304
305 while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
306 {
307 Endpoint_Write_Byte(*(DataStream++));
308 Length--;
309 }
310
311 LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);
312 Endpoint_ClearIN();
313 }
314
315 if (Endpoint_IsOUTReceived())
316 return ENDPOINT_RWCSTREAM_HostAborted;
317
318 if (LastPacketFull)
319 {
320 while (!(Endpoint_IsINReady()));
321 Endpoint_ClearIN();
322 }
323
324 while (!(Endpoint_IsOUTReceived()));
325
326 return ENDPOINT_RWCSTREAM_NoError;
327 }
328
329 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)
330 {
331 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
332 bool LastPacketFull = false;
333
334 if (Length > USB_ControlRequest.wLength)
335 Length = USB_ControlRequest.wLength;
336
337 while (Length && !(Endpoint_IsOUTReceived()))
338 {
339 if (Endpoint_IsINReady())
340 {
341 while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
342 {
343 Endpoint_Write_Byte(*(DataStream--));
344 Length--;
345 }
346
347 LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);
348 Endpoint_ClearIN();
349 }
350 }
351
352 if (Endpoint_IsOUTReceived())
353 return ENDPOINT_RWCSTREAM_HostAborted;
354
355 if (LastPacketFull)
356 {
357 while (!(Endpoint_IsINReady()));
358 Endpoint_ClearIN();
359 }
360
361 while (!(Endpoint_IsOUTReceived()));
362
363 return ENDPOINT_RWCSTREAM_NoError;
364 }
365
366 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)
367 {
368 uint8_t* DataStream = (uint8_t*)Buffer;
369
370 while (Length)
371 {
372 if (Endpoint_IsOUTReceived())
373 {
374 while (Length && Endpoint_BytesInEndpoint())
375 {
376 *(DataStream++) = Endpoint_Read_Byte();
377 Length--;
378 }
379
380 Endpoint_ClearOUT();
381 }
382 }
383
384 while (!(Endpoint_IsINReady()));
385
386 return ENDPOINT_RWCSTREAM_NoError;
387 }
388
389 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)
390 {
391 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
392
393 while (Length)
394 {
395 if (Endpoint_IsOUTReceived())
396 {
397 while (Length && Endpoint_BytesInEndpoint())
398 {
399 *(DataStream--) = Endpoint_Read_Byte();
400 Length--;
401 }
402
403 Endpoint_ClearOUT();
404 }
405 }
406
407 while (!(Endpoint_IsINReady()));
408
409 return ENDPOINT_RWCSTREAM_NoError;
410 }
411
412 #endif