USB_HostRequest renamed to USB_ControlRequest, entire control request header is now...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / DevChapter9.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_DEVICE)
34
35 #define INCLUDE_FROM_DEVCHAPTER9_C
36 #include "DevChapter9.h"
37
38 uint8_t USB_ConfigurationNumber;
39 bool USB_RemoteWakeupEnabled;
40 bool USB_CurrentlySelfPowered;
41
42 void USB_Device_ProcessControlPacket(void)
43 {
44 bool RequestHandled = false;
45 uint8_t* RequestHeader = (uint8_t*)&USB_ControlRequest;
46
47 for (uint8_t RequestHeaderByte = 0; RequestHeaderByte < sizeof(USB_Request_Header_t); RequestHeaderByte++)
48 *(RequestHeader++) = Endpoint_Read_Byte();
49
50 switch (USB_ControlRequest.bRequest)
51 {
52 case REQ_GetStatus:
53 if ((USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
54 (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT)))
55 {
56 USB_Device_GetStatus();
57 RequestHandled = true;
58 }
59
60 break;
61 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
62 case REQ_ClearFeature:
63 case REQ_SetFeature:
64 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT))
65 {
66 USB_Device_ClearSetFeature();
67 RequestHandled = true;
68 }
69
70 break;
71 #endif
72 case REQ_SetAddress:
73 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
74 {
75 USB_Device_SetAddress();
76 RequestHandled = true;
77 }
78
79 break;
80 case REQ_GetDescriptor:
81 if ((USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
82 (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE)))
83 {
84 USB_Device_GetDescriptor();
85 RequestHandled = true;
86 }
87
88 break;
89 case REQ_GetConfiguration:
90 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE))
91 {
92 USB_Device_GetConfiguration();
93 RequestHandled = true;
94 }
95
96 break;
97 case REQ_SetConfiguration:
98 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
99 {
100 USB_Device_SetConfiguration();
101 RequestHandled = true;
102 }
103
104 break;
105 }
106
107 if (!(RequestHandled))
108 RAISE_EVENT(USB_UnhandledControlPacket);
109
110 if (Endpoint_IsSETUPReceived())
111 {
112 Endpoint_StallTransaction();
113 Endpoint_ClearSETUP();
114 }
115 }
116
117 static void USB_Device_SetAddress(void)
118 {
119 Endpoint_ClearSETUP();
120
121 while (!(Endpoint_IsINReady()));
122
123 Endpoint_ClearIN();
124
125 while (!(Endpoint_IsINReady()));
126
127 UDADDR = ((1 << ADDEN) | (USB_ControlRequest.wValue & 0x7F));
128
129 return;
130 }
131
132 static void USB_Device_SetConfiguration(void)
133 {
134 bool AlreadyConfigured = (USB_ConfigurationNumber != 0);
135
136 #if defined(USE_SINGLE_DEVICE_CONFIGURATION)
137 if (USB_ControlRequest.wValue > 1)
138 #else
139 USB_Descriptor_Device_t* DevDescriptorPtr;
140
141 if ((USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DevDescriptorPtr) == NO_DESCRIPTOR) ||
142 #if defined(USE_RAM_DESCRIPTORS)
143 (USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations))
144 #elif defined (USE_EEPROM_DESCRIPTORS)
145 (USB_ControlRequest.wValue > eeprom_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
146 #else
147 (USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
148 #endif
149 #endif
150 {
151 return;
152 }
153
154 Endpoint_ClearSETUP();
155
156 USB_ConfigurationNumber = USB_ControlRequest.wValue;
157
158 Endpoint_ClearIN();
159
160 if (!(AlreadyConfigured) && USB_ConfigurationNumber)
161 RAISE_EVENT(USB_DeviceEnumerationComplete);
162
163 RAISE_EVENT(USB_ConfigurationChanged);
164 }
165
166 void USB_Device_GetConfiguration(void)
167 {
168 Endpoint_ClearSETUP();
169
170 Endpoint_Write_Byte(USB_ConfigurationNumber);
171
172 Endpoint_ClearIN();
173
174 while (!(Endpoint_IsOUTReceived()));
175 Endpoint_ClearOUT();
176 }
177
178 static void USB_Device_GetDescriptor(void)
179 {
180 void* DescriptorPointer;
181 uint16_t DescriptorSize;
182
183 if ((DescriptorSize = USB_GetDescriptor(USB_ControlRequest.wValue, USB_ControlRequest.wIndex, &DescriptorPointer)) == NO_DESCRIPTOR)
184 return;
185
186 Endpoint_ClearSETUP();
187
188 #if defined(USE_RAM_DESCRIPTORS)
189 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
190 #else
191 bool SendZLP;
192
193 if (USB_ControlRequest.wLength > DescriptorSize)
194 USB_ControlRequest.wLength = DescriptorSize;
195
196 while (USB_ControlRequest.wLength)
197 {
198 while (!(Endpoint_IsINReady()))
199 {
200 if (Endpoint_IsOUTReceived())
201 {
202 Endpoint_ClearOUT();
203 return;
204 }
205 }
206
207 while (USB_ControlRequest.wLength && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
208 {
209 #if defined (USE_EEPROM_DESCRIPTORS)
210 Endpoint_Write_Byte(eeprom_read_byte(DescriptorPointer++));
211 #else
212 Endpoint_Write_Byte(pgm_read_byte(DescriptorPointer++));
213 #endif
214
215 USB_ControlRequest.wLength--;
216 }
217
218 SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);
219 Endpoint_ClearIN();
220 }
221
222 if (SendZLP)
223 {
224 while (!(Endpoint_IsINReady()));
225 Endpoint_ClearIN();
226 }
227
228 while (!(Endpoint_IsOUTReceived()));
229 #endif
230
231 Endpoint_ClearOUT();
232 }
233
234 static void USB_Device_GetStatus(void)
235 {
236 uint8_t CurrentStatus = 0;
237
238 switch (USB_ControlRequest.bmRequestType)
239 {
240 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE):
241 if (USB_CurrentlySelfPowered)
242 CurrentStatus |= FEATURE_SELFPOWERED_ENABLED;
243
244 if (USB_RemoteWakeupEnabled)
245 CurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
246
247 break;
248 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
249 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):
250 Endpoint_SelectEndpoint(USB_ControlRequest.wIndex);
251
252 CurrentStatus = Endpoint_IsStalled();
253
254 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
255
256 break;
257 #endif
258 }
259
260 Endpoint_ClearSETUP();
261
262 Endpoint_Write_Word_LE(CurrentStatus);
263
264 Endpoint_ClearIN();
265
266 while (!(Endpoint_IsOUTReceived()));
267 Endpoint_ClearOUT();
268 }
269
270 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
271 static void USB_Device_ClearSetFeature(void)
272 {
273 switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)
274 {
275 case REQREC_ENDPOINT:
276 if (USB_ControlRequest.wValue == FEATURE_ENDPOINT_HALT)
277 {
278 uint8_t EndpointIndex = (USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
279
280 if (EndpointIndex != ENDPOINT_CONTROLEP)
281 {
282 Endpoint_SelectEndpoint(EndpointIndex);
283
284 if (Endpoint_IsEnabled())
285 {
286 if (USB_ControlRequest.bRequest == REQ_ClearFeature)
287 {
288 Endpoint_ClearStall();
289 Endpoint_ResetFIFO(EndpointIndex);
290 Endpoint_ResetDataToggle();
291 }
292 else
293 {
294 Endpoint_StallTransaction();
295 }
296 }
297
298 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
299 Endpoint_ClearSETUP();
300 Endpoint_ClearIN();
301 }
302 }
303
304 break;
305 }
306 }
307 #endif
308
309 #endif