Add new tag for the LUFA-120219-BETA release.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Endpoint_AVR8.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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #include "../Endpoint.h"
37
38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39 uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40 #endif
41
42 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
43 const uint8_t UECFG0XData,
44 const uint8_t UECFG1XData)
45 {
46 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
47 Endpoint_SelectEndpoint(Number);
48 Endpoint_EnableEndpoint();
49
50 UECFG1X = 0;
51 UECFG0X = UECFG0XData;
52 UECFG1X = UECFG1XData;
53
54 return Endpoint_IsConfigured();
55 #else
56 for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
57 {
58 uint8_t UECFG0XTemp;
59 uint8_t UECFG1XTemp;
60 uint8_t UEIENXTemp;
61
62 Endpoint_SelectEndpoint(EPNum);
63
64 if (EPNum == Number)
65 {
66 UECFG0XTemp = UECFG0XData;
67 UECFG1XTemp = UECFG1XData;
68 UEIENXTemp = 0;
69 }
70 else
71 {
72 UECFG0XTemp = UECFG0X;
73 UECFG1XTemp = UECFG1X;
74 UEIENXTemp = UEIENX;
75 }
76
77 if (!(UECFG1XTemp & (1 << ALLOC)))
78 continue;
79
80 Endpoint_DisableEndpoint();
81 UECFG1X &= ~(1 << ALLOC);
82
83 Endpoint_EnableEndpoint();
84 UECFG0X = UECFG0XTemp;
85 UECFG1X = UECFG1XTemp;
86 UEIENX = UEIENXTemp;
87
88 if (!(Endpoint_IsConfigured()))
89 return false;
90 }
91
92 Endpoint_SelectEndpoint(Number);
93 return true;
94 #endif
95 }
96
97 void Endpoint_ClearEndpoints(void)
98 {
99 UEINT = 0;
100
101 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
102 {
103 Endpoint_SelectEndpoint(EPNum);
104 UEIENX = 0;
105 UEINTX = 0;
106 UECFG1X = 0;
107 Endpoint_DisableEndpoint();
108 }
109 }
110
111 void Endpoint_ClearStatusStage(void)
112 {
113 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
114 {
115 while (!(Endpoint_IsOUTReceived()))
116 {
117 if (USB_DeviceState == DEVICE_STATE_Unattached)
118 return;
119 }
120
121 Endpoint_ClearOUT();
122 }
123 else
124 {
125 while (!(Endpoint_IsINReady()))
126 {
127 if (USB_DeviceState == DEVICE_STATE_Unattached)
128 return;
129 }
130
131 Endpoint_ClearIN();
132 }
133 }
134
135 #if !defined(CONTROL_ONLY_DEVICE)
136 uint8_t Endpoint_WaitUntilReady(void)
137 {
138 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
139 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
140 #else
141 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
142 #endif
143
144 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
145
146 for (;;)
147 {
148 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
149 {
150 if (Endpoint_IsINReady())
151 return ENDPOINT_READYWAIT_NoError;
152 }
153 else
154 {
155 if (Endpoint_IsOUTReceived())
156 return ENDPOINT_READYWAIT_NoError;
157 }
158
159 uint8_t USB_DeviceState_LCL = USB_DeviceState;
160
161 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
162 return ENDPOINT_READYWAIT_DeviceDisconnected;
163 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
164 return ENDPOINT_READYWAIT_BusSuspended;
165 else if (Endpoint_IsStalled())
166 return ENDPOINT_READYWAIT_EndpointStalled;
167
168 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
169
170 if (CurrentFrameNumber != PreviousFrameNumber)
171 {
172 PreviousFrameNumber = CurrentFrameNumber;
173
174 if (!(TimeoutMSRem--))
175 return ENDPOINT_READYWAIT_Timeout;
176 }
177 }
178 }
179 #endif
180
181 #endif
182