Fix bugs in the new VirtualSerialMassStorage demo (thanks to Martin Degelsegger).
[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* 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_ClearError();
54
55 Pipe_Unfreeze();
56
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);
62
63 Pipe_ClearSETUP();
64
65 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
66 goto End_Of_Control_Send;
67
68 Pipe_Freeze();
69
70 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
71 goto End_Of_Control_Send;
72
73 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
74 {
75 Pipe_SetPipeToken(PIPE_TOKEN_IN);
76
77 if (DataStream != NULL)
78 {
79 while (DataLen)
80 {
81 Pipe_Unfreeze();
82
83 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
84 goto End_Of_Control_Send;
85
86 if (!(Pipe_BytesInPipe()))
87 DataLen = 0;
88
89 while (Pipe_BytesInPipe() && DataLen)
90 {
91 *(DataStream++) = Pipe_Read_Byte();
92 DataLen--;
93 }
94
95 Pipe_Freeze();
96 Pipe_ClearIN();
97 }
98 }
99
100 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
101 Pipe_Unfreeze();
102
103 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
104 goto End_Of_Control_Send;
105
106 Pipe_ClearOUT();
107
108 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
109 goto End_Of_Control_Send;
110 }
111 else
112 {
113 if (DataStream != NULL)
114 {
115 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
116 Pipe_Unfreeze();
117
118 while (DataLen)
119 {
120 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
121 goto End_Of_Control_Send;
122
123 while (DataLen && (Pipe_BytesInPipe() < USB_ControlPipeSize))
124 {
125 Pipe_Write_Byte(*(DataStream++));
126 DataLen--;
127 }
128
129 Pipe_ClearOUT();
130 }
131
132 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
133 goto End_Of_Control_Send;
134
135 Pipe_Freeze();
136 }
137
138 Pipe_SetPipeToken(PIPE_TOKEN_IN);
139 Pipe_Unfreeze();
140
141 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
142 goto End_Of_Control_Send;
143
144 Pipe_ClearIN();
145 }
146
147 End_Of_Control_Send:
148 Pipe_Freeze();
149
150 if (BusSuspended)
151 USB_Host_SuspendBus();
152
153 Pipe_ResetPipe(PIPE_CONTROLPIPE);
154
155 return ReturnStatus;
156 }
157
158 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
159 {
160 #if (USB_HOST_TIMEOUT_MS < 0xFF)
161 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
162 #else
163 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
164 #endif
165
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())))
169 {
170 uint8_t ErrorCode;
171
172 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
173 return ErrorCode;
174
175 if (!(TimeoutCounter--))
176 return HOST_SENDCONTROL_SoftwareTimeOut;
177 }
178
179 return HOST_SENDCONTROL_Successful;
180 }
181
182 #endif
183