]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/XMEGA/Endpoint_XMEGA.c
Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core'
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / XMEGA / Endpoint_XMEGA.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_XMEGA)\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 Endpoint_FIFOPair_t       USB_Endpoint_FIFOs[ENDPOINT_TOTAL_ENDPOINTS];\r
46 \r
47 volatile uint8_t          USB_Endpoint_SelectedEndpoint;\r
48 volatile USB_EP_t*        USB_Endpoint_SelectedHandle;\r
49 volatile Endpoint_FIFO_t* USB_Endpoint_SelectedFIFO;\r
50 \r
51 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,\r
52                                      const uint8_t Entries)\r
53 {\r
54         for (uint8_t i = 0; i < Entries; i++)\r
55         {\r
56                 if (!(Table[i].Address))\r
57                   continue;\r
58         \r
59                 if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))\r
60                 {\r
61                         return false;\r
62                 }\r
63         }\r
64         \r
65         return true;\r
66 }\r
67 \r
68 bool Endpoint_ConfigureEndpoint_PRV(const uint8_t Address,\r
69                                     const uint8_t Config,\r
70                                     const uint8_t Size)\r
71 {\r
72         Endpoint_SelectEndpoint(Address);\r
73 \r
74         USB_Endpoint_SelectedHandle->CTRL    = 0;\r
75         USB_Endpoint_SelectedHandle->STATUS  = (Address & ENDPOINT_DIR_IN) ? USB_EP_BUSNACK0_bm : 0;\r
76         USB_Endpoint_SelectedHandle->CTRL    = Config;\r
77         USB_Endpoint_SelectedHandle->CNT     = 0;\r
78         USB_Endpoint_SelectedHandle->DATAPTR = (intptr_t)USB_Endpoint_SelectedFIFO->Data;\r
79 \r
80         USB_Endpoint_SelectedFIFO->Length    = (Address & ENDPOINT_DIR_IN) ? Size : 0;\r
81         USB_Endpoint_SelectedFIFO->Position  = 0;\r
82 \r
83         return true;\r
84 }\r
85 \r
86 void Endpoint_ClearEndpoints(void)\r
87 {\r
88         for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)\r
89         {\r
90                 ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].IN.CTRL  = 0;\r
91                 ((USB_EndpointTable_t*)USB.EPPTR)->Endpoints[EPNum].OUT.CTRL = 0;\r
92         }\r
93 }\r
94 \r
95 void Endpoint_ClearStatusStage(void)\r
96 {\r
97         if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)\r
98         {\r
99                 while (!(Endpoint_IsOUTReceived()))\r
100                 {\r
101                         if (USB_DeviceState == DEVICE_STATE_Unattached)\r
102                           return;\r
103                 }\r
104 \r
105                 Endpoint_ClearOUT();\r
106         }\r
107         else\r
108         {\r
109                 while (!(Endpoint_IsINReady()))\r
110                 {\r
111                         if (USB_DeviceState == DEVICE_STATE_Unattached)\r
112                           return;\r
113                 }\r
114 \r
115                 Endpoint_ClearIN();\r
116         }\r
117 }\r
118 \r
119 #if !defined(CONTROL_ONLY_DEVICE)\r
120 uint8_t Endpoint_WaitUntilReady(void)\r
121 {\r
122         #if (USB_STREAM_TIMEOUT_MS < 0xFF)\r
123         uint8_t  TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
124         #else\r
125         uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
126         #endif\r
127 \r
128         uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();\r
129 \r
130         for (;;)\r
131         {\r
132                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)\r
133                 {\r
134                         if (Endpoint_IsINReady())\r
135                           return ENDPOINT_READYWAIT_NoError;\r
136                 }\r
137                 else\r
138                 {\r
139                         if (Endpoint_IsOUTReceived())\r
140                           return ENDPOINT_READYWAIT_NoError;\r
141                 }\r
142 \r
143                 uint8_t USB_DeviceState_LCL = USB_DeviceState;\r
144 \r
145                 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)\r
146                   return ENDPOINT_READYWAIT_DeviceDisconnected;\r
147                 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)\r
148                   return ENDPOINT_READYWAIT_BusSuspended;\r
149                 else if (Endpoint_IsStalled())\r
150                   return ENDPOINT_READYWAIT_EndpointStalled;\r
151 \r
152                 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();\r
153 \r
154                 if (CurrentFrameNumber != PreviousFrameNumber)\r
155                 {\r
156                         PreviousFrameNumber = CurrentFrameNumber;\r
157 \r
158                         if (!(TimeoutMSRem--))\r
159                           return ENDPOINT_READYWAIT_Timeout;\r
160                 }\r
161         }\r
162 }\r
163 #endif\r
164 \r
165 #endif\r
166 \r
167 #endif\r
168 \r