]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/AVR8/Endpoint_AVR8.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / AVR8 / Endpoint_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_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 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
46                                      const uint8_t Entries)
47 {
48         for (uint8_t i = 0; i < Entries; i++)
49         {
50                 if (!(Table[i].Address))
51                   continue;
52
53                 if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
54                   return false;
55         }
56
57         return true;
58 }
59
60 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
61                                     const uint8_t UECFG0XData,
62                                     const uint8_t UECFG1XData)
63 {
64 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
65         Endpoint_SelectEndpoint(Number);
66         Endpoint_EnableEndpoint();
67
68         UECFG1X = 0;
69         UECFG0X = UECFG0XData;
70         UECFG1X = UECFG1XData;
71
72         return Endpoint_IsConfigured();
73 #else
74         for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
75         {
76                 uint8_t UECFG0XTemp;
77                 uint8_t UECFG1XTemp;
78                 uint8_t UEIENXTemp;
79
80                 Endpoint_SelectEndpoint(EPNum);
81
82                 if (EPNum == Number)
83                 {
84                         UECFG0XTemp = UECFG0XData;
85                         UECFG1XTemp = UECFG1XData;
86                         UEIENXTemp  = 0;
87                 }
88                 else
89                 {
90                         UECFG0XTemp = UECFG0X;
91                         UECFG1XTemp = UECFG1X;
92                         UEIENXTemp  = UEIENX;
93                 }
94
95                 if (!(UECFG1XTemp & (1 << ALLOC)))
96                   continue;
97
98                 Endpoint_DisableEndpoint();
99                 UECFG1X &= ~(1 << ALLOC);
100
101                 Endpoint_EnableEndpoint();
102                 UECFG0X = UECFG0XTemp;
103                 UECFG1X = UECFG1XTemp;
104                 UEIENX  = UEIENXTemp;
105
106                 if (!(Endpoint_IsConfigured()))
107                   return false;
108         }
109
110         Endpoint_SelectEndpoint(Number);
111         return true;
112 #endif
113 }
114
115 void Endpoint_ClearEndpoints(void)
116 {
117         UEINT = 0;
118
119         for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
120         {
121                 Endpoint_SelectEndpoint(EPNum);
122                 UEIENX  = 0;
123                 UEINTX  = 0;
124                 UECFG1X = 0;
125                 Endpoint_DisableEndpoint();
126         }
127 }
128
129 void Endpoint_ClearStatusStage(void)
130 {
131         if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
132         {
133                 while (!(Endpoint_IsOUTReceived()))
134                 {
135                         if (USB_DeviceState == DEVICE_STATE_Unattached)
136                           return;
137                 }
138
139                 Endpoint_ClearOUT();
140         }
141         else
142         {
143                 while (!(Endpoint_IsINReady()))
144                 {
145                         if (USB_DeviceState == DEVICE_STATE_Unattached)
146                           return;
147                 }
148
149                 Endpoint_ClearIN();
150         }
151 }
152
153 #if !defined(CONTROL_ONLY_DEVICE)
154 uint8_t Endpoint_WaitUntilReady(void)
155 {
156         #if (USB_STREAM_TIMEOUT_MS < 0xFF)
157         uint8_t  TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
158         #else
159         uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
160         #endif
161
162         uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
163
164         for (;;)
165         {
166                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
167                 {
168                         if (Endpoint_IsINReady())
169                           return ENDPOINT_READYWAIT_NoError;
170                 }
171                 else
172                 {
173                         if (Endpoint_IsOUTReceived())
174                           return ENDPOINT_READYWAIT_NoError;
175                 }
176
177                 uint8_t USB_DeviceState_LCL = USB_DeviceState;
178
179                 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
180                   return ENDPOINT_READYWAIT_DeviceDisconnected;
181                 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
182                   return ENDPOINT_READYWAIT_BusSuspended;
183                 else if (Endpoint_IsStalled())
184                   return ENDPOINT_READYWAIT_EndpointStalled;
185
186                 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
187
188                 if (CurrentFrameNumber != PreviousFrameNumber)
189                 {
190                         PreviousFrameNumber = CurrentFrameNumber;
191
192                         if (!(TimeoutMSRem--))
193                           return ENDPOINT_READYWAIT_Timeout;
194                 }
195         }
196 }
197 #endif
198
199 #endif
200
201 #endif