3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 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
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_HOSTSTDREQ_C
37 #include "HostStandardReq.h"
39 uint8_t USB_Host_SendControlRequest(void* const 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
);
57 Pipe_Write_Byte(USB_ControlRequest
.bmRequestType
);
58 Pipe_Write_Byte(USB_ControlRequest
.bRequest
);
59 Pipe_Write_Word_LE(USB_ControlRequest
.wValue
);
60 Pipe_Write_Word_LE(USB_ControlRequest
.wIndex
);
61 Pipe_Write_Word_LE(USB_ControlRequest
.wLength
);
65 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent
)) != HOST_SENDCONTROL_Successful
)
66 goto End_Of_Control_Send
;
70 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
71 goto End_Of_Control_Send
;
73 if ((USB_ControlRequest
.bmRequestType
& CONTROL_REQTYPE_DIRECTION
) == REQDIR_DEVICETOHOST
)
75 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
77 if (DataStream
!= NULL
)
83 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
)
84 goto End_Of_Control_Send
;
86 if (!(Pipe_BytesInPipe()))
89 while (Pipe_BytesInPipe() && DataLen
)
91 *(DataStream
++) = Pipe_Read_Byte();
100 Pipe_SetPipeToken(PIPE_TOKEN_OUT
);
103 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
104 goto End_Of_Control_Send
;
108 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
109 goto End_Of_Control_Send
;
113 if (DataStream
!= NULL
)
115 Pipe_SetPipeToken(PIPE_TOKEN_OUT
);
120 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
121 goto End_Of_Control_Send
;
123 while (DataLen
&& (Pipe_BytesInPipe() < USB_ControlPipeSize
))
125 Pipe_Write_Byte(*(DataStream
++));
132 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
)
133 goto End_Of_Control_Send
;
138 Pipe_SetPipeToken(PIPE_TOKEN_IN
);
141 if ((ReturnStatus
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
)
142 goto End_Of_Control_Send
;
151 USB_Host_SuspendBus();
153 Pipe_ResetPipe(PIPE_CONTROLPIPE
);
158 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType
)
160 #if (USB_HOST_TIMEOUT_MS < 0xFF)
161 uint8_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
163 uint16_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
166 while (!(((WaitType
== USB_HOST_WAITFOR_SetupSent
) && Pipe_IsSETUPSent()) ||
167 ((WaitType
== USB_HOST_WAITFOR_InReceived
) && Pipe_IsINReceived()) ||
168 ((WaitType
== USB_HOST_WAITFOR_OutReady
) && Pipe_IsOUTReady())))
172 if ((ErrorCode
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
175 if (!(TimeoutCounter
--))
176 return HOST_SENDCONTROL_SoftwareTimeOut
;
179 return HOST_SENDCONTROL_Successful
;