123a1d25f32dcd8fcbc3bacb44b66a54b9d04d3f
[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_SelectPipe(PIPE_CONTROLPIPE);
54 Pipe_SetToken(PIPE_TOKEN_SETUP);
55 Pipe_ClearErrorFlags();
56 Pipe_ClearSetupSent();
57
58 Pipe_Unfreeze();
59
60 for (uint8_t HeaderByte = 0; HeaderByte < sizeof(USB_Host_Request_Header_t); HeaderByte++)
61 Pipe_Write_Byte(*(HeaderStream++));
62
63 Pipe_ClearSetupOUT();
64
65 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_SetupSent)))
66 goto End_Of_Control_Send;
67
68 Pipe_ClearSetupSent();
69 Pipe_Freeze();
70
71 if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful)
72 goto End_Of_Control_Send;
73
74 if ((USB_HostRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST)
75 {
76 Pipe_SetToken(PIPE_TOKEN_IN);
77
78 if (DataStream != NULL)
79 {
80 while (DataLen)
81 {
82 Pipe_Unfreeze();
83
84 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived)))
85 goto End_Of_Control_Send;
86
87 if (!(Pipe_BytesInPipe()))
88 DataLen = 0;
89
90 while (Pipe_BytesInPipe() && DataLen)
91 {
92 *(DataStream++) = Pipe_Read_Byte();
93 DataLen--;
94 }
95
96 Pipe_Freeze();
97 Pipe_ClearSetupIN();
98 }
99 }
100
101 Pipe_SetToken(PIPE_TOKEN_OUT);
102 Pipe_Unfreeze();
103
104 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
105 goto End_Of_Control_Send;
106
107 Pipe_ClearSetupOUT();
108
109 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
110 goto End_Of_Control_Send;
111 }
112 else
113 {
114 if (DataStream != NULL)
115 {
116 Pipe_SetToken(PIPE_TOKEN_OUT);
117 Pipe_Unfreeze();
118
119 while (DataLen)
120 {
121 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
122 goto End_Of_Control_Send;
123
124 while (DataLen && (Pipe_BytesInPipe() < USB_ControlPipeSize))
125 {
126 Pipe_Write_Byte(*(DataStream++));
127 DataLen--;
128 }
129
130 Pipe_ClearSetupOUT();
131 }
132
133 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady)))
134 goto End_Of_Control_Send;
135
136 Pipe_Freeze();
137 }
138
139 Pipe_SetToken(PIPE_TOKEN_IN);
140 Pipe_Unfreeze();
141
142 if ((ReturnStatus = USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived)))
143 goto End_Of_Control_Send;
144
145 Pipe_ClearSetupIN();
146 }
147
148 End_Of_Control_Send:
149 Pipe_Freeze();
150
151 if (BusSuspended)
152 USB_Host_SuspendBus();
153
154 Pipe_ResetPipe(PIPE_CONTROLPIPE);
155
156 return ReturnStatus;
157 }
158
159 static uint8_t USB_Host_Wait_For_Setup_IOS(const uint8_t WaitType)
160 {
161 uint16_t TimeoutCounter = USB_HOST_TIMEOUT_MS;
162
163 while (!(((WaitType == USB_HOST_WAITFOR_SetupSent) && Pipe_IsSetupSent()) ||
164 ((WaitType == USB_HOST_WAITFOR_InReceived) && Pipe_IsSetupINReceived()) ||
165 ((WaitType == USB_HOST_WAITFOR_OutReady) && Pipe_IsSetupOUTReady())))
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