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