3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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 #include "../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define INCLUDE_FROM_HOSTCHAPTER9_C
36 #include "HostChapter9.h"
38 uint8_t USB_Host_SendControlRequest(void* BufferPtr
)
40 uint8_t* HeaderStream
= (uint8_t*)&USB_ControlRequest
;
41 uint8_t* DataStream
= (uint8_t*)BufferPtr
;
42 bool BusSuspended
= USB_Host_IsBusSuspended();
43 uint8_t ReturnStatus
= HOST_SENDCONTROL_Successful
;
44 uint16_t DataLen
= USB_ControlRequest
.wLength
;
48 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
49 goto End_Of_Control_Send
;
51 Pipe_SetToken(PIPE_TOKEN_SETUP
);
52 Pipe_ClearErrorFlags();
56 for (uint8_t HeaderByte
= 0; HeaderByte
< sizeof(USB_Request_Header_t
); HeaderByte
++)
57 Pipe_Write_Byte(*(HeaderStream
++));
61 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent
)) != HOST_SENDCONTROL_Successful
)
62 goto End_Of_Control_Send
;
66 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
67 goto End_Of_Control_Send
;
69 if ((USB_ControlRequest
.bmRequestType
& CONTROL_REQTYPE_DIRECTION
) == REQDIR_DEVICETOHOST
)
71 Pipe_SetToken(PIPE_TOKEN_IN
);
73 if (DataStream
!= NULL
)
79 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
)
80 goto End_Of_Control_Send
;
82 if (!(Pipe_BytesInPipe()))
85 while (Pipe_BytesInPipe() && DataLen
)
87 *(DataStream
++) = Pipe_Read_Byte();
96 Pipe_SetToken(PIPE_TOKEN_OUT
);
99 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
100 goto End_Of_Control_Send
;
104 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
105 goto End_Of_Control_Send
;
109 if (DataStream
!= NULL
)
111 Pipe_SetToken(PIPE_TOKEN_OUT
);
116 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
117 goto End_Of_Control_Send
;
119 while (DataLen
&& (Pipe_BytesInPipe() < USB_ControlPipeSize
))
121 Pipe_Write_Byte(*(DataStream
++));
128 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
129 goto End_Of_Control_Send
;
134 Pipe_SetToken(PIPE_TOKEN_IN
);
137 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
)
138 goto End_Of_Control_Send
;
147 USB_Host_SuspendBus();
149 Pipe_ResetPipe(PIPE_CONTROLPIPE
);
154 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType
)
156 #if (USB_HOST_TIMEOUT_MS < 0xFF)
157 uint8_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
159 uint16_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
162 while (!(((WaitType
== USB_HOST_WAITFOR_SetupSent
) && Pipe_IsSETUPSent()) ||
163 ((WaitType
== USB_HOST_WAITFOR_InReceived
) && Pipe_IsINReceived()) ||
164 ((WaitType
== USB_HOST_WAITFOR_OutReady
) && Pipe_IsOUTReady())))
168 if ((ErrorCode
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
171 if (!(TimeoutCounter
--))
172 return HOST_SENDCONTROL_SoftwareTimeOut
;
175 return HOST_SENDCONTROL_Successful
;