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 USB_Host_Request_Header_t USB_HostRequest
;
40 uint8_t USB_Host_SendControlRequest(void* BufferPtr
)
42 uint8_t* HeaderStream
= (uint8_t*)&USB_HostRequest
;
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_HostRequest
.wLength
;
50 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
53 Pipe_SetToken(PIPE_TOKEN_SETUP
);
54 Pipe_ClearErrorFlags();
55 Pipe_ClearSetupSent();
59 for (uint8_t HeaderByte
= 0; HeaderByte
< sizeof(USB_Host_Request_Header_t
); HeaderByte
++)
60 Pipe_Write_Byte(*(HeaderStream
++));
64 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_SetupSent
)))
65 goto End_Of_Control_Send
;
67 Pipe_ClearSetupSent();
70 if ((ReturnStatus
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
71 goto End_Of_Control_Send
;
73 if ((USB_HostRequest
.bmRequestType
& CONTROL_REQTYPE_DIRECTION
) == REQDIR_DEVICETOHOST
)
75 Pipe_SetToken(PIPE_TOKEN_IN
);
77 if (DataStream
!= NULL
)
83 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived
)))
84 goto End_Of_Control_Send
;
86 if (!(Pipe_BytesInPipe()))
89 while (Pipe_BytesInPipe() && DataLen
)
91 *(DataStream
++) = Pipe_Read_Byte();
100 Pipe_SetToken(PIPE_TOKEN_OUT
);
103 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
)))
104 goto End_Of_Control_Send
;
106 Pipe_ClearSetupOUT();
108 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
)))
109 goto End_Of_Control_Send
;
113 if (DataStream
!= NULL
)
115 Pipe_SetToken(PIPE_TOKEN_OUT
);
120 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
)))
121 goto End_Of_Control_Send
;
123 while (DataLen
&& (Pipe_BytesInPipe() < USB_ControlPipeSize
))
125 Pipe_Write_Byte(*(DataStream
++));
129 Pipe_ClearSetupOUT();
132 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
)))
133 goto End_Of_Control_Send
;
138 Pipe_SetToken(PIPE_TOKEN_IN
);
141 if ((ReturnStatus
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived
)))
142 goto End_Of_Control_Send
;
151 USB_Host_SuspendBus();
153 Pipe_ResetPipe(PIPE_CONTROLPIPE
);
158 static uint8_t USB_Host_Wait_For_Setup_IOS(const uint8_t WaitType
)
160 uint16_t TimeoutCounter
= USB_HOST_TIMEOUT_MS
;
162 while (!(((WaitType
== USB_HOST_WAITFOR_SetupSent
) && Pipe_IsSetupSent()) ||
163 ((WaitType
== USB_HOST_WAITFOR_InReceived
) && Pipe_IsSetupINReceived()) ||
164 ((WaitType
== USB_HOST_WAITFOR_OutReady
) && Pipe_IsSetupOUTReady())))
168 if ((ErrorCode
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
)
171 if (!(TimeoutCounter
--))
172 return HOST_SENDCONTROL_SoftwareTimeOut
;
175 return HOST_SENDCONTROL_Successful
;