]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/AVR8/PipeStream_AVR8.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / AVR8 / PipeStream_AVR8.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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_HOST)
38
39 #include "PipeStream_AVR8.h"
40
41 uint8_t Pipe_Discard_Stream(uint16_t Length,
42 uint16_t* const BytesProcessed)
43 {
44 uint8_t ErrorCode;
45 uint16_t BytesInTransfer = 0;
46
47 Pipe_SetPipeToken(PIPE_TOKEN_IN);
48
49 if ((ErrorCode = Pipe_WaitUntilReady()))
50 return ErrorCode;
51
52 if (BytesProcessed != NULL)
53 Length -= *BytesProcessed;
54
55 while (Length)
56 {
57 if (!(Pipe_IsReadWriteAllowed()))
58 {
59 Pipe_ClearIN();
60
61 if (BytesProcessed != NULL)
62 {
63 *BytesProcessed += BytesInTransfer;
64 return PIPE_RWSTREAM_IncompleteTransfer;
65 }
66
67 if ((ErrorCode = Pipe_WaitUntilReady()))
68 return ErrorCode;
69 }
70 else
71 {
72 Pipe_Discard_8();
73
74 Length--;
75 BytesInTransfer++;
76 }
77 }
78
79 return PIPE_RWSTREAM_NoError;
80 }
81
82 uint8_t Pipe_Null_Stream(uint16_t Length,
83 uint16_t* const BytesProcessed)
84 {
85 uint8_t ErrorCode;
86 uint16_t BytesInTransfer = 0;
87
88 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
89
90 if ((ErrorCode = Pipe_WaitUntilReady()))
91 return ErrorCode;
92
93 if (BytesProcessed != NULL)
94 Length -= *BytesProcessed;
95
96 while (Length)
97 {
98 if (!(Pipe_IsReadWriteAllowed()))
99 {
100 Pipe_ClearOUT();
101
102 if (BytesProcessed != NULL)
103 {
104 *BytesProcessed += BytesInTransfer;
105 return PIPE_RWSTREAM_IncompleteTransfer;
106 }
107
108 USB_USBTask();
109
110 if ((ErrorCode = Pipe_WaitUntilReady()))
111 return ErrorCode;
112 }
113 else
114 {
115 Pipe_Write_8(0);
116
117 Length--;
118 BytesInTransfer++;
119 }
120 }
121
122 return PIPE_RWSTREAM_NoError;
123 }
124
125 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,
126 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
127
128 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
129 #define TEMPLATE_BUFFER_TYPE const void*
130 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
131 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
132 #define TEMPLATE_BUFFER_OFFSET(Length) 0
133 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
134 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(*BufferPtr)
135 #include "Template/Template_Pipe_RW.c"
136
137 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
138 #define TEMPLATE_BUFFER_TYPE const void*
139 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
140 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
141 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
142 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
143 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(*BufferPtr)
144 #include "Template/Template_Pipe_RW.c"
145
146 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
147 #define TEMPLATE_BUFFER_TYPE void*
148 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
149 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
150 #define TEMPLATE_BUFFER_OFFSET(Length) 0
151 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
152 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Pipe_Read_8()
153 #include "Template/Template_Pipe_RW.c"
154
155 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
156 #define TEMPLATE_BUFFER_TYPE void*
157 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
158 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
159 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
160 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
161 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Pipe_Read_8()
162 #include "Template/Template_Pipe_RW.c"
163
164 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
165 #define TEMPLATE_BUFFER_TYPE const void*
166 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
167 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
168 #define TEMPLATE_BUFFER_OFFSET(Length) 0
169 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
170 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(pgm_read_byte(BufferPtr))
171 #include "Template/Template_Pipe_RW.c"
172
173 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
174 #define TEMPLATE_BUFFER_TYPE const void*
175 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
176 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
177 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
178 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
179 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(pgm_read_byte(BufferPtr))
180 #include "Template/Template_Pipe_RW.c"
181
182 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
183 #define TEMPLATE_BUFFER_TYPE const void*
184 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
185 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
186 #define TEMPLATE_BUFFER_OFFSET(Length) 0
187 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
188 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(eeprom_read_byte(BufferPtr))
189 #include "Template/Template_Pipe_RW.c"
190
191 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
192 #define TEMPLATE_BUFFER_TYPE const void*
193 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
194 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
195 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
196 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
197 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_8(eeprom_read_byte(BufferPtr))
198 #include "Template/Template_Pipe_RW.c"
199
200 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
201 #define TEMPLATE_BUFFER_TYPE void*
202 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
203 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
204 #define TEMPLATE_BUFFER_OFFSET(Length) 0
205 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
206 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Pipe_Read_8())
207 #include "Template/Template_Pipe_RW.c"
208
209 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
210 #define TEMPLATE_BUFFER_TYPE void*
211 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
212 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
213 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
214 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
215 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Pipe_Read_8())
216 #include "Template/Template_Pipe_RW.c"
217
218 #endif
219
220 #endif
221
Imprint / Impressum