Add more missing function attributes to the TWI peripheral driver.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / HostStandardReq.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 Pipe_Write_8(USB_ControlRequest.bmRequestType);
59 Pipe_Write_8(USB_ControlRequest.bRequest);
60 Pipe_Write_16_LE(USB_ControlRequest.wValue);
61 Pipe_Write_16_LE(USB_ControlRequest.wIndex);
62 Pipe_Write_16_LE(USB_ControlRequest.wLength);
63
64 Pipe_ClearSETUP();
65
66 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
67 goto End_Of_Control_Send;
68
69 Pipe_Freeze();
70
71 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
72 goto End_Of_Control_Send;
73
74 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
75 {
76 Pipe_SetPipeToken(PIPE_TOKEN_IN);
77
78 if (DataStream != NULL)
79 {
80 while (DataLen)
81 {
82 Pipe_Unfreeze();
83
84 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
85 goto End_Of_Control_Send;
86
87 if (!(Pipe_BytesInPipe()))
88 DataLen = 0;
89
90 while (Pipe_BytesInPipe() && DataLen)
91 {
92 *(DataStream++) = Pipe_Read_8();
93 DataLen--;
94 }
95
96 Pipe_Freeze();
97 Pipe_ClearIN();
98 }
99 }
100
101 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
102 Pipe_Unfreeze();
103
104 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
105 goto End_Of_Control_Send;
106
107 Pipe_ClearOUT();
108
109 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
110 goto End_Of_Control_Send;
111 }
112 else
113 {
114 if (DataStream != NULL)
115 {
116 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
117 Pipe_Unfreeze();
118
119 while (DataLen)
120 {
121 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
122 goto End_Of_Control_Send;
123
124 while (DataLen && (Pipe_BytesInPipe() < USB_Host_ControlPipeSize))
125 {
126 Pipe_Write_8(*(DataStream++));
127 DataLen--;
128 }
129
130 Pipe_ClearOUT();
131 }
132
133 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
134 goto End_Of_Control_Send;
135
136 Pipe_Freeze();
137 }
138
139 Pipe_SetPipeToken(PIPE_TOKEN_IN);
140 Pipe_Unfreeze();
141
142 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
143 goto End_Of_Control_Send;
144
145 Pipe_ClearIN();
146 }
147
148 End_Of_Control_Send:
149 Pipe_Freeze();
150
151 if (BusSuspended)
152 USB_Host_SuspendBus();
153
154 Pipe_ResetPipe(PIPE_CONTROLPIPE);
155
156 return ReturnStatus;
157 }
158
159 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
160 {
161 #if (USB_HOST_TIMEOUT_MS < 0xFF)
162 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
163 #else
164 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
165 #endif
166
167 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
168 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
169 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
170 {
171 uint8_t ErrorCode;
172
173 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
174 return ErrorCode;
175
176 if (!(TimeoutCounter--))
177 return HOST_SENDCONTROL_SoftwareTimeOut;
178 }
179
180 return HOST_SENDCONTROL_Successful;
181 }
182
183 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber)
184 {
185 uint8_t ErrorCode;
186
187 USB_ControlRequest = (USB_Request_Header_t)
188 {
189 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
190 .bRequest = REQ_SetConfiguration,
191 .wValue = ConfigNumber,
192 .wIndex = 0,
193 .wLength = 0,
194 };
195
196 Pipe_SelectPipe(PIPE_CONTROLPIPE);
197
198 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) == HOST_SENDCONTROL_Successful)
199 {
200 USB_Host_ConfigurationNumber = ConfigNumber;
201 USB_HostState = (ConfigNumber) ? HOST_STATE_Configured : HOST_STATE_Addressed;
202 }
203
204 return ErrorCode;
205 }
206
207 uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr)
208 {
209 USB_ControlRequest = (USB_Request_Header_t)
210 {
211 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
212 .bRequest = REQ_GetDescriptor,
213 .wValue = (DTYPE_Device << 8),
214 .wIndex = 0,
215 .wLength = sizeof(USB_Descriptor_Device_t),
216 };
217
218 Pipe_SelectPipe(PIPE_CONTROLPIPE);
219
220 return USB_Host_SendControlRequest(DeviceDescriptorPtr);
221 }
222
223 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
224 void* const Buffer,
225 const uint8_t BufferLength)
226 {
227 USB_ControlRequest = (USB_Request_Header_t)
228 {
229 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
230 .bRequest = REQ_GetDescriptor,
231 .wValue = (DTYPE_String << 8) | Index,
232 .wIndex = 0,
233 .wLength = BufferLength,
234 };
235
236 Pipe_SelectPipe(PIPE_CONTROLPIPE);
237
238 return USB_Host_SendControlRequest(Buffer);
239 }
240
241 uint8_t USB_Host_GetDeviceStatus(uint8_t* const FeatureStatus)
242 {
243 USB_ControlRequest = (USB_Request_Header_t)
244 {
245 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
246 .bRequest = REQ_GetStatus,
247 .wValue = 0,
248 .wIndex = 0,
249 .wLength = 0,
250 };
251
252 Pipe_SelectPipe(PIPE_CONTROLPIPE);
253
254 return USB_Host_SendControlRequest(FeatureStatus);
255 }
256
257 uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointAddress)
258 {
259 USB_ControlRequest = (USB_Request_Header_t)
260 {
261 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
262 .bRequest = REQ_ClearFeature,
263 .wValue = FEATURE_SEL_EndpointHalt,
264 .wIndex = EndpointAddress,
265 .wLength = 0,
266 };
267
268 Pipe_SelectPipe(PIPE_CONTROLPIPE);
269
270 return USB_Host_SendControlRequest(NULL);
271 }
272
273 uint8_t USB_Host_SetInterfaceAltSetting(const uint8_t InterfaceIndex,
274 const uint8_t AltSetting)
275 {
276 USB_ControlRequest = (USB_Request_Header_t)
277 {
278 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
279 .bRequest = REQ_SetInterface,
280 .wValue = AltSetting,
281 .wIndex = InterfaceIndex,
282 .wLength = 0,
283 };
284
285 Pipe_SelectPipe(PIPE_CONTROLPIPE);
286
287 return USB_Host_SendControlRequest(NULL);
288 }
289
290 #endif
291