3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../HighLevel/USBMode.h"
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_HOSTCHAPTER9_C
37 #include "HostChapter9.h"
39 uint8_t USB_Host_SendControlRequest(void* BufferPtr
)
41 uint8_t* HeaderStream
= (uint8_t*)&USB_ControlRequest
;
42 uint8_t* DataStream
= (uint8_t*)BufferPtr
;
43 bool BusSuspended
= USB_Host_IsBusSuspended();
44 uint8_t ReturnStatus
= HOST_SENDCONTROL_Successful
;
45 uint16_t DataLen
= USB_ControlRequest
.wLength
;
49 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
50 goto End_Of_Control_Send
;
52 Pipe_SetPipeToken(PIPE_TOKEN_SETUP
);
53 Pipe_ClearErrorFlags();
57 for (uint8_t HeaderByte
= 0; HeaderByte
< sizeof(USB_Request_Header_t
); HeaderByte
++)
58 Pipe_Write_Byte(*(HeaderStream
++));
62 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent
)) != HOST_SENDCONTROL_Successful
)
63 goto End_Of_Control_Send
;
67 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
68 goto End_Of_Control_Send
;
70 if ((USB_ControlRequest
.bmRequestType
& CONTROL_REQTYPE_DIRECTION
) == REQDIR_DEVICETOHOST
)
72 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
74 if (DataStream
!= NULL
)
80 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
)
81 goto End_Of_Control_Send
;
83 if (!(Pipe_BytesInPipe()))
86 while (Pipe_BytesInPipe() && DataLen
)
88 *(DataStream
++) = Pipe_Read_Byte();
97 Pipe_SetPipeToken(PIPE_TOKEN_OUT
);
100 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
101 goto End_Of_Control_Send
;
105 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
106 goto End_Of_Control_Send
;
110 if (DataStream
!= NULL
)
112 Pipe_SetPipeToken(PIPE_TOKEN_OUT
);
117 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
118 goto End_Of_Control_Send
;
120 while (DataLen
&& (Pipe_BytesInPipe() < USB_ControlPipeSize
))
122 Pipe_Write_Byte(*(DataStream
++));
129 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
130 goto End_Of_Control_Send
;
135 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
138 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
)
139 goto End_Of_Control_Send
;
148 USB_Host_SuspendBus();
150 Pipe_ResetPipe(PIPE_CONTROLPIPE
);
155 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType
)
157 #if (USB_HOST_TIMEOUT_MS < 0xFF)
158 uint8_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
160 uint16_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
163 while (!(((WaitType
== USB_HOST_WAITFOR_SetupSent
) && Pipe_IsSETUPSent()) ||
164 ((WaitType
== USB_HOST_WAITFOR_InReceived
) && Pipe_IsINReceived()) ||
165 ((WaitType
== USB_HOST_WAITFOR_OutReady
) && Pipe_IsOUTReady())))
169 if ((ErrorCode
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
172 if (!(TimeoutCounter
--))
173 return HOST_SENDCONTROL_SoftwareTimeOut
;
176 return HOST_SENDCONTROL_Successful
;