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