]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/LUFA/Drivers/USB/Core/AVR8/EndpointStream_AVR8.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / LUFA / Drivers / USB / Core / AVR8 / EndpointStream_AVR8.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 disclaims 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 "../../../../Common/Common.h"
32 #if (ARCH == ARCH_AVR8)
33
34 #define  __INCLUDE_FROM_USB_DRIVER
35 #include "../USBMode.h"
36
37 #if defined(USB_CAN_BE_DEVICE)
38
39 #include "EndpointStream_AVR8.h"
40
41 #if !defined(CONTROL_ONLY_DEVICE)
42 uint8_t Endpoint_Discard_Stream(uint16_t Length,
43                                 uint16_t* const BytesProcessed)
44 {
45         uint8_t  ErrorCode;
46         uint16_t BytesInTransfer = 0;
47
48         if ((ErrorCode = Endpoint_WaitUntilReady()))
49           return ErrorCode;
50
51         if (BytesProcessed != NULL)
52           Length -= *BytesProcessed;
53
54         while (Length)
55         {
56                 if (!(Endpoint_IsReadWriteAllowed()))
57                 {
58                         Endpoint_ClearOUT();
59
60                         if (BytesProcessed != NULL)
61                         {
62                                 *BytesProcessed += BytesInTransfer;
63                                 return ENDPOINT_RWSTREAM_IncompleteTransfer;
64                         }
65
66                         if ((ErrorCode = Endpoint_WaitUntilReady()))
67                           return ErrorCode;
68                 }
69                 else
70                 {
71                         Endpoint_Discard_8();
72
73                         Length--;
74                         BytesInTransfer++;
75                 }
76         }
77
78         return ENDPOINT_RWSTREAM_NoError;
79 }
80
81 uint8_t Endpoint_Null_Stream(uint16_t Length,
82                              uint16_t* const BytesProcessed)
83 {
84         uint8_t  ErrorCode;
85         uint16_t BytesInTransfer = 0;
86
87         if ((ErrorCode = Endpoint_WaitUntilReady()))
88           return ErrorCode;
89
90         if (BytesProcessed != NULL)
91           Length -= *BytesProcessed;
92
93         while (Length)
94         {
95                 if (!(Endpoint_IsReadWriteAllowed()))
96                 {
97                         Endpoint_ClearIN();
98
99                         if (BytesProcessed != NULL)
100                         {
101                                 *BytesProcessed += BytesInTransfer;
102                                 return ENDPOINT_RWSTREAM_IncompleteTransfer;
103                         }
104
105                         if ((ErrorCode = Endpoint_WaitUntilReady()))
106                           return ErrorCode;
107                 }
108                 else
109                 {
110                         Endpoint_Write_8(0);
111
112                         Length--;
113                         BytesInTransfer++;
114                 }
115         }
116
117         return ENDPOINT_RWSTREAM_NoError;
118 }
119
120 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,
121  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
122
123 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE
124 #define  TEMPLATE_BUFFER_TYPE                      const void*
125 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()
126 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
127 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
128 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)
129 #include "Template/Template_Endpoint_RW.c"
130
131 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE
132 #define  TEMPLATE_BUFFER_TYPE                      const void*
133 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()
134 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
135 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
136 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)
137 #include "Template/Template_Endpoint_RW.c"
138
139 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE
140 #define  TEMPLATE_BUFFER_TYPE                      void*
141 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()
142 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
143 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
144 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()
145 #include "Template/Template_Endpoint_RW.c"
146
147 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE
148 #define  TEMPLATE_BUFFER_TYPE                      void*
149 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()
150 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
151 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
152 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()
153 #include "Template/Template_Endpoint_RW.c"
154
155 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE)
156         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE
157         #define  TEMPLATE_BUFFER_TYPE                      const void*
158         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()
159         #define  TEMPLATE_BUFFER_OFFSET(Length)            0
160         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
161         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr))
162         #include "Template/Template_Endpoint_RW.c"
163
164         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE
165         #define  TEMPLATE_BUFFER_TYPE                      const void*
166         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()
167         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
168         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
169         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr))
170         #include "Template/Template_Endpoint_RW.c"
171 #endif
172
173 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE)
174         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE
175         #define  TEMPLATE_BUFFER_TYPE                      const void*
176         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()
177         #define  TEMPLATE_BUFFER_OFFSET(Length)            0
178         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
179         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr))
180         #include "Template/Template_Endpoint_RW.c"
181
182         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE
183         #define  TEMPLATE_BUFFER_TYPE                      const void*
184         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()
185         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
186         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
187         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr))
188         #include "Template/Template_Endpoint_RW.c"
189
190         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE
191         #define  TEMPLATE_BUFFER_TYPE                      void*
192         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()
193         #define  TEMPLATE_BUFFER_OFFSET(Length)            0
194         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
195         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8())
196         #include "Template/Template_Endpoint_RW.c"
197
198         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE
199         #define  TEMPLATE_BUFFER_TYPE                      void*
200         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()
201         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
202         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
203         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8())
204         #include "Template/Template_Endpoint_RW.c"
205 #endif
206
207 #endif
208
209 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE
210 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
211 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
212 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)
213 #include "Template/Template_Endpoint_Control_W.c"
214
215 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE
216 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
217 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
218 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)
219 #include "Template/Template_Endpoint_Control_W.c"
220
221 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE
222 #define  TEMPLATE_BUFFER_OFFSET(Length)            0
223 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
224 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()
225 #include "Template/Template_Endpoint_Control_R.c"
226
227 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE
228 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
229 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
230 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()
231 #include "Template/Template_Endpoint_Control_R.c"
232
233 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE)
234         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_LE
235         #define  TEMPLATE_BUFFER_OFFSET(Length)            0
236         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
237         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr))
238         #include "Template/Template_Endpoint_Control_W.c"
239
240         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_BE
241         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
242         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
243         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr))
244         #include "Template/Template_Endpoint_Control_W.c"
245 #endif
246
247 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE)
248         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_LE
249         #define  TEMPLATE_BUFFER_OFFSET(Length)            0
250         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
251         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr))
252         #include "Template/Template_Endpoint_Control_W.c"
253
254         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_BE
255         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
256         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
257         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr))
258         #include "Template/Template_Endpoint_Control_W.c"
259
260         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_LE
261         #define  TEMPLATE_BUFFER_OFFSET(Length)            0
262         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount
263         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8())
264         #include "Template/Template_Endpoint_Control_R.c"
265
266         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_BE
267         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)
268         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount
269         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8())
270         #include "Template/Template_Endpoint_Control_R.c"
271 #endif
272
273 #endif
274
275 #endif