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