Oops - with new changes to the way the device Configuration Descriptor is retrieved...
[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 uint8_t USB_Host_SendControlRequest(void* BufferPtr)
39 {
40 uint8_t* HeaderStream = (uint8_t*)&USB_ControlRequest;
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_SetToken(PIPE_TOKEN_SETUP);
52 Pipe_ClearErrorFlags();
53
54 Pipe_Unfreeze();
55
56 for (uint8_t HeaderByte = 0; HeaderByte < sizeof(USB_Request_Header_t); HeaderByte++)
57 Pipe_Write_Byte(*(HeaderStream++));
58
59 Pipe_ClearSETUP();
60
61 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful)
62 goto End_Of_Control_Send;
63
64 Pipe_Freeze();
65
66 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
67 goto End_Of_Control_Send;
68
69 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
70 {
71 Pipe_SetToken(PIPE_TOKEN_IN);
72
73 if (DataStream != NULL)
74 {
75 while (DataLen)
76 {
77 Pipe_Unfreeze();
78
79 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
80 goto End_Of_Control_Send;
81
82 if (!(Pipe_BytesInPipe()))
83 DataLen = 0;
84
85 while (Pipe_BytesInPipe() && DataLen)
86 {
87 *(DataStream++) = Pipe_Read_Byte();
88 DataLen--;
89 }
90
91 Pipe_Freeze();
92 Pipe_ClearIN();
93 }
94 }
95
96 Pipe_SetToken(PIPE_TOKEN_OUT);
97 Pipe_Unfreeze();
98
99 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
100 goto End_Of_Control_Send;
101
102 Pipe_ClearOUT();
103
104 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
105 goto End_Of_Control_Send;
106 }
107 else
108 {
109 if (DataStream != NULL)
110 {
111 Pipe_SetToken(PIPE_TOKEN_OUT);
112 Pipe_Unfreeze();
113
114 while (DataLen)
115 {
116 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
117 goto End_Of_Control_Send;
118
119 while (DataLen && (Pipe_BytesInPipe() < USB_ControlPipeSize))
120 {
121 Pipe_Write_Byte(*(DataStream++));
122 DataLen--;
123 }
124
125 Pipe_ClearOUT();
126 }
127
128 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful)
129 goto End_Of_Control_Send;
130
131 Pipe_Freeze();
132 }
133
134 Pipe_SetToken(PIPE_TOKEN_IN);
135 Pipe_Unfreeze();
136
137 if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful)
138 goto End_Of_Control_Send;
139
140 Pipe_ClearIN();
141 }
142
143 End_Of_Control_Send:
144 Pipe_Freeze();
145
146 if (BusSuspended)
147 USB_Host_SuspendBus();
148
149 Pipe_ResetPipe(PIPE_CONTROLPIPE);
150
151 return ReturnStatus;
152 }
153
154 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType)
155 {
156 #if (USB_HOST_TIMEOUT_MS < 0xFF)
157 uint8_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
158 #else
159 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
160 #endif
161
162 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSETUPSent()) ||
163 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsINReceived()) ||
164 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsOUTReady())))
165 {
166 uint8_t ErrorCode;
167
168 if ((ErrorCode = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
169 return ErrorCode;
170
171 if (!(TimeoutCounter--))
172 return HOST_SENDCONTROL_SoftwareTimeOut;
173 }
174
175 return HOST_SENDCONTROL_Successful;
176 }
177
178 #endif