Oops - with new changes to the way the device Configuration Descriptor is retrieved...
[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 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 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
141 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
142 #else
143 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, bMaxPacketSize0)];
144 #endif
145
146 USB_Host_ResetDevice();
147
148 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
149 break;
150 case HOST_STATE_Default_PostReset:
151 Pipe_DisablePipe();
152 Pipe_DeallocateMemory();
153 Pipe_ResetPipe(PIPE_CONTROLPIPE);
154
155 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
156 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
157 USB_ControlPipeSize, PIPE_BANK_SINGLE);
158
159 if (!(Pipe_IsConfigured()))
160 {
161 ErrorCode = HOST_ENUMERROR_PipeConfigError;
162 SubErrorCode = 0;
163 break;
164 }
165
166 USB_ControlRequest = (USB_Request_Header_t)
167 {
168 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
169 .bRequest = REQ_SetAddress,
170 .wValue = USB_HOST_DEVICEADDRESS,
171 .wIndex = 0,
172 .wLength = 0,
173 };
174
175 if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
176 {
177 ErrorCode = HOST_ENUMERROR_ControlError;
178 break;
179 }
180
181 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
182 break;
183 case HOST_STATE_Default_PostAddressSet:
184 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
185
186 EVENT_USB_Host_DeviceEnumerationComplete();
187 USB_HostState = HOST_STATE_Addressed;
188 break;
189 }
190
191 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
192 {
193 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode, SubErrorCode);
194
195 USB_Host_VBUS_Auto_Off();
196
197 EVENT_USB_Host_DeviceUnattached();
198
199 USB_ResetInterface();
200 }
201 }
202
203 uint8_t USB_Host_WaitMS(uint8_t MS)
204 {
205 bool BusSuspended = USB_Host_IsBusSuspended();
206 uint8_t ErrorCode = HOST_WAITERROR_Successful;
207
208 USB_Host_ResumeBus();
209
210 while (MS)
211 {
212 if (USB_INT_HasOccurred(USB_INT_HSOFI))
213 {
214 USB_INT_Clear(USB_INT_HSOFI);
215 MS--;
216 }
217
218 if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))
219 {
220 ErrorCode = HOST_WAITERROR_DeviceDisconnect;
221
222 break;
223 }
224
225 if (Pipe_IsError() == true)
226 {
227 Pipe_ClearError();
228 ErrorCode = HOST_WAITERROR_PipeError;
229
230 break;
231 }
232
233 if (Pipe_IsStalled() == true)
234 {
235 Pipe_ClearStall();
236 ErrorCode = HOST_WAITERROR_SetupStalled;
237
238 break;
239 }
240 }
241
242 if (BusSuspended)
243 USB_Host_SuspendBus();
244
245 return ErrorCode;
246 }
247
248 static void USB_Host_ResetDevice(void)
249 {
250 bool BusSuspended = USB_Host_IsBusSuspended();
251
252 USB_INT_Disable(USB_INT_DDISCI);
253
254 USB_Host_ResetBus();
255 while (!(USB_Host_IsBusResetComplete()));
256
257 USB_Host_ResumeBus();
258
259 USB_INT_Clear(USB_INT_HSOFI);
260
261 for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
262 {
263 /* Workaround for powerless-pull-up devices. After a USB bus reset,
264 all disconnection interrupts are suppressed while a USB frame is
265 looked for - if it is found within 10ms, the device is still
266 present. */
267
268 if (USB_INT_HasOccurred(USB_INT_HSOFI))
269 {
270 USB_INT_Clear(USB_INT_HSOFI);
271 USB_INT_Clear(USB_INT_DDISCI);
272 break;
273 }
274
275 _delay_ms(1);
276 }
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_ClearPipeStall(uint8_t EndpointNum)
317 {
318 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
319 EndpointNum |= (1 << 7);
320
321 USB_ControlRequest = (USB_Request_Header_t)
322 {
323 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
324 .bRequest = REQ_ClearFeature,
325 .wValue = FEATURE_ENDPOINT_HALT,
326 .wIndex = EndpointNum,
327 .wLength = 0,
328 };
329
330 Pipe_SelectPipe(PIPE_CONTROLPIPE);
331
332 return USB_Host_SendControlRequest(NULL);
333 }
334
335 #endif