]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/XMEGA/Endpoint_XMEGA.c
Squashed 'tmk_core/' content from commit 05caacc
[tmk_keyboard.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / XMEGA / Endpoint_XMEGA.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 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 "../../../../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_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
52 const uint8_t Entries)
53 {
54 for (uint8_t i = 0; i < Entries; i++)
55 {
56 if (!(Table[i].Address))
57 continue;
58
59 if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
60 {
61 return false;
62 }
63 }
64
65 return true;
66 }
67
68 bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Address,
69 const uint8_t Config,
70 const uint8_t Size)
71 {
72 Endpoint_SelectEndpoint(Address);
73
74 USB_Endpoint_SelectedHandle->CTRL = 0;
75 USB_Endpoint_SelectedHandle->STATUS = (Address & ENDPOINT_DIR_IN) ? USB_EP_BUSNACK0_bm : 0;
76 USB_Endpoint_SelectedHandle->CTRL = Config;
77 USB_Endpoint_SelectedHandle->CNT = 0;
78 USB_Endpoint_SelectedHandle->DATAPTR = (intptr_t)USB_Endpoint_SelectedFIFO->Data;
79
80 USB_Endpoint_SelectedFIFO->Length = (Address & ENDPOINT_DIR_IN) ? Size : 0;
81 USB_Endpoint_SelectedFIFO->Position = 0;
82
83 return true;
84 }
85
86 void Endpoint_ClearEndpoints(void)
87 {
88 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
89 {
90 ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].IN.CTRL = 0;
91 ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].OUT.CTRL = 0;
92 }
93 }
94
95 void Endpoint_ClearStatusStage(void)
96 {
97 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
98 {
99 while (!(Endpoint_IsOUTReceived()))
100 {
101 if (USB_DeviceState == DEVICE_STATE_Unattached)
102 return;
103 }
104
105 Endpoint_ClearOUT();
106 }
107 else
108 {
109 while (!(Endpoint_IsINReady()))
110 {
111 if (USB_DeviceState == DEVICE_STATE_Unattached)
112 return;
113 }
114
115 Endpoint_ClearIN();
116 }
117 }
118
119 #if !defined(CONTROL_ONLY_DEVICE)
120 uint8_t Endpoint_WaitUntilReady(void)
121 {
122 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
123 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
124 #else
125 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
126 #endif
127
128 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
129
130 for (;;)
131 {
132 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
133 {
134 if (Endpoint_IsINReady())
135 return ENDPOINT_READYWAIT_NoError;
136 }
137 else
138 {
139 if (Endpoint_IsOUTReceived())
140 return ENDPOINT_READYWAIT_NoError;
141 }
142
143 uint8_t USB_DeviceState_LCL = USB_DeviceState;
144
145 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
146 return ENDPOINT_READYWAIT_DeviceDisconnected;
147 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
148 return ENDPOINT_READYWAIT_BusSuspended;
149 else if (Endpoint_IsStalled())
150 return ENDPOINT_READYWAIT_EndpointStalled;
151
152 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
153
154 if (CurrentFrameNumber != PreviousFrameNumber)
155 {
156 PreviousFrameNumber = CurrentFrameNumber;
157
158 if (!(TimeoutMSRem--))
159 return ENDPOINT_READYWAIT_Timeout;
160 }
161 }
162 }
163 #endif
164
165 #endif
166
167 #endif
168
Imprint / Impressum