Removed specialized Endpoint_ClearControl* and Pipe_ClearControl* macros in favour...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / HostChapter9.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "../HighLevel/USBMode.h"
32
33 #if defined(USB_CAN_BE_HOST)
34
35 #define INCLUDE_FROM_HOSTCHAPTER9_C
36 #include "HostChapter9.h"
37
38 USB_Host_Request_Header_t USB_HostRequest;
39
40 uint8_t USB_Host_SendControlRequest(void* BufferPtr)
41 {
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;
47
48 USB_Host_ResumeBus();
49
50 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
51 return ReturnStatus;
52
53 Pipe_SetToken(PIPE_TOKEN_SETUP);
54 Pipe_ClearErrorFlags();
55
56 Pipe_Unfreeze();
57
58 for (uint8_t HeaderByte = 0; HeaderByte < sizeof(USB_Host_Request_Header_t); HeaderByte++)
59 Pipe_Write_Byte(*(HeaderStream++));
60
61 Pipe_ClearSETUP();
62
63 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_SetupSent)))
64 goto End_Of_Control_Send;
65
66 Pipe_Freeze();
67
68 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
69 goto End_Of_Control_Send;
70
71 if ((USB_HostRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
72 {
73 Pipe_SetToken(PIPE_TOKEN_IN);
74
75 if (DataStream != NULL)
76 {
77 while (DataLen)
78 {
79 Pipe_Unfreeze();
80
81 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived)))
82 goto End_Of_Control_Send;
83
84 if (!(Pipe_BytesInPipe()))
85 DataLen = 0;
86
87 while (Pipe_BytesInPipe() && DataLen)
88 {
89 *(DataStream++) = Pipe_Read_Byte();
90 DataLen--;
91 }
92
93 Pipe_Freeze();
94 Pipe_ClearIN();
95 }
96 }
97
98 Pipe_SetToken(PIPE_TOKEN_OUT);
99 Pipe_Unfreeze();
100
101 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
102 goto End_Of_Control_Send;
103
104 Pipe_ClearOUT();
105
106 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
107 goto End_Of_Control_Send;
108 }
109 else
110 {
111 if (DataStream != NULL)
112 {
113 Pipe_SetToken(PIPE_TOKEN_OUT);
114 Pipe_Unfreeze();
115
116 while (DataLen)
117 {
118 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
119 goto End_Of_Control_Send;
120
121 while (DataLen && (Pipe_BytesInPipe() < USB_ControlPipeSize))
122 {
123 Pipe_Write_Byte(*(DataStream++));
124 DataLen--;
125 }
126
127 Pipe_ClearOUT();
128 }
129
130 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
131 goto End_Of_Control_Send;
132
133 Pipe_Freeze();
134 }
135
136 Pipe_SetToken(PIPE_TOKEN_IN);
137 Pipe_Unfreeze();
138
139 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived)))
140 goto End_Of_Control_Send;
141
142 Pipe_ClearIN();
143 }
144
145 End_Of_Control_Send:
146 Pipe_Freeze();
147
148 if (BusSuspended)
149 USB_Host_SuspendBus();
150
151 Pipe_ResetPipe(PIPE_CONTROLPIPE);
152
153 return ReturnStatus;
154 }
155
156 static uint8_t USB_Host_Wait_For_Setup_IOS(const uint8_t WaitType)
157 {
158 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
159
160 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
161 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
162 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
163 {
164 uint8_t ErrorCode;
165
166 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
167 return ErrorCode;
168
169 if (!(TimeoutCounter--))
170 return HOST_SENDCONTROL_SoftwareTimeOut;
171 }
172
173 return HOST_SENDCONTROL_Successful;
174 }
175
176 #endif