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