2b66e28b4b7cf783d98622a78aba5bff862907a7
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Host.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 "../HighLevel/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 USB_HostState = HOST_STATE_Powered_WaitForConnect;
84 }
85
86 break;
87 case HOST_STATE_Powered_WaitForConnect:
88 if (USB_INT_HasOccurred(USB_INT_DCONNI))
89 {
90 USB_INT_Clear(USB_INT_DCONNI);
91 USB_INT_Clear(USB_INT_DDISCI);
92
93 USB_INT_Clear(USB_INT_VBERRI);
94 USB_INT_Enable(USB_INT_VBERRI);
95
96 USB_Host_ResumeBus();
97 Pipe_ClearPipes();
98
99 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
100 }
101
102 break;
103 case HOST_STATE_Powered_DoReset:
104 USB_Host_ResetDevice();
105
106 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
107 break;
108 case HOST_STATE_Powered_ConfigPipe:
109 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
110 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
111 PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
112
113 if (!(Pipe_IsConfigured()))
114 {
115 ErrorCode = HOST_ENUMERROR_PipeConfigError;
116 SubErrorCode = 0;
117 break;
118 }
119
120 USB_HostState = HOST_STATE_Default;
121 break;
122 case HOST_STATE_Default:
123 USB_ControlRequest = (USB_Request_Header_t)
124 {
125 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
126 .bRequest = REQ_GetDescriptor,
127 .wValue = (DTYPE_Device << 8),
128 .wIndex = 0,
129 .wLength = 8,
130 };
131
132 uint8_t DataBuffer[8];
133
134 if ((SubErrorCode = USB_Host_SendControlRequest(DataBuffer)) != HOST_SENDCONTROL_Successful)
135 {
136 ErrorCode = HOST_ENUMERROR_ControlError;
137 break;
138 }
139
140 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
141
142 USB_Host_ResetDevice();
143
144 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
145 break;
146 case HOST_STATE_Default_PostReset:
147 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
148 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
149 USB_ControlPipeSize, PIPE_BANK_SINGLE);
150
151 if (!(Pipe_IsConfigured()))
152 {
153 ErrorCode = HOST_ENUMERROR_PipeConfigError;
154 SubErrorCode = 0;
155 break;
156 }
157
158 USB_ControlRequest = (USB_Request_Header_t)
159 {
160 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
161 .bRequest = REQ_SetAddress,
162 .wValue = USB_HOST_DEVICEADDRESS,
163 .wIndex = 0,
164 .wLength = 0,
165 };
166
167 if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
168 {
169 ErrorCode = HOST_ENUMERROR_ControlError;
170 break;
171 }
172
173 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
174 break;
175 case HOST_STATE_Default_PostAddressSet:
176 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
177
178 EVENT_USB_Host_DeviceEnumerationComplete();
179 USB_HostState = HOST_STATE_Addressed;
180 break;
181 }
182
183 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
184 {
185 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
186
187 USB_Host_VBUS_Auto_Off();
188
189 EVENT_USB_Host_DeviceUnattached();
190
191 USB_ResetInterface();
192 }
193 }
194
195 uint8_t USB_Host_WaitMS(uint8_t MS)
196 {
197 bool BusSuspended = USB_Host_IsBusSuspended();
198 uint8_t ErrorCode = HOST_WAITERROR_Successful;
199 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
200
201 USB_Host_ResumeBus();
202
203 while (MS)
204 {
205 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
206
207 if (CurrentFrameNumber != PreviousFrameNumber)
208 {
209 PreviousFrameNumber = CurrentFrameNumber;
210 MS--;
211 }
212
213 if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))
214 {
215 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
216
217 break;
218 }
219
220 if (Pipe_IsError() == true)
221 {
222 Pipe_ClearError();
223 ErrorCode = HOST_WAITERROR_PipeError;
224
225 break;
226 }
227
228 if (Pipe_IsStalled() == true)
229 {
230 Pipe_ClearStall();
231 ErrorCode = HOST_WAITERROR_SetupStalled;
232
233 break;
234 }
235 }
236
237 if (BusSuspended)
238 USB_Host_SuspendBus();
239
240 return ErrorCode;
241 }
242
243 static void USB_Host_ResetDevice(void)
244 {
245 bool BusSuspended = USB_Host_IsBusSuspended();
246
247 USB_INT_Disable(USB_INT_DDISCI);
248
249 USB_Host_ResetBus();
250 while (!(USB_Host_IsBusResetComplete()));
251 USB_Host_ResumeBus();
252
253 bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
254
255 USB_INT_Disable(USB_INT_HSOFI);
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 (HSOFIEnabled)
276 USB_INT_Enable(USB_INT_HSOFI);
277
278 if (BusSuspended)
279 USB_Host_SuspendBus();
280
281 USB_INT_Enable(USB_INT_DDISCI);
282 }
283
284 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber)
285 {
286 USB_ControlRequest = (USB_Request_Header_t)
287 {
288 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
289 .bRequest = REQ_SetConfiguration,
290 .wValue = ConfigNumber,
291 .wIndex = 0,
292 .wLength = 0,
293 };
294
295 Pipe_SelectPipe(PIPE_CONTROLPIPE);
296
297 return USB_Host_SendControlRequest(NULL);
298 }
299
300 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr)
301 {
302 USB_ControlRequest = (USB_Request_Header_t)
303 {
304 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
305 .bRequest = REQ_GetDescriptor,
306 .wValue = (DTYPE_Device << 8),
307 .wIndex = 0,
308 .wLength = sizeof(USB_Descriptor_Device_t),
309 };
310
311 Pipe_SelectPipe(PIPE_CONTROLPIPE);
312
313 return USB_Host_SendControlRequest(DeviceDescriptorPtr);
314 }
315
316 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
317 void* const Buffer,
318 const uint8_t BufferLength)
319 {
320 USB_ControlRequest = (USB_Request_Header_t)
321 {
322 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
323 .bRequest = REQ_GetDescriptor,
324 .wValue = (DTYPE_String << 8) | Index,
325 .wIndex = 0,
326 .wLength = BufferLength,
327 };
328
329 Pipe_SelectPipe(PIPE_CONTROLPIPE);
330
331 return USB_Host_SendControlRequest(Buffer);
332 }
333
334 uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum)
335 {
336 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
337 EndpointNum |= ENDPOINT_DESCRIPTOR_DIR_IN;
338
339 USB_ControlRequest = (USB_Request_Header_t)
340 {
341 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
342 .bRequest = REQ_ClearFeature,
343 .wValue = FEATURE_ENDPOINT_HALT,
344 .wIndex = EndpointNum,
345 .wLength = 0,
346 };
347
348 Pipe_SelectPipe(PIPE_CONTROLPIPE);
349
350 return USB_Host_SendControlRequest(NULL);
351 }
352
353 #endif