Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[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 uint8_t Pipe_WaitUntilReady(void)
74 {
75 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
76 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
77 #else
78 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
79 #endif
80
81 for (;;)
82 {
83 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
84 {
85 if (Pipe_IsINReceived())
86 return PIPE_READYWAIT_NoError;
87 }
88 else
89 {
90 if (Pipe_IsOUTReady())
91 return PIPE_READYWAIT_NoError;
92 }
93
94 if (Pipe_IsStalled())
95 return PIPE_READYWAIT_PipeStalled;
96 else if (USB_HostState == HOST_STATE_Unattached)
97 return PIPE_READYWAIT_DeviceDisconnected;
98
99 if (USB_INT_HasOccurred(USB_INT_HSOFI))
100 {
101 USB_INT_Clear(USB_INT_HSOFI);
102
103 if (!(TimeoutMSRem--))
104 return PIPE_READYWAIT_Timeout;
105 }
106 }
107 }
108
109 uint8_t Pipe_Discard_Stream(uint16_t Length
110 #if !defined(NO_STREAM_CALLBACKS)
111 , StreamCallbackPtr_t Callback
112 #endif
113 )
114 {
115 uint8_t ErrorCode;
116
117 Pipe_SetToken(PIPE_TOKEN_IN);
118
119 if ((ErrorCode = Pipe_WaitUntilReady()))
120 return ErrorCode;
121
122 #if defined(FAST_STREAM_TRANSFERS)
123 uint8_t BytesRemToAlignment = (Pipe_BytesInPipe() & 0x07);
124
125 if (Length >= 8)
126 {
127 Length -= BytesRemToAlignment;
128
129 switch (BytesRemToAlignment)
130 {
131 default:
132 do
133 {
134 if (!(Pipe_IsReadWriteAllowed()))
135 {
136 Pipe_ClearIN();
137
138 #if !defined(NO_STREAM_CALLBACKS)
139 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
140 return PIPE_RWSTREAM_CallbackAborted;
141 #endif
142
143 if ((ErrorCode = Pipe_WaitUntilReady()))
144 return ErrorCode;
145 }
146
147 Length -= 8;
148
149 Pipe_Discard_Byte();
150 case 7: Pipe_Discard_Byte();
151 case 6: Pipe_Discard_Byte();
152 case 5: Pipe_Discard_Byte();
153 case 4: Pipe_Discard_Byte();
154 case 3: Pipe_Discard_Byte();
155 case 2: Pipe_Discard_Byte();
156 case 1: Pipe_Discard_Byte();
157 } while (Length >= 8);
158 }
159 }
160 #endif
161
162 while (Length)
163 {
164 if (!(Pipe_IsReadWriteAllowed()))
165 {
166 Pipe_ClearIN();
167
168 #if !defined(NO_STREAM_CALLBACKS)
169 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
170 return PIPE_RWSTREAM_CallbackAborted;
171 #endif
172
173 if ((ErrorCode = Pipe_WaitUntilReady()))
174 return ErrorCode;
175 }
176 else
177 {
178 Pipe_Discard_Byte();
179 Length--;
180 }
181 }
182
183 return PIPE_RWSTREAM_NoError;
184 }
185
186 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
187 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
188 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
189 #define TEMPLATE_BUFFER_OFFSET(Length) 0
190 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*(BufferPtr++))
191 #include "Template/Template_Pipe_RW.c"
192
193 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
194 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
195 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
196 #define TEMPLATE_BUFFER_OFFSET(Length) 0
197 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte(BufferPtr++))
198 #include "Template/Template_Pipe_RW.c"
199
200 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
201 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
202 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
203 #define TEMPLATE_BUFFER_OFFSET(Length) 0
204 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte(BufferPtr++))
205 #include "Template/Template_Pipe_RW.c"
206
207 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
208 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
209 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
210 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
211 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*(BufferPtr--))
212 #include "Template/Template_Pipe_RW.c"
213
214 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
215 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
216 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
217 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
218 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte(BufferPtr--))
219 #include "Template/Template_Pipe_RW.c"
220
221 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
222 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
223 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
224 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
225 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte(BufferPtr--))
226 #include "Template/Template_Pipe_RW.c"
227
228 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
229 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
230 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
231 #define TEMPLATE_BUFFER_OFFSET(Length) 0
232 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *(BufferPtr++) = Pipe_Read_Byte()
233 #include "Template/Template_Pipe_RW.c"
234
235 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
236 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
237 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
238 #define TEMPLATE_BUFFER_OFFSET(Length) 0
239 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte(BufferPtr++, Pipe_Read_Byte())
240 #include "Template/Template_Pipe_RW.c"
241
242 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
243 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
244 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
245 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
246 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *(BufferPtr--) = Pipe_Read_Byte()
247 #include "Template/Template_Pipe_RW.c"
248
249 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
250 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
251 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
252 #define TEMPLATE_BUFFER_OFFSET(Length) Length - 1
253 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte(BufferPtr--, Pipe_Read_Byte())
254 #include "Template/Template_Pipe_RW.c"
255
256 #endif