Changed PIPE_CONTROLPIPE_DEFAULT_SIZE from 8 to 64 to try to prevent problems with...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / USBTask.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 "USBMode.h"
32
33 #define INCLUDE_FROM_USBTASK_C
34 #include "USBTask.h"
35
36 volatile bool USB_IsSuspended;
37 volatile bool USB_IsConnected;
38 volatile bool USB_IsInitialized;
39 USB_Request_Header_t USB_ControlRequest;
40
41 #if defined(USB_CAN_BE_HOST)
42 volatile uint8_t USB_HostState;
43 #endif
44
45 TASK(USB_USBTask)
46 {
47 #if defined(USB_HOST_ONLY)
48 USB_HostTask();
49 #elif defined(USB_DEVICE_ONLY)
50 USB_DeviceTask();
51 #else
52 if (USB_CurrentMode == USB_MODE_DEVICE)
53 USB_DeviceTask();
54 else if (USB_CurrentMode == USB_MODE_HOST)
55 USB_HostTask();
56 #endif
57 }
58
59 #if defined(USB_CAN_BE_DEVICE)
60 static void USB_DeviceTask(void)
61 {
62 if (USB_IsConnected)
63 {
64 uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();
65
66 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
67
68 if (Endpoint_IsSETUPReceived())
69 {
70 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
71 {
72 USB_Device_ProcessControlPacket();
73 }
74 }
75
76 Endpoint_SelectEndpoint(PrevEndpoint);
77 }
78 }
79 #endif
80
81 #if defined(USB_CAN_BE_HOST)
82 static void USB_HostTask(void)
83 {
84 uint8_t ErrorCode = HOST_ENUMERROR_NoError;
85 uint8_t SubErrorCode = HOST_ENUMERROR_NoError;
86
87 static uint16_t WaitMSRemaining;
88 static uint8_t PostWaitState;
89
90 uint8_t PrevPipe = Pipe_GetCurrentPipe();
91
92 Pipe_SelectPipe(PIPE_CONTROLPIPE);
93
94 switch (USB_HostState)
95 {
96 case HOST_STATE_WaitForDevice:
97 if (WaitMSRemaining)
98 {
99 if ((SubErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
100 {
101 USB_HostState = PostWaitState;
102 ErrorCode = HOST_ENUMERROR_WaitStage;
103 break;
104 }
105
106 WaitMSRemaining--;
107 }
108 else
109 {
110 USB_HostState = PostWaitState;
111 }
112
113 break;
114 case HOST_STATE_Attached:
115 WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
116
117 USB_HostState = HOST_STATE_Attached_WaitForDeviceSettle;
118 break;
119 case HOST_STATE_Attached_WaitForDeviceSettle:
120 _delay_ms(1);
121
122 if (!(WaitMSRemaining--))
123 {
124 USB_Host_VBUS_Manual_Off();
125
126 USB_OTGPAD_On();
127 USB_Host_VBUS_Auto_Enable();
128 USB_Host_VBUS_Auto_On();
129
130 USB_HostState = HOST_STATE_Attached_WaitForConnect;
131 }
132
133 break;
134 case HOST_STATE_Attached_WaitForConnect:
135 if (USB_INT_HasOccurred(USB_INT_DCONNI))
136 {
137 USB_INT_Clear(USB_INT_DCONNI);
138 USB_INT_Clear(USB_INT_DDISCI);
139
140 USB_INT_Clear(USB_INT_VBERRI);
141 USB_INT_Enable(USB_INT_VBERRI);
142
143 USB_IsConnected = true;
144 RAISE_EVENT(USB_Connect);
145
146 USB_Host_ResumeBus();
147 Pipe_ClearPipes();
148
149 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Attached_DoReset);
150 }
151
152 break;
153 case HOST_STATE_Attached_DoReset:
154 USB_Host_ResetDevice();
155
156 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered);
157 break;
158 case HOST_STATE_Powered:
159 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
160 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
161 PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
162
163 if (!(Pipe_IsConfigured()))
164 {
165 ErrorCode = HOST_ENUMERROR_PipeConfigError;
166 SubErrorCode = 0;
167 break;
168 }
169
170 USB_HostState = HOST_STATE_Default;
171 break;
172 case HOST_STATE_Default:
173 USB_ControlRequest = (USB_Request_Header_t)
174 {
175 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
176 .bRequest = REQ_GetDescriptor,
177 .wValue = (DTYPE_Device << 8),
178 .wIndex = 0,
179 .wLength = PIPE_CONTROLPIPE_DEFAULT_SIZE,
180 };
181
182 uint8_t DataBuffer[PIPE_CONTROLPIPE_DEFAULT_SIZE];
183
184 if ((SubErrorCode = USB_Host_SendControlRequest(DataBuffer)) != HOST_SENDCONTROL_Successful)
185 {
186 ErrorCode = HOST_ENUMERROR_ControlError;
187 break;
188 }
189
190 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
191 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
192 #else
193 USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, bMaxPacketSize0)];
194 #endif
195
196 USB_Host_ResetDevice();
197
198 HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
199 break;
200 case HOST_STATE_Default_PostReset:
201 Pipe_DisablePipe();
202 Pipe_DeallocateMemory();
203 Pipe_ResetPipe(PIPE_CONTROLPIPE);
204
205 Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
206 PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
207 USB_ControlPipeSize, PIPE_BANK_SINGLE);
208
209 if (!(Pipe_IsConfigured()))
210 {
211 ErrorCode = HOST_ENUMERROR_PipeConfigError;
212 SubErrorCode = 0;
213 break;
214 }
215
216 Pipe_SetInfiniteINRequests();
217
218 USB_ControlRequest = (USB_Request_Header_t)
219 {
220 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
221 .bRequest = REQ_SetAddress,
222 .wValue = USB_HOST_DEVICEADDRESS,
223 .wIndex = 0,
224 .wLength = 0,
225 };
226
227 if ((SubErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
228 {
229 ErrorCode = HOST_ENUMERROR_ControlError;
230 break;
231 }
232
233 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet);
234 break;
235 case HOST_STATE_Default_PostAddressSet:
236 USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS);
237
238 RAISE_EVENT(USB_DeviceEnumerationComplete);
239 USB_HostState = HOST_STATE_Addressed;
240
241 break;
242 }
243
244 if ((ErrorCode != HOST_ENUMERROR_NoError) && (USB_HostState != HOST_STATE_Unattached))
245 {
246 RAISE_EVENT(USB_DeviceEnumerationFailed, ErrorCode, SubErrorCode);
247
248 USB_Host_VBUS_Auto_Off();
249
250 RAISE_EVENT(USB_DeviceUnattached);
251
252 if (USB_IsConnected)
253 RAISE_EVENT(USB_Disconnect);
254
255 USB_ResetInterface();
256 }
257
258 Pipe_SelectPipe(PrevPipe);
259 }
260 #endif