Re-add in old Endpoint/Pipe workaround for unordered pipes - add new ORDERED_EP_CONFI...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 "../HighLevel/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_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 uint8_t UECFG0XTemp[ENDPOINT_TOTAL_ENDPOINTS];
57 uint8_t UECFG1XTemp[ENDPOINT_TOTAL_ENDPOINTS];
58 uint8_t UEIENXTemp[ENDPOINT_TOTAL_ENDPOINTS];
59
60 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
61 {
62 Endpoint_SelectEndpoint(EPNum);
63 UECFG0XTemp[EPNum] = UECFG0X;
64 UECFG1XTemp[EPNum] = UECFG1X;
65 UEIENXTemp[EPNum] = UEIENX;
66 }
67
68 UECFG0XTemp[Number] = UECFG0XData;
69 UECFG1XTemp[Number] = UECFG1XData;
70 UEIENXTemp[Number] = 0;
71
72 for (uint8_t EPNum = 1; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
73 {
74 Endpoint_SelectEndpoint(EPNum);
75 UEIENX = 0;
76 UEINTX = 0;
77 UECFG1X = 0;
78 Endpoint_DisableEndpoint();
79 }
80
81 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
82 {
83 if (!(UECFG1XTemp[EPNum] & (1 << ALLOC)))
84 continue;
85
86 Endpoint_SelectEndpoint(EPNum);
87 Endpoint_EnableEndpoint();
88
89 UECFG0X = UECFG0XTemp[EPNum];
90 UECFG1X = UECFG1XTemp[EPNum];
91 UEIENX = UEIENXTemp[EPNum];
92
93 if (!(Endpoint_IsConfigured()))
94 return false;
95 }
96
97 Endpoint_SelectEndpoint(Number);
98 return true;
99 #endif
100 }
101
102 void Endpoint_ClearEndpoints(void)
103 {
104 UEINT = 0;
105
106 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
107 {
108 Endpoint_SelectEndpoint(EPNum);
109 UEIENX = 0;
110 UEINTX = 0;
111 UECFG1X = 0;
112 Endpoint_DisableEndpoint();
113 }
114 }
115
116 void Endpoint_ClearStatusStage(void)
117 {
118 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
119 {
120 while (!(Endpoint_IsOUTReceived()))
121 {
122 if (USB_DeviceState == DEVICE_STATE_Unattached)
123 return;
124 }
125
126 Endpoint_ClearOUT();
127 }
128 else
129 {
130 while (!(Endpoint_IsINReady()))
131 {
132 if (USB_DeviceState == DEVICE_STATE_Unattached)
133 return;
134 }
135
136 Endpoint_ClearIN();
137 }
138 }
139
140 #if !defined(CONTROL_ONLY_DEVICE)
141 uint8_t Endpoint_WaitUntilReady(void)
142 {
143 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
144 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
145 #else
146 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
147 #endif
148
149 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
150
151 for (;;)
152 {
153 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
154 {
155 if (Endpoint_IsINReady())
156 return ENDPOINT_READYWAIT_NoError;
157 }
158 else
159 {
160 if (Endpoint_IsOUTReceived())
161 return ENDPOINT_READYWAIT_NoError;
162 }
163
164 uint8_t USB_DeviceState_LCL = USB_DeviceState;
165
166 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
167 return ENDPOINT_READYWAIT_DeviceDisconnected;
168 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
169 return ENDPOINT_READYWAIT_BusSuspended;
170 else if (Endpoint_IsStalled())
171 return ENDPOINT_READYWAIT_EndpointStalled;
172
173 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
174
175 if (CurrentFrameNumber != PreviousFrameNumber)
176 {
177 PreviousFrameNumber = CurrentFrameNumber;
178
179 if (!(TimeoutMSRem--))
180 return ENDPOINT_READYWAIT_Timeout;
181 }
182 }
183 }
184 #endif
185
186 #endif
187