Fixed broken USB_GetNextDescriptor() function causing the descriptor to jump ahead...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.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_HOST)
34
35 #define INCLUDE_FROM_PIPE_C
36 #include "Pipe.h"
37
38 uint8_t USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39
40 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
41 const uint16_t Size, const uint8_t Banks)
42 {
43 Pipe_SelectPipe(Number);
44 Pipe_EnablePipe();
45
46 UPCFG1X = 0;
47
48 UPCFG0X = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
49 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
50
51 Pipe_SetInfiniteINRequests();
52
53 return Pipe_IsConfigured();
54 }
55
56 void Pipe_ClearPipes(void)
57 {
58 UPINT = 0;
59
60 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
61 {
62 Pipe_ResetPipe(PNum);
63 Pipe_SelectPipe(PNum);
64 UPIENX = 0;
65 UPINTX = 0;
66 Pipe_ClearError();
67 Pipe_ClearErrorFlags();
68 Pipe_DeallocateMemory();
69 Pipe_DisablePipe();
70 }
71 }
72
73 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
74 {
75 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
76
77 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
78 {
79 Pipe_SelectPipe(PNum);
80
81 if (Pipe_IsConfigured() && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
82 return true;
83 }
84
85 Pipe_SelectPipe(PrevPipeNumber);
86 return false;
87 }
88
89 uint8_t Pipe_WaitUntilReady(void)
90 {
91 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
92 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
93 #else
94 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
95 #endif
96
97 for (;;)
98 {
99 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
100 {
101 if (Pipe_IsINReceived())
102 return PIPE_READYWAIT_NoError;
103 }
104 else
105 {
106 if (Pipe_IsOUTReady())
107 return PIPE_READYWAIT_NoError;
108 }
109
110 if (Pipe_IsStalled())
111 return PIPE_READYWAIT_PipeStalled;
112 else if (USB_HostState == HOST_STATE_Unattached)
113 return PIPE_READYWAIT_DeviceDisconnected;
114
115 if (USB_INT_HasOccurred(USB_INT_HSOFI))
116 {
117 USB_INT_Clear(USB_INT_HSOFI);
118
119 if (!(TimeoutMSRem--))
120 return PIPE_READYWAIT_Timeout;
121 }
122 }
123 }
124
125 uint8_t Pipe_Discard_Stream(uint16_t Length
126 #if !defined(NO_STREAM_CALLBACKS)
127 , StreamCallbackPtr_t Callback
128 #endif
129 )
130 {
131 uint8_t ErrorCode;
132
133 Pipe_SetToken(PIPE_TOKEN_IN);
134
135 if ((ErrorCode = Pipe_WaitUntilReady()))
136 return ErrorCode;
137
138 #if defined(FAST_STREAM_TRANSFERS)
139 uint8_t BytesRemToAlignment = (Pipe_BytesInPipe() & 0x07);
140
141 if (Length >= 8)
142 {
143 Length -= BytesRemToAlignment;
144
145 switch (BytesRemToAlignment)
146 {
147 default:
148 do
149 {
150 if (!(Pipe_IsReadWriteAllowed()))
151 {
152 Pipe_ClearIN();
153
154 #if !defined(NO_STREAM_CALLBACKS)
155 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
156 return PIPE_RWSTREAM_CallbackAborted;
157 #endif
158
159 if ((ErrorCode = Pipe_WaitUntilReady()))
160 return ErrorCode;
161 }
162
163 Length -= 8;
164
165 Pipe_Discard_Byte();
166 case 7: Pipe_Discard_Byte();
167 case 6: Pipe_Discard_Byte();
168 case 5: Pipe_Discard_Byte();
169 case 4: Pipe_Discard_Byte();
170 case 3: Pipe_Discard_Byte();
171 case 2: Pipe_Discard_Byte();
172 case 1: Pipe_Discard_Byte();
173 } while (Length >= 8);
174 }
175 }
176 #endif
177
178 while (Length)
179 {
180 if (!(Pipe_IsReadWriteAllowed()))
181 {
182 Pipe_ClearIN();
183
184 #if !defined(NO_STREAM_CALLBACKS)
185 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
186 return PIPE_RWSTREAM_CallbackAborted;
187 #endif
188
189 if ((ErrorCode = Pipe_WaitUntilReady()))
190 return ErrorCode;
191 }
192 else
193 {
194 Pipe_Discard_Byte();
195 Length--;
196 }
197 }
198
199 return PIPE_RWSTREAM_NoError;
200 }
201
202 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
203 #define TEMPLATE_BUFFER_TYPE const void*
204 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
205 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
206 #define TEMPLATE_BUFFER_OFFSET(Length) 0
207 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr++))
208 #include "Template/Template_Pipe_RW.c"
209
210 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
211 #define TEMPLATE_BUFFER_TYPE const void*
212 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
213 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
214 #define TEMPLATE_BUFFER_OFFSET(Length) 0
215 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
216 #include "Template/Template_Pipe_RW.c"
217
218 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
219 #define TEMPLATE_BUFFER_TYPE const void*
220 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
221 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
222 #define TEMPLATE_BUFFER_OFFSET(Length) 0
223 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
224 #include "Template/Template_Pipe_RW.c"
225
226 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
227 #define TEMPLATE_BUFFER_TYPE const void*
228 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
229 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
230 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
231 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr--))
232 #include "Template/Template_Pipe_RW.c"
233
234 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
235 #define TEMPLATE_BUFFER_TYPE const void*
236 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
237 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
238 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
239 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
240 #include "Template/Template_Pipe_RW.c"
241
242 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
243 #define TEMPLATE_BUFFER_TYPE const void*
244 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
245 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
246 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
247 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
248 #include "Template/Template_Pipe_RW.c"
249
250 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
251 #define TEMPLATE_BUFFER_TYPE void*
252 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
253 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
254 #define TEMPLATE_BUFFER_OFFSET(Length) 0
255 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Pipe_Read_Byte()
256 #include "Template/Template_Pipe_RW.c"
257
258 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
259 #define TEMPLATE_BUFFER_TYPE void*
260 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
261 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
262 #define TEMPLATE_BUFFER_OFFSET(Length) 0
263 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr++, Pipe_Read_Byte())
264 #include "Template/Template_Pipe_RW.c"
265
266 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
267 #define TEMPLATE_BUFFER_TYPE void*
268 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
269 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
270 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
271 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Pipe_Read_Byte()
272 #include "Template/Template_Pipe_RW.c"
273
274 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
275 #define TEMPLATE_BUFFER_TYPE void*
276 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
277 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
278 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
279 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr--, Pipe_Read_Byte())
280 #include "Template/Template_Pipe_RW.c"
281
282 #endif