2 * @brief USB host standard request management
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * Copyright(C) Dean Camera, 2011, 2012
\r
7 * All rights reserved.
\r
10 * Software that is described herein is for illustrative purposes only
\r
11 * which provides customers with programming information regarding the
\r
12 * LPC products. This software is supplied "AS IS" without any warranties of
\r
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
14 * all warranties, express or implied, including all implied warranties of
\r
15 * merchantability, fitness for a particular purpose and non-infringement of
\r
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
17 * or liability for the use of the software, conveys no license or rights under any
\r
18 * patent, copyright, mask work right, or any other intellectual property rights in
\r
19 * or to any products. NXP Semiconductors reserves the right to make changes
\r
20 * in the software without notification. NXP Semiconductors also makes no
\r
21 * representation or warranty that such application will be suitable for the
\r
22 * specified use without further testing or modification.
\r
25 * Permission to use, copy, modify, and distribute this software and its
\r
26 * documentation is hereby granted, under NXP Semiconductors' and its
\r
27 * licensor's relevant copyrights in the software, without fee, provided that it
\r
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
29 * copyright, permission, and disclaimer notice must appear in all copies of
\r
33 #define __INCLUDE_FROM_USB_DRIVER
\r
34 #include "USBMode.h"
\r
36 #if defined(USB_CAN_BE_HOST)
\r
38 #define __INCLUDE_FROM_HOSTSTDREQ_C
\r
39 #include "HostStandardReq.h"
\r
41 uint8_t USB_Host_ConfigurationNumber;
\r
45 uint8_t USB_Host_SendControlRequest(const uint8_t corenum, void* const BufferPtr)
\r
47 uint8_t* DataStream = (uint8_t*)BufferPtr;
\r
48 uint16_t DataLen = USB_ControlRequest.wLength;
\r
51 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_HOSTTODEVICE)
\r
53 Pipe_Write_Stream_LE(corenum, BufferPtr, DataLen, NULL);
\r
56 ret = (uint8_t)HcdControlTransfer(PipeInfo[corenum][pipeselected[corenum]].PipeHandle, &USB_ControlRequest,
\r
57 PipeInfo[corenum][pipeselected[corenum]].Buffer);
\r
59 if(ret == (uint8_t)HOST_SENDCONTROL_Successful)
\r
61 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
\r
63 PipeInfo[corenum][pipeselected[corenum]].ByteTransfered = USB_ControlRequest.wLength;
\r
66 *(DataStream++) = Pipe_Read_8(corenum);
\r
69 /* Pipe_Read_Stream_LE(BufferPtr, DataLen, NULL); cannot use read stream as it call HcdDataTransfer*/
\r
71 PipeInfo[corenum][pipeselected[corenum]].StartIdx = PipeInfo[corenum][pipeselected[corenum]].ByteTransfered = 0; /* Clear Control Pipe */
\r
72 return HOST_SENDCONTROL_Successful;
\r
76 return HOST_SENDCONTROL_PipeError;
\r
80 #else // The following code is deprecated
\r
81 uint8_t USB_Host_SendControlRequest(void* const BufferPtr)
\r
83 uint8_t* DataStream = (uint8_t*)BufferPtr;
\r
84 bool BusSuspended = USB_Host_IsBusSuspended();
\r
85 uint8_t ReturnStatus = HOST_SENDCONTROL_Successful;
\r
86 uint16_t DataLen = USB_ControlRequest.wLength;
\r
88 USB_Host_ResumeBus();
\r
90 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
\r
91 goto End_Of_Control_Send;
\r
93 Pipe_SetPipeToken(PIPE_TOKEN_SETUP);
\r
98 Pipe_Write_8(USB_ControlRequest.bmRequestType);
\r
99 Pipe_Write_8(USB_ControlRequest.bRequest);
\r
100 Pipe_Write_16_LE(USB_ControlRequest.wValue);
\r
101 Pipe_Write_16_LE(USB_ControlRequest.wIndex);
\r
102 Pipe_Write_16_LE(USB_ControlRequest.wLength);
\r
106 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
\r
107 goto End_Of_Control_Send;
\r
111 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
\r
112 goto End_Of_Control_Send;
\r
114 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
\r
116 Pipe_SetPipeToken(PIPE_TOKEN_IN);
\r
118 if (DataStream != NULL)
\r
124 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
\r
125 goto End_Of_Control_Send;
\r
127 if (!(Pipe_BytesInPipe()))
\r
130 while (Pipe_BytesInPipe() && DataLen)
\r
132 *(DataStream++) = Pipe_Read_8();
\r
141 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
\r
144 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
\r
145 goto End_Of_Control_Send;
\r
149 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
\r
150 goto End_Of_Control_Send;
\r
154 if (DataStream != NULL)
\r
156 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
\r
161 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
\r
162 goto End_Of_Control_Send;
\r
164 while (DataLen && (Pipe_BytesInPipe() < USB_Host_ControlPipeSize))
\r
166 Pipe_Write_8(*(DataStream++));
\r
173 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
\r
174 goto End_Of_Control_Send;
\r
179 Pipe_SetPipeToken(PIPE_TOKEN_IN);
\r
182 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
\r
183 goto End_Of_Control_Send;
\r
188 End_Of_Control_Send:
\r
192 USB_Host_SuspendBus();
\r
194 Pipe_ResetPipe(PIPE_CONTROLPIPE);
\r
196 return ReturnStatus;
\r
199 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
\r
201 #if (USB_HOST_TIMEOUT_MS < 0xFF)
\r
202 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
\r
204 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
\r
207 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
\r
208 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
\r
209 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
\r
213 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
\r
216 if (!(TimeoutCounter--))
\r
217 return HOST_SENDCONTROL_SoftwareTimeOut;
\r
220 return HOST_SENDCONTROL_Successful;
\r
224 uint8_t USB_Host_SetDeviceConfiguration(const uint8_t corenum, const uint8_t ConfigNumber)
\r
228 USB_ControlRequest = (USB_Request_Header_t)
\r
230 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
\r
231 .bRequest = REQ_SetConfiguration,
\r
232 .wValue = ConfigNumber,
\r
237 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
\r
239 if ((ErrorCode = USB_Host_SendControlRequest(corenum, NULL)) == HOST_SENDCONTROL_Successful)
\r
241 USB_Host_ConfigurationNumber = ConfigNumber;
\r
242 USB_HostState[corenum] = (ConfigNumber) ? HOST_STATE_Configured : HOST_STATE_Addressed;
\r
248 uint8_t USB_Host_GetDeviceDescriptor(const uint8_t corenum, void* const DeviceDescriptorPtr)
\r
250 USB_ControlRequest = (USB_Request_Header_t)
\r
252 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
\r
253 .bRequest = REQ_GetDescriptor,
\r
254 .wValue = (DTYPE_Device << 8),
\r
256 .wLength = sizeof(USB_Descriptor_Device_t),
\r
259 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
\r
261 return USB_Host_SendControlRequest(corenum,DeviceDescriptorPtr);
\r
264 uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t corenum,
\r
265 const uint8_t Index,
\r
266 void* const Buffer,
\r
267 const uint8_t BufferLength)
\r
269 USB_ControlRequest = (USB_Request_Header_t)
\r
271 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
\r
272 .bRequest = REQ_GetDescriptor,
\r
273 .wValue = (DTYPE_String << 8) | Index,
\r
275 .wLength = BufferLength,
\r
278 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
\r
280 return USB_Host_SendControlRequest(corenum,Buffer);
\r
283 uint8_t USB_Host_GetDeviceStatus(const uint8_t corenum, uint8_t* const FeatureStatus)
\r
285 USB_ControlRequest = (USB_Request_Header_t)
\r
287 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
\r
288 .bRequest = REQ_GetStatus,
\r
294 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
\r
296 return USB_Host_SendControlRequest(corenum, FeatureStatus);
\r
299 uint8_t USB_Host_ClearEndpointStall(const uint8_t corenum, const uint8_t EndpointAddress)
\r
301 USB_ControlRequest = (USB_Request_Header_t)
\r
303 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
\r
304 .bRequest = REQ_ClearFeature,
\r
305 .wValue = FEATURE_SEL_EndpointHalt,
\r
306 .wIndex = EndpointAddress,
\r
310 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
\r
312 return USB_Host_SendControlRequest(corenum,NULL);
\r
315 uint8_t USB_Host_SetInterfaceAltSetting(const uint8_t corenum,
\r
316 const uint8_t InterfaceIndex,
\r
317 const uint8_t AltSetting)
\r
319 USB_ControlRequest = (USB_Request_Header_t)
\r
321 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
\r
322 .bRequest = REQ_SetInterface,
\r
323 .wValue = AltSetting,
\r
324 .wIndex = InterfaceIndex,
\r
328 Pipe_SelectPipe(corenum, PIPE_CONTROLPIPE);
\r
330 return USB_Host_SendControlRequest(corenum,NULL);
\r