57c613a1f9e6d355518cd4bfd9b36f76d683f65b
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../HighLevel/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define __INCLUDE_FROM_ENDPOINT_C
37 #include "Endpoint.h"
38
39 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
40 uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
41 #endif
42
43 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
44 const uint8_t UECFG0XData,
45 const uint8_t UECFG1XData)
46 {
47 uint8_t UECFG0XTemp[ENDPOINT_TOTAL_ENDPOINTS];
48 uint8_t UECFG1XTemp[ENDPOINT_TOTAL_ENDPOINTS];
49
50 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
51 {
52 Endpoint_SelectEndpoint(EPNum);
53 UECFG0XTemp[EPNum] = UECFG0X;
54 UECFG1XTemp[EPNum] = UECFG1X;
55 }
56
57 UECFG0XTemp[Number] = UECFG0XData;
58 UECFG1XTemp[Number] = UECFG1XData;
59
60 for (uint8_t EPNum = 1; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
61 {
62 Endpoint_SelectEndpoint(EPNum);
63 UEIENX = 0;
64 UEINTX = 0;
65 UECFG1X = 0;
66 Endpoint_DisableEndpoint();
67 }
68
69 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
70 {
71 if (!(UECFG1XTemp[EPNum] & (1 << ALLOC)))
72 continue;
73
74 Endpoint_SelectEndpoint(EPNum);
75 Endpoint_EnableEndpoint();
76
77 UECFG0X = UECFG0XTemp[EPNum];
78 UECFG1X = UECFG1XTemp[EPNum];
79
80 if (!(Endpoint_IsConfigured()))
81 return false;
82 }
83
84 Endpoint_SelectEndpoint(Number);
85 return true;
86 }
87
88 void Endpoint_ClearEndpoints(void)
89 {
90 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
91 {
92 Endpoint_SelectEndpoint(EPNum);
93 UEIENX = 0;
94 UEINTX = 0;
95 UECFG1X = 0;
96 Endpoint_DisableEndpoint();
97 }
98 }
99
100 void Endpoint_ClearStatusStage(void)
101 {
102 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
103 {
104 while (!(Endpoint_IsOUTReceived()))
105 {
106 if (USB_DeviceState == DEVICE_STATE_Unattached)
107 return;
108 }
109
110 Endpoint_ClearOUT();
111 }
112 else
113 {
114 while (!(Endpoint_IsINReady()))
115 {
116 if (USB_DeviceState == DEVICE_STATE_Unattached)
117 return;
118 }
119
120 Endpoint_ClearIN();
121 }
122 }
123
124 #if !defined(CONTROL_ONLY_DEVICE)
125 uint8_t Endpoint_WaitUntilReady(void)
126 {
127 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
128 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
129 #else
130 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
131 #endif
132
133 for (;;)
134 {
135 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
136 {
137 if (Endpoint_IsINReady())
138 return ENDPOINT_READYWAIT_NoError;
139 }
140 else
141 {
142 if (Endpoint_IsOUTReceived())
143 return ENDPOINT_READYWAIT_NoError;
144 }
145
146 if (USB_DeviceState == DEVICE_STATE_Unattached)
147 return ENDPOINT_READYWAIT_DeviceDisconnected;
148 else if (USB_DeviceState == DEVICE_STATE_Suspended)
149 return ENDPOINT_READYWAIT_BusSuspended;
150 else if (Endpoint_IsStalled())
151 return ENDPOINT_READYWAIT_EndpointStalled;
152
153 if (USB_INT_HasOccurred(USB_INT_SOFI))
154 {
155 USB_INT_Clear(USB_INT_SOFI);
156
157 if (!(TimeoutMSRem--))
158 return ENDPOINT_READYWAIT_Timeout;
159 }
160 }
161 }
162
163 uint8_t Endpoint_Discard_Stream(uint16_t Length
164 #if !defined(NO_STREAM_CALLBACKS)
165 , StreamCallbackPtr_t Callback
166 #endif
167 )
168 {
169 uint8_t ErrorCode;
170
171 if ((ErrorCode = Endpoint_WaitUntilReady()))
172 return ErrorCode;
173
174 #if defined(FAST_STREAM_TRANSFERS)
175 uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07);
176
177 if (Length >= 8)
178 {
179 Length -= BytesRemToAlignment;
180
181 switch (BytesRemToAlignment)
182 {
183 default:
184 do
185 {
186 if (!(Endpoint_IsReadWriteAllowed()))
187 {
188 Endpoint_ClearOUT();
189
190 #if !defined(NO_STREAM_CALLBACKS)
191 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
192 return ENDPOINT_RWSTREAM_CallbackAborted;
193 #endif
194
195 if ((ErrorCode = Endpoint_WaitUntilReady()))
196 return ErrorCode;
197 }
198
199 Length -= 8;
200
201 Endpoint_Discard_Byte();
202 case 7: Endpoint_Discard_Byte();
203 case 6: Endpoint_Discard_Byte();
204 case 5: Endpoint_Discard_Byte();
205 case 4: Endpoint_Discard_Byte();
206 case 3: Endpoint_Discard_Byte();
207 case 2: Endpoint_Discard_Byte();
208 case 1: Endpoint_Discard_Byte();
209 } while (Length >= 8);
210 }
211 }
212 #endif
213
214 while (Length)
215 {
216 if (!(Endpoint_IsReadWriteAllowed()))
217 {
218 Endpoint_ClearOUT();
219
220 #if !defined(NO_STREAM_CALLBACKS)
221 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
222 return ENDPOINT_RWSTREAM_CallbackAborted;
223 #endif
224
225 if ((ErrorCode = Endpoint_WaitUntilReady()))
226 return ErrorCode;
227 }
228 else
229 {
230 Endpoint_Discard_Byte();
231 Length--;
232 }
233 }
234
235 return ENDPOINT_RWSTREAM_NoError;
236 }
237
238 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
239 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
240
241 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE
242 #define TEMPLATE_BUFFER_TYPE const void*
243 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
244 #define TEMPLATE_BUFFER_OFFSET(Length) 0
245 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
246 #include "Template/Template_Endpoint_RW.c"
247
248 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE
249 #define TEMPLATE_BUFFER_TYPE const void*
250 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
251 #define TEMPLATE_BUFFER_OFFSET(Length) 0
252 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
253 #include "Template/Template_Endpoint_RW.c"
254
255 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_LE
256 #define TEMPLATE_BUFFER_TYPE const void*
257 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
258 #define TEMPLATE_BUFFER_OFFSET(Length) 0
259 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
260 #include "Template/Template_Endpoint_RW.c"
261
262 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE
263 #define TEMPLATE_BUFFER_TYPE const void*
264 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
265 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
266 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
267 #include "Template/Template_Endpoint_RW.c"
268
269 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE
270 #define TEMPLATE_BUFFER_TYPE const void*
271 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
272 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
273 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
274 #include "Template/Template_Endpoint_RW.c"
275
276 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE
277 #define TEMPLATE_BUFFER_TYPE const void*
278 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
279 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
280 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
281 #include "Template/Template_Endpoint_RW.c"
282
283 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE
284 #define TEMPLATE_BUFFER_TYPE void*
285 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
286 #define TEMPLATE_BUFFER_OFFSET(Length) 0
287 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
288 #include "Template/Template_Endpoint_RW.c"
289
290 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE
291 #define TEMPLATE_BUFFER_TYPE void*
292 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
293 #define TEMPLATE_BUFFER_OFFSET(Length) 0
294 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
295 #include "Template/Template_Endpoint_RW.c"
296
297 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE
298 #define TEMPLATE_BUFFER_TYPE void*
299 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
300 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
301 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
302 #include "Template/Template_Endpoint_RW.c"
303
304 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE
305 #define TEMPLATE_BUFFER_TYPE void*
306 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
307 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
308 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
309 #include "Template/Template_Endpoint_RW.c"
310
311 #endif
312
313 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE
314 #define TEMPLATE_BUFFER_OFFSET(Length) 0
315 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
316 #include "Template/Template_Endpoint_Control_W.c"
317
318 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_LE
319 #define TEMPLATE_BUFFER_OFFSET(Length) 0
320 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
321 #include "Template/Template_Endpoint_Control_W.c"
322
323 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_LE
324 #define TEMPLATE_BUFFER_OFFSET(Length) 0
325 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
326 #include "Template/Template_Endpoint_Control_W.c"
327
328 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE
329 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
330 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
331 #include "Template/Template_Endpoint_Control_W.c"
332
333 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_BE
334 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
335 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
336 #include "Template/Template_Endpoint_Control_W.c"
337
338 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_BE
339 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
340 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
341 #include "Template/Template_Endpoint_Control_W.c"
342
343 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE
344 #define TEMPLATE_BUFFER_OFFSET(Length) 0
345 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
346 #include "Template/Template_Endpoint_Control_R.c"
347
348 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_LE
349 #define TEMPLATE_BUFFER_OFFSET(Length) 0
350 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
351 #include "Template/Template_Endpoint_Control_R.c"
352
353 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE
354 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
355 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
356 #include "Template/Template_Endpoint_Control_R.c"
357
358 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_BE
359 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
360 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
361 #include "Template/Template_Endpoint_Control_R.c"
362
363 #endif