]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/UC3/Host_UC3.c
Squashed 'tmk_core/' content from commit 05caacc
[tmk_keyboard.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / UC3 / Host_UC3.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 disclaim 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_HOST)
38
39 #define __INCLUDE_FROM_HOST_C
40 #include "../Host.h"
41
42 void USB_Host_ProcessNextHostState(void)
43 {
44 uint8_t ErrorCode = HOST_ENUMERROR_NoError;
45 uint8_t SubErrorCode = HOST_ENUMERROR_NoError;
46
47 static uint16_t WaitMSRemaining;
48 static uint8_t PostWaitState;
49
50 switch (USB_HostState)
51 {
52 case HOST_STATE_WaitForDevice:
53 if (WaitMSRemaining)
54 {
55 if ((SubErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
56 {
57 USB_HostState = PostWaitState;
58 ErrorCode = HOST_ENUMERROR_WaitStage;
59 break;
60 }
61
62 if (!(--WaitMSRemaining))
63 USB_HostState = PostWaitState;
64 }
65
66 break;
67 case HOST_STATE_Powered:
68 WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
69
70 USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;
71 break;
72 case HOST_STATE_Powered_WaitForDeviceSettle:
73 if (WaitMSRemaining--)
74 {
75 Delay_MS(1);
76 break;
77 }
78 else
79 {
80 USB_Host_VBUS_Manual_Off();
81
82 USB_OTGPAD_On();
83 USB_Host_VBUS_Auto_Enable();
84 USB_Host_VBUS_Auto_On();
85
86 #if defined(NO_AUTO_VBUS_MANAGEMENT)
87 USB_Host_VBUS_Manual_Enable();
88 USB_Host_VBUS_Manual_On();
89 #endif
90
91 USB_HostState = HOST_STATE_Powered_WaitForConnect;
92 }
93
94 break;
95 case HOST_STATE_Powered_WaitForConnect:
96 if (USB_INT_HasOccurred(USB_INT_DCONNI))
97 {
98 USB_INT_Clear(USB_INT_DCONNI);
99 USB_INT_Clear(USB_INT_DDISCI);
100
101 USB_INT_Clear(USB_INT_VBERRI);
102 USB_INT_Enable(USB_INT_VBERRI);
103
104 USB_Host_ResumeBus();
105 Pipe_ClearPipes();
106
107 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
108 }
109
110 break;
111 case HOST_STATE_Powered_DoReset:
112 USB_Host_ResetDevice();
113
114 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
115 break;
116 case HOST_STATE_Powered_ConfigPipe:
117 if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL, ENDPOINT_CONTROLEP, PIPE_CONTROLPIPE_DEFAULT_SIZE, 1)))
118 {
119 ErrorCode = HOST_ENUMERROR_PipeConfigError;
120 SubErrorCode = 0;
121 break;
122 }
123
124 USB_HostState = HOST_STATE_Default;
125 break;
126 case HOST_STATE_Default:
127 USB_ControlRequest = (USB_Request_Header_t)
128 {
129 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
130 .bRequest = REQ_GetDescriptor,
131 .wValue = (DTYPE_Device << 8),
132 .wIndex = 0,
133 .wLength = 8,
134 };
135
136 uint8_t DataBuffer[8];
137
138 Pipe_SelectPipe(PIPE_CONTROLPIPE);
139 if ((SubErrorCode = USB_Host_SendControlRequest(DataBuffer)) != HOST_SENDCONTROL_Successful)
140 {
141 ErrorCode = HOST_ENUMERROR_ControlError;
142 break;
143 }
144
145 USB_Host_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
146
147 USB_Host_ResetDevice();
148
149 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
150 break;
151 case HOST_STATE_Default_PostReset:
152 if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL, ENDPOINT_CONTROLEP, USB_Host_ControlPipeSize, 1)))
153 {
154 ErrorCode = HOST_ENUMERROR_PipeConfigError;
155 SubErrorCode = 0;
156 break;
157 }
158
159 USB_ControlRequest = (USB_Request_Header_t)
160 {
161 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
162 .bRequest = REQ_SetAddress,
163 .wValue = USB_HOST_DEVICEADDRESS,
164 .wIndex = 0,
165 .wLength = 0,
166 };
167
168 if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
169 {
170 ErrorCode = HOST_ENUMERROR_ControlError;
171 break;
172 }
173
174 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
175 break;
176 case HOST_STATE_Default_PostAddressSet:
177 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
178
179 USB_HostState = HOST_STATE_Addressed;
180
181 EVENT_USB_Host_DeviceEnumerationComplete();
182 break;
183 }
184
185 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
186 {
187 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
188
189 USB_Host_VBUS_Auto_Off();
190
191 EVENT_USB_Host_DeviceUnattached();
192
193 USB_ResetInterface();
194 }
195 }
196
197 uint8_t USB_Host_WaitMS(uint8_t MS)
198 {
199 bool BusSuspended = USB_Host_IsBusSuspended();
200 uint8_t ErrorCode = HOST_WAITERROR_Successful;
201 bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
202
203 USB_INT_Disable(USB_INT_HSOFI);
204 USB_INT_Clear(USB_INT_HSOFI);
205
206 USB_Host_ResumeBus();
207
208 while (MS)
209 {
210 if (USB_INT_HasOccurred(USB_INT_HSOFI))
211 {
212 USB_INT_Clear(USB_INT_HSOFI);
213 MS--;
214 }
215
216 if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode != USB_MODE_Host))
217 {
218 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
219
220 break;
221 }
222
223 if (Pipe_IsError())
224 {
225 Pipe_ClearError();
226 ErrorCode = HOST_WAITERROR_PipeError;
227
228 break;
229 }
230
231 if (Pipe_IsStalled())
232 {
233 Pipe_ClearStall();
234 ErrorCode = HOST_WAITERROR_SetupStalled;
235
236 break;
237 }
238 }
239
240 if (BusSuspended)
241 USB_Host_SuspendBus();
242
243 if (HSOFIEnabled)
244 USB_INT_Enable(USB_INT_HSOFI);
245
246 return ErrorCode;
247 }
248
249 static void USB_Host_ResetDevice(void)
250 {
251 bool BusSuspended = USB_Host_IsBusSuspended();
252
253 USB_INT_Disable(USB_INT_DDISCI);
254
255 USB_Host_ResetBus();
256 while (!(USB_Host_IsBusResetComplete()));
257 USB_Host_ResumeBus();
258
259 USB_Host_ConfigurationNumber = 0;
260
261 bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
262
263 USB_INT_Disable(USB_INT_HSOFI);
264 USB_INT_Clear(USB_INT_HSOFI);
265
266 for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
267 {
268 /* Workaround for powerless-pull-up devices. After a USB bus reset,
269 all disconnection interrupts are suppressed while a USB frame is
270 looked for - if it is found within 10ms, the device is still
271 present. */
272
273 if (USB_INT_HasOccurred(USB_INT_HSOFI))
274 {
275 USB_INT_Clear(USB_INT_HSOFI);
276 USB_INT_Clear(USB_INT_DDISCI);
277 break;
278 }
279
280 Delay_MS(1);
281 }
282
283 if (HSOFIEnabled)
284 USB_INT_Enable(USB_INT_HSOFI);
285
286 if (BusSuspended)
287 USB_Host_SuspendBus();
288
289 USB_INT_Enable(USB_INT_DDISCI);
290 }
291
292 #endif
293
294 #endif
Imprint / Impressum