Fixed host state machine not enabling Auto VBUS mode when HOST_DEVICE_SETTLE_DELAY_MS...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Host.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 "../HighLevel/USBMode.h"
32
33 #if defined(USB_CAN_BE_HOST)
34
35 #define INCLUDE_FROM_HOST_C
36 #include "Host.h"
37
38 void USB_Host_ProcessNextHostState(void)
39 {
40 uint8_t ErrorCode = HOST_ENUMERROR_NoError;
41 uint8_t SubErrorCode = HOST_ENUMERROR_NoError;
42
43 static uint16_t WaitMSRemaining;
44 static uint8_t PostWaitState;
45
46 switch (USB_HostState)
47 {
48 case HOST_STATE_WaitForDevice:
49 if (WaitMSRemaining)
50 {
51 if ((SubErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
52 {
53 USB_HostState = PostWaitState;
54 ErrorCode = HOST_ENUMERROR_WaitStage;
55 break;
56 }
57
58 if (!(--WaitMSRemaining))
59 USB_HostState = PostWaitState;
60 }
61
62 break;
63 case HOST_STATE_Powered:
64 WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
65
66 USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;
67 break;
68 case HOST_STATE_Powered_WaitForDeviceSettle:
69 #if HOST_DEVICE_SETTLE_DELAY_MS > 0
70 if (!(WaitMSRemaining--))
71 {
72 _delay_ms(1);
73 break;
74 }
75 else
76 #endif
77 {
78 USB_Host_VBUS_Manual_Off();
79
80 USB_OTGPAD_On();
81 USB_Host_VBUS_Auto_Enable();
82 USB_Host_VBUS_Auto_On();
83
84 USB_HostState = HOST_STATE_Powered_WaitForConnect;
85 }
86
87 break;
88 case HOST_STATE_Powered_WaitForConnect:
89 if (USB_INT_HasOccurred(USB_INT_DCONNI))
90 {
91 USB_INT_Clear(USB_INT_DCONNI);
92 USB_INT_Clear(USB_INT_DDISCI);
93
94 USB_INT_Clear(USB_INT_VBERRI);
95 USB_INT_Enable(USB_INT_VBERRI);
96
97 USB_Host_ResumeBus();
98 Pipe_ClearPipes();
99
100 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
101 }
102
103 break;
104 case HOST_STATE_Powered_DoReset:
105 USB_Host_ResetDevice();
106
107 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
108 break;
109 case HOST_STATE_Powered_ConfigPipe:
110 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
111 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
112 PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
113
114 if (!(Pipe_IsConfigured()))
115 {
116 ErrorCode = HOST_ENUMERROR_PipeConfigError;
117 SubErrorCode = 0;
118 break;
119 }
120
121 USB_HostState = HOST_STATE_Default;
122 break;
123 case HOST_STATE_Default:
124 USB_ControlRequest = (USB_Request_Header_t)
125 {
126 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
127 .bRequest = REQ_GetDescriptor,
128 .wValue = (DTYPE_Device << 8),
129 .wIndex = 0,
130 .wLength = 8,
131 };
132
133 uint8_t DataBuffer[8];
134
135 if ((SubErrorCode = USB_Host_SendControlRequest(DataBuffer)) != HOST_SENDCONTROL_Successful)
136 {
137 ErrorCode = HOST_ENUMERROR_ControlError;
138 break;
139 }
140
141 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
142
143 USB_Host_ResetDevice();
144
145 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
146 break;
147 case HOST_STATE_Default_PostReset:
148 Pipe_DisablePipe();
149 Pipe_DeallocateMemory();
150 Pipe_ResetPipe(PIPE_CONTROLPIPE);
151
152 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
153 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
154 USB_ControlPipeSize, PIPE_BANK_SINGLE);
155
156 if (!(Pipe_IsConfigured()))
157 {
158 ErrorCode = HOST_ENUMERROR_PipeConfigError;
159 SubErrorCode = 0;
160 break;
161 }
162
163 USB_ControlRequest = (USB_Request_Header_t)
164 {
165 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
166 .bRequest = REQ_SetAddress,
167 .wValue = USB_HOST_DEVICEADDRESS,
168 .wIndex = 0,
169 .wLength = 0,
170 };
171
172 if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
173 {
174 ErrorCode = HOST_ENUMERROR_ControlError;
175 break;
176 }
177
178 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
179 break;
180 case HOST_STATE_Default_PostAddressSet:
181 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
182
183 EVENT_USB_Host_DeviceEnumerationComplete();
184 USB_HostState = HOST_STATE_Addressed;
185 break;
186 }
187
188 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
189 {
190 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
191
192 USB_Host_VBUS_Auto_Off();
193
194 EVENT_USB_Host_DeviceUnattached();
195
196 USB_ResetInterface();
197 }
198 }
199
200 uint8_t USB_Host_WaitMS(uint8_t MS)
201 {
202 bool BusSuspended = USB_Host_IsBusSuspended();
203 uint8_t ErrorCode = HOST_WAITERROR_Successful;
204
205 USB_Host_ResumeBus();
206
207 while (MS)
208 {
209 if (USB_INT_HasOccurred(USB_INT_HSOFI))
210 {
211 USB_INT_Clear(USB_INT_HSOFI);
212 MS--;
213 }
214
215 if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))
216 {
217 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
218
219 break;
220 }
221
222 if (Pipe_IsError() == true)
223 {
224 Pipe_ClearError();
225 ErrorCode = HOST_WAITERROR_PipeError;
226
227 break;
228 }
229
230 if (Pipe_IsStalled() == true)
231 {
232 Pipe_ClearStall();
233 ErrorCode = HOST_WAITERROR_SetupStalled;
234
235 break;
236 }
237 }
238
239 if (BusSuspended)
240 USB_Host_SuspendBus();
241
242 return ErrorCode;
243 }
244
245 static void USB_Host_ResetDevice(void)
246 {
247 bool BusSuspended = USB_Host_IsBusSuspended();
248
249 USB_INT_Disable(USB_INT_DDISCI);
250
251 USB_Host_ResetBus();
252 while (!(USB_Host_IsBusResetComplete()));
253
254 USB_Host_ResumeBus();
255
256 USB_INT_Clear(USB_INT_HSOFI);
257
258 for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
259 {
260 /* Workaround for powerless-pull-up devices. After a USB bus reset,
261 all disconnection interrupts are suppressed while a USB frame is
262 looked for - if it is found within 10ms, the device is still
263 present. */
264
265 if (USB_INT_HasOccurred(USB_INT_HSOFI))
266 {
267 USB_INT_Clear(USB_INT_HSOFI);
268 USB_INT_Clear(USB_INT_DDISCI);
269 break;
270 }
271
272 _delay_ms(1);
273 }
274
275 if (BusSuspended)
276 USB_Host_SuspendBus();
277
278 USB_INT_Enable(USB_INT_DDISCI);
279 }
280
281 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber)
282 {
283 USB_ControlRequest = (USB_Request_Header_t)
284 {
285 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
286 .bRequest = REQ_SetConfiguration,
287 .wValue = ConfigNumber,
288 .wIndex = 0,
289 .wLength = 0,
290 };
291
292 Pipe_SelectPipe(PIPE_CONTROLPIPE);
293
294 return USB_Host_SendControlRequest(NULL);
295 }
296
297 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr)
298 {
299 USB_ControlRequest = (USB_Request_Header_t)
300 {
301 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
302 bRequest: REQ_GetDescriptor,
303 wValue: (DTYPE_Device << 8),
304 wIndex: 0,
305 wLength: sizeof(USB_Descriptor_Device_t),
306 };
307
308 Pipe_SelectPipe(PIPE_CONTROLPIPE);
309
310 return USB_Host_SendControlRequest(DeviceDescriptorPtr);
311 }
312
313 uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum)
314 {
315 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
316 EndpointNum |= ENDPOINT_DESCRIPTOR_DIR_IN;
317
318 USB_ControlRequest = (USB_Request_Header_t)
319 {
320 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
321 .bRequest = REQ_ClearFeature,
322 .wValue = FEATURE_ENDPOINT_HALT,
323 .wIndex = EndpointNum,
324 .wLength = 0,
325 };
326
327 Pipe_SelectPipe(PIPE_CONTROLPIPE);
328
329 return USB_Host_SendControlRequest(NULL);
330 }
331
332 #endif