]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/XMEGA/Endpoint_XMEGA.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / XMEGA / Endpoint_XMEGA.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_XMEGA)
33
34 #define __INCLUDE_FROM_USB_DRIVER
35 #include "../USBMode.h"
36
37 #if defined(USB_CAN_BE_DEVICE)
38
39 #include "../Endpoint.h"
40
41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
42 uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
43 #endif
44
45 Endpoint_FIFOPair_t USB_Endpoint_FIFOs[ENDPOINT_TOTAL_ENDPOINTS];
46
47 volatile uint8_t USB_Endpoint_SelectedEndpoint;
48 volatile USB_EP_t* USB_Endpoint_SelectedHandle;
49 volatile Endpoint_FIFO_t* USB_Endpoint_SelectedFIFO;
50
51 bool Endpoint_IsINReady(void)
52 {
53 Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint | ENDPOINT_DIR_IN);
54
55 return ((USB_Endpoint_SelectedHandle->STATUS & USB_EP_BUSNACK0_bm) ? true : false);
56 }
57
58 bool Endpoint_IsOUTReceived(void)
59 {
60 Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
61
62 if (USB_Endpoint_SelectedHandle->STATUS & USB_EP_TRNCOMPL0_bm)
63 {
64 USB_Endpoint_SelectedFIFO->Length = USB_Endpoint_SelectedHandle->CNT;
65 return true;
66 }
67
68 return false;
69 }
70
71 bool Endpoint_IsSETUPReceived(void)
72 {
73 Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
74
75 if (USB_Endpoint_SelectedHandle->STATUS & USB_EP_SETUP_bm)
76 {
77 USB_Endpoint_SelectedFIFO->Length = USB_Endpoint_SelectedHandle->CNT;
78 return true;
79 }
80
81 return false;
82 }
83
84 void Endpoint_ClearSETUP(void)
85 {
86 Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
87 USB_Endpoint_SelectedHandle->STATUS &= ~(USB_EP_SETUP_bm | USB_EP_TRNCOMPL0_bm | USB_EP_BUSNACK0_bm | USB_EP_OVF_bm);
88 USB_Endpoint_SelectedHandle->STATUS |= USB_EP_TOGGLE_bm;
89 USB_Endpoint_SelectedFIFO->Position = 0;
90
91 Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint | ENDPOINT_DIR_IN);
92 USB_Endpoint_SelectedHandle->STATUS |= USB_EP_TOGGLE_bm;
93 USB_Endpoint_SelectedFIFO->Position = 0;
94 }
95
96 void Endpoint_ClearIN(void)
97 {
98 USB_Endpoint_SelectedHandle->CNT = USB_Endpoint_SelectedFIFO->Position;
99 USB_Endpoint_SelectedHandle->STATUS &= ~(USB_EP_TRNCOMPL0_bm | USB_EP_BUSNACK0_bm | USB_EP_OVF_bm);
100 USB_Endpoint_SelectedFIFO->Position = 0;
101 }
102
103 void Endpoint_ClearOUT(void)
104 {
105 USB_Endpoint_SelectedHandle->STATUS &= ~(USB_EP_TRNCOMPL0_bm | USB_EP_BUSNACK0_bm | USB_EP_OVF_bm);
106 USB_Endpoint_SelectedFIFO->Position = 0;
107 }
108
109 void Endpoint_StallTransaction(void)
110 {
111 USB_Endpoint_SelectedHandle->CTRL |= USB_EP_STALL_bm;
112
113 if ((USB_Endpoint_SelectedHandle->CTRL & USB_EP_TYPE_gm) == USB_EP_TYPE_CONTROL_gc)
114 {
115 Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint ^ ENDPOINT_DIR_IN);
116 USB_Endpoint_SelectedHandle->CTRL |= USB_EP_STALL_bm;
117 }
118 }
119
120 uint8_t Endpoint_Read_8(void)
121 {
122 return USB_Endpoint_SelectedFIFO->Data[USB_Endpoint_SelectedFIFO->Position++];
123 }
124
125 void Endpoint_Write_8(const uint8_t Data)
126 {
127 USB_Endpoint_SelectedFIFO->Data[USB_Endpoint_SelectedFIFO->Position++] = Data;
128 }
129
130 void Endpoint_SelectEndpoint(const uint8_t Address)
131 {
132 uint8_t EndpointNumber = (Address & ENDPOINT_EPNUM_MASK);
133
134 USB_Endpoint_SelectedEndpoint = Address;
135
136 Endpoint_FIFOPair_t* EndpointFIFOPair = &USB_Endpoint_FIFOs[EndpointNumber];
137 USB_EndpointTable_t* EndpointTable = (USB_EndpointTable_t*)USB.EPPTR;
138
139 if (Address & ENDPOINT_DIR_IN)
140 {
141 USB_Endpoint_SelectedFIFO = &EndpointFIFOPair->IN;
142 USB_Endpoint_SelectedHandle = &EndpointTable->Endpoints[EndpointNumber].IN;
143 }
144 else
145 {
146 USB_Endpoint_SelectedFIFO = &EndpointFIFOPair->OUT;
147 USB_Endpoint_SelectedHandle = &EndpointTable->Endpoints[EndpointNumber].OUT;
148 }
149 }
150
151 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
152 const uint8_t Entries)
153 {
154 for (uint8_t i = 0; i < Entries; i++)
155 {
156 if (!(Table[i].Address))
157 continue;
158
159 if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
160 {
161 return false;
162 }
163 }
164
165 return true;
166 }
167
168 bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Address,
169 const uint8_t Config,
170 const uint8_t Size)
171 {
172 Endpoint_SelectEndpoint(Address);
173
174 USB_Endpoint_SelectedHandle->CTRL = 0;
175 USB_Endpoint_SelectedHandle->STATUS = (Address & ENDPOINT_DIR_IN) ? USB_EP_BUSNACK0_bm : 0;
176 USB_Endpoint_SelectedHandle->CTRL = Config;
177 USB_Endpoint_SelectedHandle->CNT = 0;
178 USB_Endpoint_SelectedHandle->DATAPTR = (intptr_t)USB_Endpoint_SelectedFIFO->Data;
179
180 USB_Endpoint_SelectedFIFO->Length = (Address & ENDPOINT_DIR_IN) ? Size : 0;
181 USB_Endpoint_SelectedFIFO->Position = 0;
182
183 return true;
184 }
185
186 void Endpoint_ClearEndpoints(void)
187 {
188 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
189 {
190 ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].IN.CTRL = 0;
191 ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].OUT.CTRL = 0;
192 }
193 }
194
195 void Endpoint_ClearStatusStage(void)
196 {
197 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
198 {
199 while (!(Endpoint_IsOUTReceived()))
200 {
201 if (USB_DeviceState == DEVICE_STATE_Unattached)
202 return;
203 }
204
205 Endpoint_ClearOUT();
206 }
207 else
208 {
209 while (!(Endpoint_IsINReady()))
210 {
211 if (USB_DeviceState == DEVICE_STATE_Unattached)
212 return;
213 }
214
215 Endpoint_ClearIN();
216 }
217 }
218
219 #if !defined(CONTROL_ONLY_DEVICE)
220 uint8_t Endpoint_WaitUntilReady(void)
221 {
222 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
223 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
224 #else
225 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
226 #endif
227
228 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
229
230 for (;;)
231 {
232 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
233 {
234 if (Endpoint_IsINReady())
235 return ENDPOINT_READYWAIT_NoError;
236 }
237 else
238 {
239 if (Endpoint_IsOUTReceived())
240 return ENDPOINT_READYWAIT_NoError;
241 }
242
243 uint8_t USB_DeviceState_LCL = USB_DeviceState;
244
245 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
246 return ENDPOINT_READYWAIT_DeviceDisconnected;
247 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
248 return ENDPOINT_READYWAIT_BusSuspended;
249 else if (Endpoint_IsStalled())
250 return ENDPOINT_READYWAIT_EndpointStalled;
251
252 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
253
254 if (CurrentFrameNumber != PreviousFrameNumber)
255 {
256 PreviousFrameNumber = CurrentFrameNumber;
257
258 if (!(TimeoutMSRem--))
259 return ENDPOINT_READYWAIT_Timeout;
260 }
261 }
262 }
263 #endif
264
265 #endif
266
267 #endif
268
Imprint / Impressum