F_CLOCK changed to F_USB to be more descriptive, and applicable on future architectur...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / HostStandardReq.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_HOSTSTDREQ_C
37 #include "HostStandardReq.h"
38
39 uint8_t USB_Host_SendControlRequest(void* const BufferPtr)
40 {
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;
45
46 USB_Host_ResumeBus();
47
48 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
49 goto End_Of_Control_Send;
50
51 Pipe_SetPipeToken(PIPE_TOKEN_SETUP);
52 Pipe_ClearError();
53
54 Pipe_Unfreeze();
55
56 Pipe_Write_Byte(USB_ControlRequest.bmRequestType);
57 Pipe_Write_Byte(USB_ControlRequest.bRequest);
58 Pipe_Write_Word_LE(USB_ControlRequest.wValue);
59 Pipe_Write_Word_LE(USB_ControlRequest.wIndex);
60 Pipe_Write_Word_LE(USB_ControlRequest.wLength);
61
62 Pipe_ClearSETUP();
63
64 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
65 goto End_Of_Control_Send;
66
67 Pipe_Freeze();
68
69 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
70 goto End_Of_Control_Send;
71
72 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
73 {
74 Pipe_SetPipeToken(PIPE_TOKEN_IN);
75
76 if (DataStream != NULL)
77 {
78 while (DataLen)
79 {
80 Pipe_Unfreeze();
81
82 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
83 goto End_Of_Control_Send;
84
85 if (!(Pipe_BytesInPipe()))
86 DataLen = 0;
87
88 while (Pipe_BytesInPipe() && DataLen)
89 {
90 *(DataStream++) = Pipe_Read_Byte();
91 DataLen--;
92 }
93
94 Pipe_Freeze();
95 Pipe_ClearIN();
96 }
97 }
98
99 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
100 Pipe_Unfreeze();
101
102 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
103 goto End_Of_Control_Send;
104
105 Pipe_ClearOUT();
106
107 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
108 goto End_Of_Control_Send;
109 }
110 else
111 {
112 if (DataStream != NULL)
113 {
114 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
115 Pipe_Unfreeze();
116
117 while (DataLen)
118 {
119 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
120 goto End_Of_Control_Send;
121
122 while (DataLen && (Pipe_BytesInPipe() < USB_ControlPipeSize))
123 {
124 Pipe_Write_Byte(*(DataStream++));
125 DataLen--;
126 }
127
128 Pipe_ClearOUT();
129 }
130
131 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
132 goto End_Of_Control_Send;
133
134 Pipe_Freeze();
135 }
136
137 Pipe_SetPipeToken(PIPE_TOKEN_IN);
138 Pipe_Unfreeze();
139
140 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
141 goto End_Of_Control_Send;
142
143 Pipe_ClearIN();
144 }
145
146 End_Of_Control_Send:
147 Pipe_Freeze();
148
149 if (BusSuspended)
150 USB_Host_SuspendBus();
151
152 Pipe_ResetPipe(PIPE_CONTROLPIPE);
153
154 return ReturnStatus;
155 }
156
157 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
158 {
159 #if (USB_HOST_TIMEOUT_MS < 0xFF)
160 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
161 #else
162 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
163 #endif
164
165 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
166 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
167 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
168 {
169 uint8_t ErrorCode;
170
171 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
172 return ErrorCode;
173
174 if (!(TimeoutCounter--))
175 return HOST_SENDCONTROL_SoftwareTimeOut;
176 }
177
178 return HOST_SENDCONTROL_Successful;
179 }
180
181 #endif
182