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