]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/AVR8/Endpoint_AVR8.c
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / AVR8 / Endpoint_AVR8.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #include "../../../../Common/Common.h"\r
32 #if (ARCH == ARCH_AVR8)\r
33 \r
34 #define  __INCLUDE_FROM_USB_DRIVER\r
35 #include "../USBMode.h"\r
36 \r
37 #if defined(USB_CAN_BE_DEVICE)\r
38 \r
39 #include "../Endpoint.h"\r
40 \r
41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)\r
42 uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;\r
43 #endif\r
44 \r
45 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,\r
46                                      const uint8_t Entries)\r
47 {\r
48         for (uint8_t i = 0; i < Entries; i++)\r
49         {\r
50                 if (!(Table[i].Address))\r
51                   continue;\r
52         \r
53                 if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))\r
54                   return false;\r
55         }\r
56         \r
57         return true;\r
58 }\r
59 \r
60 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,\r
61                                     const uint8_t UECFG0XData,\r
62                                     const uint8_t UECFG1XData)\r
63 {\r
64 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)\r
65         Endpoint_SelectEndpoint(Number);\r
66         Endpoint_EnableEndpoint();\r
67 \r
68         UECFG1X = 0;\r
69         UECFG0X = UECFG0XData;\r
70         UECFG1X = UECFG1XData;\r
71 \r
72         return Endpoint_IsConfigured();\r
73 #else\r
74         for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)\r
75         {\r
76                 uint8_t UECFG0XTemp;\r
77                 uint8_t UECFG1XTemp;\r
78                 uint8_t UEIENXTemp;\r
79 \r
80                 Endpoint_SelectEndpoint(EPNum);\r
81 \r
82                 if (EPNum == Number)\r
83                 {\r
84                         UECFG0XTemp = UECFG0XData;\r
85                         UECFG1XTemp = UECFG1XData;\r
86                         UEIENXTemp  = 0;\r
87                 }\r
88                 else\r
89                 {\r
90                         UECFG0XTemp = UECFG0X;\r
91                         UECFG1XTemp = UECFG1X;\r
92                         UEIENXTemp  = UEIENX;\r
93                 }\r
94 \r
95                 if (!(UECFG1XTemp & (1 << ALLOC)))\r
96                   continue;\r
97 \r
98                 Endpoint_DisableEndpoint();\r
99                 UECFG1X &= ~(1 << ALLOC);\r
100 \r
101                 Endpoint_EnableEndpoint();\r
102                 UECFG0X = UECFG0XTemp;\r
103                 UECFG1X = UECFG1XTemp;\r
104                 UEIENX  = UEIENXTemp;\r
105 \r
106                 if (!(Endpoint_IsConfigured()))\r
107                   return false;\r
108         }\r
109 \r
110         Endpoint_SelectEndpoint(Number);\r
111         return true;\r
112 #endif\r
113 }\r
114 \r
115 void Endpoint_ClearEndpoints(void)\r
116 {\r
117         UEINT = 0;\r
118 \r
119         for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)\r
120         {\r
121                 Endpoint_SelectEndpoint(EPNum);\r
122                 UEIENX  = 0;\r
123                 UEINTX  = 0;\r
124                 UECFG1X = 0;\r
125                 Endpoint_DisableEndpoint();\r
126         }\r
127 }\r
128 \r
129 void Endpoint_ClearStatusStage(void)\r
130 {\r
131         if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)\r
132         {\r
133                 while (!(Endpoint_IsOUTReceived()))\r
134                 {\r
135                         if (USB_DeviceState == DEVICE_STATE_Unattached)\r
136                           return;\r
137                 }\r
138 \r
139                 Endpoint_ClearOUT();\r
140         }\r
141         else\r
142         {\r
143                 while (!(Endpoint_IsINReady()))\r
144                 {\r
145                         if (USB_DeviceState == DEVICE_STATE_Unattached)\r
146                           return;\r
147                 }\r
148 \r
149                 Endpoint_ClearIN();\r
150         }\r
151 }\r
152 \r
153 #if !defined(CONTROL_ONLY_DEVICE)\r
154 uint8_t Endpoint_WaitUntilReady(void)\r
155 {\r
156         #if (USB_STREAM_TIMEOUT_MS < 0xFF)\r
157         uint8_t  TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
158         #else\r
159         uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
160         #endif\r
161 \r
162         uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();\r
163 \r
164         for (;;)\r
165         {\r
166                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)\r
167                 {\r
168                         if (Endpoint_IsINReady())\r
169                           return ENDPOINT_READYWAIT_NoError;\r
170                 }\r
171                 else\r
172                 {\r
173                         if (Endpoint_IsOUTReceived())\r
174                           return ENDPOINT_READYWAIT_NoError;\r
175                 }\r
176 \r
177                 uint8_t USB_DeviceState_LCL = USB_DeviceState;\r
178 \r
179                 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)\r
180                   return ENDPOINT_READYWAIT_DeviceDisconnected;\r
181                 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)\r
182                   return ENDPOINT_READYWAIT_BusSuspended;\r
183                 else if (Endpoint_IsStalled())\r
184                   return ENDPOINT_READYWAIT_EndpointStalled;\r
185 \r
186                 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();\r
187 \r
188                 if (CurrentFrameNumber != PreviousFrameNumber)\r
189                 {\r
190                         PreviousFrameNumber = CurrentFrameNumber;\r
191 \r
192                         if (!(TimeoutMSRem--))\r
193                           return ENDPOINT_READYWAIT_Timeout;\r
194                 }\r
195         }\r
196 }\r
197 #endif\r
198 \r
199 #endif\r
200 \r
201 #endif\r