Update file contributor copyrights for 2012.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / HostStandardReq.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 "USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_HOSTSTDREQ_C
37 #include "HostStandardReq.h"
38
39 uint8_t USB_Host_ConfigurationNumber;
40
41 uint8_t USB_Host_SendControlRequest(void* const BufferPtr)
42 {
43 uint8_t* DataStream = (uint8_t*)BufferPtr;
44 bool BusSuspended = USB_Host_IsBusSuspended();
45 uint8_t ReturnStatus = HOST_SENDCONTROL_Successful;
46 uint16_t DataLen = USB_ControlRequest.wLength;
47
48 USB_Host_ResumeBus();
49
50 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
51 goto End_Of_Control_Send;
52
53 Pipe_SetPipeToken(PIPE_TOKEN_SETUP);
54 Pipe_ClearError();
55
56 Pipe_Unfreeze();
57
58 #if defined(ARCH_BIG_ENDIAN)
59 Pipe_Write_8(USB_ControlRequest.bmRequestType);
60 Pipe_Write_8(USB_ControlRequest.bRequest);
61 Pipe_Write_16_LE(USB_ControlRequest.wValue);
62 Pipe_Write_16_LE(USB_ControlRequest.wIndex);
63 Pipe_Write_16_LE(USB_ControlRequest.wLength);
64 #else
65 uint8_t* HeaderStream = (uint8_t*)&USB_ControlRequest;
66
67 for (uint8_t HeaderByte = 0; HeaderByte < sizeof(USB_Request_Header_t); HeaderByte++)
68 Pipe_Write_8(*(HeaderStream++));
69 #endif
70
71 Pipe_ClearSETUP();
72
73 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
74 goto End_Of_Control_Send;
75
76 Pipe_Freeze();
77
78 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
79 goto End_Of_Control_Send;
80
81 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
82 {
83 Pipe_SetPipeToken(PIPE_TOKEN_IN);
84
85 if (DataStream != NULL)
86 {
87 while (DataLen)
88 {
89 Pipe_Unfreeze();
90
91 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
92 goto End_Of_Control_Send;
93
94 if (!(Pipe_BytesInPipe()))
95 DataLen = 0;
96
97 while (Pipe_BytesInPipe() && DataLen)
98 {
99 *(DataStream++) = Pipe_Read_8();
100 DataLen--;
101 }
102
103 Pipe_Freeze();
104 Pipe_ClearIN();
105 }
106 }
107
108 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
109 Pipe_Unfreeze();
110
111 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
112 goto End_Of_Control_Send;
113
114 Pipe_ClearOUT();
115
116 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
117 goto End_Of_Control_Send;
118 }
119 else
120 {
121 if (DataStream != NULL)
122 {
123 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
124 Pipe_Unfreeze();
125
126 while (DataLen)
127 {
128 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
129 goto End_Of_Control_Send;
130
131 while (DataLen && (Pipe_BytesInPipe() < USB_Host_ControlPipeSize))
132 {
133 Pipe_Write_8(*(DataStream++));
134 DataLen--;
135 }
136
137 Pipe_ClearOUT();
138 }
139
140 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
141 goto End_Of_Control_Send;
142
143 Pipe_Freeze();
144 }
145
146 Pipe_SetPipeToken(PIPE_TOKEN_IN);
147 Pipe_Unfreeze();
148
149 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
150 goto End_Of_Control_Send;
151
152 Pipe_ClearIN();
153 }
154
155 End_Of_Control_Send:
156 Pipe_Freeze();
157
158 if (BusSuspended)
159 USB_Host_SuspendBus();
160
161 Pipe_ResetPipe(PIPE_CONTROLPIPE);
162
163 return ReturnStatus;
164 }
165
166 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
167 {
168 #if (USB_HOST_TIMEOUT_MS < 0xFF)
169 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
170 #else
171 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
172 #endif
173
174 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
175 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
176 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
177 {
178 uint8_t ErrorCode;
179
180 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
181 return ErrorCode;
182
183 if (!(TimeoutCounter--))
184 return HOST_SENDCONTROL_SoftwareTimeOut;
185 }
186
187 return HOST_SENDCONTROL_Successful;
188 }
189
190 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber)
191 {
192 uint8_t ErrorCode;
193
194 USB_ControlRequest = (USB_Request_Header_t)
195 {
196 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
197 .bRequest = REQ_SetConfiguration,
198 .wValue = ConfigNumber,
199 .wIndex = 0,
200 .wLength = 0,
201 };
202
203 Pipe_SelectPipe(PIPE_CONTROLPIPE);
204
205 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) == HOST_SENDCONTROL_Successful)
206 {
207 USB_Host_ConfigurationNumber = ConfigNumber;
208 USB_HostState = (ConfigNumber) ? HOST_STATE_Configured : HOST_STATE_Addressed;
209 }
210
211 return ErrorCode;
212 }
213
214 uint8_t USB_Host_GetDeviceConfiguration(uint8_t* const ConfigNumber)
215 {
216 USB_ControlRequest = (USB_Request_Header_t)
217 {
218 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
219 .bRequest = REQ_GetConfiguration,
220 .wValue = 0,
221 .wIndex = 0,
222 .wLength = sizeof(uint8_t),
223 };
224
225 Pipe_SelectPipe(PIPE_CONTROLPIPE);
226
227 return USB_Host_SendControlRequest(ConfigNumber);
228 }
229
230 uint8_t USB_Host_GetDescriptor(const uint8_t Type,
231 const uint8_t Index,
232 void* const Buffer,
233 const uint8_t BufferLength)
234 {
235 USB_ControlRequest = (USB_Request_Header_t)
236 {
237 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
238 .bRequest = REQ_GetDescriptor,
239 .wValue = (((uint16_t)Type << 8) | Index),
240 .wIndex = 0,
241 .wLength = BufferLength,
242 };
243
244 Pipe_SelectPipe(PIPE_CONTROLPIPE);
245
246 return USB_Host_SendControlRequest(Buffer);
247 }
248
249 uint8_t USB_Host_GetDeviceStatus(uint8_t* const FeatureStatus)
250 {
251 USB_ControlRequest = (USB_Request_Header_t)
252 {
253 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
254 .bRequest = REQ_GetStatus,
255 .wValue = 0,
256 .wIndex = 0,
257 .wLength = 0,
258 };
259
260 Pipe_SelectPipe(PIPE_CONTROLPIPE);
261
262 return USB_Host_SendControlRequest(FeatureStatus);
263 }
264
265 uint8_t USB_Host_ClearEndpointStall(const uint8_t EndpointAddress)
266 {
267 USB_ControlRequest = (USB_Request_Header_t)
268 {
269 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
270 .bRequest = REQ_ClearFeature,
271 .wValue = FEATURE_SEL_EndpointHalt,
272 .wIndex = EndpointAddress,
273 .wLength = 0,
274 };
275
276 Pipe_SelectPipe(PIPE_CONTROLPIPE);
277
278 return USB_Host_SendControlRequest(NULL);
279 }
280
281 uint8_t USB_Host_SetInterfaceAltSetting(const uint8_t InterfaceIndex,
282 const uint8_t AltSetting)
283 {
284 USB_ControlRequest = (USB_Request_Header_t)
285 {
286 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
287 .bRequest = REQ_SetInterface,
288 .wValue = AltSetting,
289 .wIndex = InterfaceIndex,
290 .wLength = 0,
291 };
292
293 Pipe_SelectPipe(PIPE_CONTROLPIPE);
294
295 return USB_Host_SendControlRequest(NULL);
296 }
297
298 uint8_t USB_Host_GetInterfaceAltSetting(const uint8_t InterfaceIndex,
299 uint8_t* const AltSetting)
300 {
301 USB_ControlRequest = (USB_Request_Header_t)
302 {
303 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
304 .bRequest = REQ_GetInterface,
305 .wValue = 0,
306 .wIndex = InterfaceIndex,
307 .wLength = sizeof(uint8_t),
308 };
309
310 Pipe_SelectPipe(PIPE_CONTROLPIPE);
311
312 return USB_Host_SendControlRequest(AltSetting);
313 }
314
315 #endif
316