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