Add new tag for the LUFA-120219-BETA release.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Host_AVR8.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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_HOST_C
37 #include "../Host.h"
38
39 void USB_Host_ProcessNextHostState(void)
40 {
41 uint8_t ErrorCode = HOST_ENUMERROR_NoError;
42 uint8_t SubErrorCode = HOST_ENUMERROR_NoError;
43
44 static uint16_t WaitMSRemaining;
45 static uint8_t PostWaitState;
46
47 switch (USB_HostState)
48 {
49 case HOST_STATE_WaitForDevice:
50 if (WaitMSRemaining)
51 {
52 if ((SubErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
53 {
54 USB_HostState = PostWaitState;
55 ErrorCode = HOST_ENUMERROR_WaitStage;
56 break;
57 }
58
59 if (!(--WaitMSRemaining))
60 USB_HostState = PostWaitState;
61 }
62
63 break;
64 case HOST_STATE_Powered:
65 WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
66
67 USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;
68 break;
69 case HOST_STATE_Powered_WaitForDeviceSettle:
70 if (WaitMSRemaining--)
71 {
72 Delay_MS(1);
73 break;
74 }
75 else
76 {
77 USB_Host_VBUS_Manual_Off();
78
79 USB_OTGPAD_On();
80 USB_Host_VBUS_Auto_Enable();
81 USB_Host_VBUS_Auto_On();
82
83 #if defined(NO_AUTO_VBUS_MANAGEMENT)
84 USB_Host_VBUS_Manual_Enable();
85 USB_Host_VBUS_Manual_On();
86 #endif
87
88 USB_HostState = HOST_STATE_Powered_WaitForConnect;
89 }
90
91 break;
92 case HOST_STATE_Powered_WaitForConnect:
93 if (USB_INT_HasOccurred(USB_INT_DCONNI))
94 {
95 USB_INT_Clear(USB_INT_DCONNI);
96 USB_INT_Clear(USB_INT_DDISCI);
97
98 USB_INT_Clear(USB_INT_VBERRI);
99 USB_INT_Enable(USB_INT_VBERRI);
100
101 USB_Host_ResumeBus();
102 Pipe_ClearPipes();
103
104 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
105 }
106
107 break;
108 case HOST_STATE_Powered_DoReset:
109 USB_Host_ResetDevice();
110
111 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
112 break;
113 case HOST_STATE_Powered_ConfigPipe:
114 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
115 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
116 PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
117
118 if (!(Pipe_IsConfigured()))
119 {
120 ErrorCode = HOST_ENUMERROR_PipeConfigError;
121 SubErrorCode = 0;
122 break;
123 }
124
125 USB_HostState = HOST_STATE_Default;
126 break;
127 case HOST_STATE_Default:
128 USB_ControlRequest = (USB_Request_Header_t)
129 {
130 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
131 .bRequest = REQ_GetDescriptor,
132 .wValue = (DTYPE_Device << 8),
133 .wIndex = 0,
134 .wLength = 8,
135 };
136
137 uint8_t DataBuffer[8];
138
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 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
153 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
154 USB_Host_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 USB_HostState = HOST_STATE_Addressed;
184
185 EVENT_USB_Host_DeviceEnumerationComplete();
186 break;
187 }
188
189 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
190 {
191 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
192
193 USB_Host_VBUS_Auto_Off();
194
195 EVENT_USB_Host_DeviceUnattached();
196
197 USB_ResetInterface();
198 }
199 }
200
201 uint8_t USB_Host_WaitMS(uint8_t MS)
202 {
203 bool BusSuspended = USB_Host_IsBusSuspended();
204 uint8_t ErrorCode = HOST_WAITERROR_Successful;
205 bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
206
207 USB_INT_Disable(USB_INT_HSOFI);
208 USB_INT_Clear(USB_INT_HSOFI);
209
210 USB_Host_ResumeBus();
211
212 while (MS)
213 {
214 if (USB_INT_HasOccurred(USB_INT_HSOFI))
215 {
216 USB_INT_Clear(USB_INT_HSOFI);
217 MS--;
218 }
219
220 if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode != USB_MODE_Host))
221 {
222 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
223
224 break;
225 }
226
227 if (Pipe_IsError())
228 {
229 Pipe_ClearError();
230 ErrorCode = HOST_WAITERROR_PipeError;
231
232 break;
233 }
234
235 if (Pipe_IsStalled())
236 {
237 Pipe_ClearStall();
238 ErrorCode = HOST_WAITERROR_SetupStalled;
239
240 break;
241 }
242 }
243
244 if (BusSuspended)
245 USB_Host_SuspendBus();
246
247 if (HSOFIEnabled)
248 USB_INT_Enable(USB_INT_HSOFI);
249
250 return ErrorCode;
251 }
252
253 static void USB_Host_ResetDevice(void)
254 {
255 bool BusSuspended = USB_Host_IsBusSuspended();
256
257 USB_INT_Disable(USB_INT_DDISCI);
258
259 USB_Host_ResetBus();
260 while (!(USB_Host_IsBusResetComplete()));
261 USB_Host_ResumeBus();
262
263 USB_Host_ConfigurationNumber = 0;
264
265 bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
266
267 USB_INT_Disable(USB_INT_HSOFI);
268 USB_INT_Clear(USB_INT_HSOFI);
269
270 for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
271 {
272 /* Workaround for powerless-pull-up devices. After a USB bus reset,
273 all disconnection interrupts are suppressed while a USB frame is
274 looked for - if it is found within 10ms, the device is still
275 present. */
276
277 if (USB_INT_HasOccurred(USB_INT_HSOFI))
278 {
279 USB_INT_Clear(USB_INT_HSOFI);
280 USB_INT_Clear(USB_INT_DDISCI);
281 break;
282 }
283
284 Delay_MS(1);
285 }
286
287 if (HSOFIEnabled)
288 USB_INT_Enable(USB_INT_HSOFI);
289
290 if (BusSuspended)
291 USB_Host_SuspendBus();
292
293 USB_INT_Enable(USB_INT_DDISCI);
294 }
295
296 #endif
297