]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/UC3/EndpointStream_UC3.c
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / UC3 / EndpointStream_UC3.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4               \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this \r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in \r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting \r
17   documentation, and that the name of the author not be used in \r
18   advertising or publicity pertaining to distribution of the \r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #include "../../../../Common/Common.h"\r
32 #if (ARCH == ARCH_UC3)\r
33 \r
34 #define  __INCLUDE_FROM_USB_DRIVER\r
35 #include "../USBMode.h"\r
36 \r
37 #if defined(USB_CAN_BE_DEVICE)\r
38 \r
39 #include "EndpointStream_UC3.h"\r
40 \r
41 #if !defined(CONTROL_ONLY_DEVICE)\r
42 uint8_t Endpoint_Discard_Stream(uint16_t Length,\r
43                                 uint16_t* const BytesProcessed)\r
44 {\r
45         uint8_t  ErrorCode;\r
46         uint16_t BytesInTransfer = 0;\r
47         \r
48         if ((ErrorCode = Endpoint_WaitUntilReady()))\r
49           return ErrorCode;\r
50           \r
51         if (BytesProcessed != NULL)\r
52           Length -= *BytesProcessed;\r
53 \r
54         while (Length)\r
55         {\r
56                 if (!(Endpoint_IsReadWriteAllowed()))\r
57                 {\r
58                         Endpoint_ClearOUT();\r
59 \r
60                         if (BytesProcessed != NULL)\r
61                         {\r
62                                 *BytesProcessed += BytesInTransfer;\r
63                                 return ENDPOINT_RWSTREAM_IncompleteTransfer;\r
64                         }\r
65 \r
66                         if ((ErrorCode = Endpoint_WaitUntilReady()))\r
67                           return ErrorCode;\r
68                 }\r
69                 else\r
70                 {\r
71                         Endpoint_Discard_8();\r
72 \r
73                         Length--;\r
74                         BytesInTransfer++;\r
75                 }\r
76         }\r
77         \r
78         return ENDPOINT_RWSTREAM_NoError;\r
79 }\r
80 \r
81 uint8_t Endpoint_Null_Stream(uint16_t Length,\r
82                              uint16_t* const BytesProcessed)\r
83 {\r
84         uint8_t  ErrorCode;\r
85         uint16_t BytesInTransfer = 0;\r
86         \r
87         if ((ErrorCode = Endpoint_WaitUntilReady()))\r
88           return ErrorCode;\r
89           \r
90         if (BytesProcessed != NULL)\r
91           Length -= *BytesProcessed;\r
92 \r
93         while (Length)\r
94         {\r
95                 if (!(Endpoint_IsReadWriteAllowed()))\r
96                 {\r
97                         Endpoint_ClearIN();\r
98 \r
99                         if (BytesProcessed != NULL)\r
100                         {\r
101                                 *BytesProcessed += BytesInTransfer;\r
102                                 return ENDPOINT_RWSTREAM_IncompleteTransfer;\r
103                         }\r
104 \r
105                         if ((ErrorCode = Endpoint_WaitUntilReady()))\r
106                           return ErrorCode;\r
107                 }\r
108                 else\r
109                 {\r
110                         Endpoint_Write_8(0);\r
111 \r
112                         Length--;\r
113                         BytesInTransfer++;\r
114                 }\r
115         }\r
116         \r
117         return ENDPOINT_RWSTREAM_NoError;\r
118 }\r
119 \r
120 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,\r
121  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */\r
122 \r
123 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE\r
124 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
125 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
126 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
127 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
128 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)\r
129 #include "Template/Template_Endpoint_RW.c"\r
130 \r
131 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE\r
132 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
133 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
134 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
135 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
136 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)\r
137 #include "Template/Template_Endpoint_RW.c"\r
138 \r
139 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE\r
140 #define  TEMPLATE_BUFFER_TYPE                      void*\r
141 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
142 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
143 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
144 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()\r
145 #include "Template/Template_Endpoint_RW.c"\r
146 \r
147 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE\r
148 #define  TEMPLATE_BUFFER_TYPE                      void*\r
149 #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
150 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
151 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
152 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()\r
153 #include "Template/Template_Endpoint_RW.c"\r
154 \r
155 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE)\r
156         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE\r
157         #define  TEMPLATE_BUFFER_TYPE                      const void*\r
158         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
159         #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
160         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
161         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr))\r
162         #include "Template/Template_Endpoint_RW.c"\r
163 \r
164         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE\r
165         #define  TEMPLATE_BUFFER_TYPE                      const void*\r
166         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
167         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
168         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
169         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(pgm_read_byte(BufferPtr))\r
170         #include "Template/Template_Endpoint_RW.c"\r
171 #endif\r
172 \r
173 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE)\r
174         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE\r
175         #define  TEMPLATE_BUFFER_TYPE                      const void*\r
176         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
177         #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
178         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
179         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr))\r
180         #include "Template/Template_Endpoint_RW.c"\r
181 \r
182         #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE\r
183         #define  TEMPLATE_BUFFER_TYPE                      const void*\r
184         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
185         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
186         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
187         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(eeprom_read_byte(BufferPtr))\r
188         #include "Template/Template_Endpoint_RW.c"\r
189 \r
190         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE\r
191         #define  TEMPLATE_BUFFER_TYPE                      void*\r
192         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
193         #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
194         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
195         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8())\r
196         #include "Template/Template_Endpoint_RW.c"\r
197 \r
198         #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE\r
199         #define  TEMPLATE_BUFFER_TYPE                      void*\r
200         #define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
201         #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
202         #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
203         #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Endpoint_Read_8())\r
204         #include "Template/Template_Endpoint_RW.c"\r
205 #endif\r
206 \r
207 #endif\r
208 \r
209 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE\r
210 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
211 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
212 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)\r
213 #include "Template/Template_Endpoint_Control_W.c"\r
214 \r
215 #define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE\r
216 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
217 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
218 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_8(*BufferPtr)\r
219 #include "Template/Template_Endpoint_Control_W.c"\r
220 \r
221 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE\r
222 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
223 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr += Amount\r
224 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()\r
225 #include "Template/Template_Endpoint_Control_R.c"\r
226 \r
227 #define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE\r
228 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
229 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   BufferPtr -= Amount\r
230 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Endpoint_Read_8()\r
231 #include "Template/Template_Endpoint_Control_R.c"\r
232 \r
233 #endif\r
234 \r
235 #endif\r