Added new Pipe_BoundEndpointNumber() and Pipe_IsEndpointBound() functions.
[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 _delay_ms(1);
71
72 if (!(WaitMSRemaining--))
73 {
74 USB_Host_VBUS_Manual_Off();
75
76 USB_OTGPAD_On();
77 USB_Host_VBUS_Auto_Enable();
78 USB_Host_VBUS_Auto_On();
79
80 USB_HostState = HOST_STATE_Powered_WaitForConnect;
81 }
82 #else
83 USB_HostState = HOST_STATE_Powered_WaitForConnect;
84 #endif
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 EVENT_USB_Connect();
97
98 USB_Host_ResumeBus();
99 Pipe_ClearPipes();
100
101 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
102 }
103
104 break;
105 case HOST_STATE_Powered_DoReset:
106 USB_Host_ResetDevice();
107
108 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
109 break;
110 case HOST_STATE_Powered_ConfigPipe:
111 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
112 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
113 PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
114
115 if (!(Pipe_IsConfigured()))
116 {
117 ErrorCode = HOST_ENUMERROR_PipeConfigError;
118 SubErrorCode = 0;
119 break;
120 }
121
122 USB_HostState = HOST_STATE_Default;
123 break;
124 case HOST_STATE_Default:
125 USB_ControlRequest = (USB_Request_Header_t)
126 {
127 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
128 .bRequest = REQ_GetDescriptor,
129 .wValue = (DTYPE_Device << 8),
130 .wIndex = 0,
131 .wLength = 8,
132 };
133
134 uint8_t DataBuffer[8];
135
136 if ((SubErrorCode = USB_Host_SendControlRequest(DataBuffer)) != HOST_SENDCONTROL_Successful)
137 {
138 ErrorCode = HOST_ENUMERROR_ControlError;
139 break;
140 }
141
142 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
143 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
144 #else
145 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, bMaxPacketSize0)];
146 #endif
147
148 USB_Host_ResetDevice();
149
150 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
151 break;
152 case HOST_STATE_Default_PostReset:
153 Pipe_DisablePipe();
154 Pipe_DeallocateMemory();
155 Pipe_ResetPipe(PIPE_CONTROLPIPE);
156
157 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
158 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
159 USB_ControlPipeSize, PIPE_BANK_SINGLE);
160
161 if (!(Pipe_IsConfigured()))
162 {
163 ErrorCode = HOST_ENUMERROR_PipeConfigError;
164 SubErrorCode = 0;
165 break;
166 }
167
168 USB_ControlRequest = (USB_Request_Header_t)
169 {
170 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
171 .bRequest = REQ_SetAddress,
172 .wValue = USB_HOST_DEVICEADDRESS,
173 .wIndex = 0,
174 .wLength = 0,
175 };
176
177 if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
178 {
179 ErrorCode = HOST_ENUMERROR_ControlError;
180 break;
181 }
182
183 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
184 break;
185 case HOST_STATE_Default_PostAddressSet:
186 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
187
188 EVENT_USB_DeviceEnumerationComplete();
189 USB_HostState = HOST_STATE_Addressed;
190 break;
191 }
192
193 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
194 {
195 EVENT_USB_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
196
197 USB_Host_VBUS_Auto_Off();
198
199 EVENT_USB_DeviceUnattached();
200 EVENT_USB_Disconnect();
201
202 USB_ResetInterface();
203 }
204 }
205
206 uint8_t USB_Host_WaitMS(uint8_t MS)
207 {
208 bool BusSuspended = USB_Host_IsBusSuspended();
209 uint8_t ErrorCode = HOST_WAITERROR_Successful;
210
211 USB_Host_ResumeBus();
212
213 while (MS)
214 {
215 if (USB_INT_HasOccurred(USB_INT_HSOFI))
216 {
217 USB_INT_Clear(USB_INT_HSOFI);
218 MS--;
219 }
220
221 if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))
222 {
223 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
224
225 break;
226 }
227
228 if (Pipe_IsError() == true)
229 {
230 Pipe_ClearError();
231 ErrorCode = HOST_WAITERROR_PipeError;
232
233 break;
234 }
235
236 if (Pipe_IsStalled() == true)
237 {
238 Pipe_ClearStall();
239 ErrorCode = HOST_WAITERROR_SetupStalled;
240
241 break;
242 }
243 }
244
245 if (BusSuspended)
246 USB_Host_SuspendBus();
247
248 return ErrorCode;
249 }
250
251 static void USB_Host_ResetDevice(void)
252 {
253 bool BusSuspended = USB_Host_IsBusSuspended();
254
255 USB_INT_Disable(USB_INT_DDISCI);
256
257 USB_Host_ResetBus();
258 while (!(USB_Host_IsBusResetComplete()));
259
260 USB_Host_ResumeBus();
261
262 USB_INT_Clear(USB_INT_HSOFI);
263
264 for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
265 {
266 /* Workaround for powerless-pull-up devices. After a USB bus reset,
267 all disconnection interrupts are suppressed while a USB frame is
268 looked for - if it is found within 10ms, the device is still
269 present. */
270
271 if (USB_INT_HasOccurred(USB_INT_HSOFI))
272 {
273 USB_INT_Clear(USB_INT_HSOFI);
274 USB_INT_Clear(USB_INT_DDISCI);
275 break;
276 }
277
278 _delay_ms(1);
279 }
280
281 if (BusSuspended)
282 USB_Host_SuspendBus();
283
284 USB_INT_Enable(USB_INT_DDISCI);
285 }
286
287 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber)
288 {
289 USB_ControlRequest = (USB_Request_Header_t)
290 {
291 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
292 .bRequest = REQ_SetConfiguration,
293 .wValue = ConfigNumber,
294 .wIndex = 0,
295 .wLength = 0,
296 };
297
298 Pipe_SelectPipe(PIPE_CONTROLPIPE);
299
300 return USB_Host_SendControlRequest(NULL);
301 }
302
303 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr)
304 {
305 USB_ControlRequest = (USB_Request_Header_t)
306 {
307 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
308 bRequest: REQ_GetDescriptor,
309 wValue: (DTYPE_Device << 8),
310 wIndex: 0,
311 wLength: sizeof(USB_Descriptor_Device_t),
312 };
313
314 Pipe_SelectPipe(PIPE_CONTROLPIPE);
315
316 return USB_Host_SendControlRequest(DeviceDescriptorPtr);
317 }
318
319 uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum)
320 {
321 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
322 EndpointNum |= (1 << 7);
323
324 USB_ControlRequest = (USB_Request_Header_t)
325 {
326 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
327 .bRequest = REQ_ClearFeature,
328 .wValue = FEATURE_ENDPOINT_HALT,
329 .wIndex = EndpointNum,
330 .wLength = 0,
331 };
332
333 Pipe_SelectPipe(PIPE_CONTROLPIPE);
334
335 return USB_Host_SendControlRequest(NULL);
336 }
337
338 #endif