Add missing doxygen group terminator to the new CompilerSpecific.h header file.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / DeviceStandardReq.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_DEVICE)
35
36 #define __INCLUDE_FROM_DEVICESTDREQ_C
37 #include "DeviceStandardReq.h"
38
39 uint8_t USB_ConfigurationNumber;
40
41 #if !defined(NO_DEVICE_SELF_POWER)
42 bool USB_CurrentlySelfPowered;
43 #endif
44
45 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
46 bool USB_RemoteWakeupEnabled;
47 #endif
48
49 void USB_Device_ProcessControlRequest(void)
50 {
51 USB_ControlRequest.bmRequestType = Endpoint_Read_8();
52 USB_ControlRequest.bRequest = Endpoint_Read_8();
53 USB_ControlRequest.wValue = Endpoint_Read_16_LE();
54 USB_ControlRequest.wIndex = Endpoint_Read_16_LE();
55 USB_ControlRequest.wLength = Endpoint_Read_16_LE();
56
57 EVENT_USB_Device_ControlRequest();
58
59 if (Endpoint_IsSETUPReceived())
60 {
61 uint8_t bmRequestType = USB_ControlRequest.bmRequestType;
62
63 switch (USB_ControlRequest.bRequest)
64 {
65 case REQ_GetStatus:
66 if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
67 (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT)))
68 {
69 USB_Device_GetStatus();
70 }
71
72 break;
73 case REQ_ClearFeature:
74 case REQ_SetFeature:
75 if ((bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE)) ||
76 (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT)))
77 {
78 USB_Device_ClearSetFeature();
79 }
80
81 break;
82 case REQ_SetAddress:
83 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
84 USB_Device_SetAddress();
85
86 break;
87 case REQ_GetDescriptor:
88 if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
89 (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE)))
90 {
91 USB_Device_GetDescriptor();
92 }
93
94 break;
95 case REQ_GetConfiguration:
96 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE))
97 USB_Device_GetConfiguration();
98
99 break;
100 case REQ_SetConfiguration:
101 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
102 USB_Device_SetConfiguration();
103
104 break;
105 }
106 }
107
108 if (Endpoint_IsSETUPReceived())
109 {
110 Endpoint_StallTransaction();
111 Endpoint_ClearSETUP();
112 }
113 }
114
115 static void USB_Device_SetAddress(void)
116 {
117 uint8_t DeviceAddress = (USB_ControlRequest.wValue & 0x7F);
118 uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
119 GlobalInterruptDisable();
120
121 Endpoint_ClearSETUP();
122
123 Endpoint_ClearStatusStage();
124
125 while (!(Endpoint_IsINReady()));
126
127 USB_Device_SetDeviceAddress(DeviceAddress);
128 USB_DeviceState = (DeviceAddress) ? DEVICE_STATE_Addressed : DEVICE_STATE_Default;
129
130 SetGlobalInterruptMask(CurrentGlobalInt);
131 }
132
133 static void USB_Device_SetConfiguration(void)
134 {
135 #if defined(FIXED_NUM_CONFIGURATIONS)
136 if ((uint8_t)USB_ControlRequest.wValue > FIXED_NUM_CONFIGURATIONS)
137 return;
138 #else
139 USB_Descriptor_Device_t* DevDescriptorPtr;
140
141 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE)
142 #if defined(USE_FLASH_DESCRIPTORS)
143 #define MemoryAddressSpace MEMSPACE_FLASH
144 #elif defined(USE_EEPROM_DESCRIPTORS)
145 #define MemoryAddressSpace MEMSPACE_EEPROM
146 #elif defined(USE_SRAM_DESCRIPTORS)
147 #define MemoryAddressSpace MEMSPACE_SRAM
148 #else
149 uint8_t MemoryAddressSpace;
150 #endif
151 #endif
152
153 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DevDescriptorPtr
154 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
155 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
156 , &MemoryAddressSpace
157 #endif
158 ) == NO_DESCRIPTOR)
159 {
160 return;
161 }
162
163 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE)
164 if (MemoryAddressSpace == MEMSPACE_FLASH)
165 {
166 if (((uint8_t)USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
167 return;
168 }
169 else if (MemoryAddressSpace == MEMSPACE_EEPROM)
170 {
171 if (((uint8_t)USB_ControlRequest.wValue > eeprom_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
172 return;
173 }
174 else
175 {
176 if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)
177 return;
178 }
179 #else
180 if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)
181 return;
182 #endif
183 #endif
184
185 Endpoint_ClearSETUP();
186
187 USB_ConfigurationNumber = (uint8_t)USB_ControlRequest.wValue;
188
189 Endpoint_ClearStatusStage();
190
191 if (USB_ConfigurationNumber)
192 USB_DeviceState = DEVICE_STATE_Configured;
193 else
194 USB_DeviceState = (USB_Device_IsAddressSet()) ? DEVICE_STATE_Configured : DEVICE_STATE_Powered;
195
196 EVENT_USB_Device_ConfigurationChanged();
197 }
198
199 static void USB_Device_GetConfiguration(void)
200 {
201 Endpoint_ClearSETUP();
202
203 Endpoint_Write_8(USB_ConfigurationNumber);
204 Endpoint_ClearIN();
205
206 Endpoint_ClearStatusStage();
207 }
208
209 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
210 static void USB_Device_GetInternalSerialDescriptor(void)
211 {
212 struct
213 {
214 USB_Descriptor_Header_t Header;
215 uint16_t UnicodeString[INTERNAL_SERIAL_LENGTH_BITS / 4];
216 } SignatureDescriptor;
217
218 SignatureDescriptor.Header.Type = DTYPE_String;
219 SignatureDescriptor.Header.Size = USB_STRING_LEN(INTERNAL_SERIAL_LENGTH_BITS / 4);
220
221 USB_Device_GetSerialString(SignatureDescriptor.UnicodeString);
222
223 Endpoint_ClearSETUP();
224
225 Endpoint_Write_Control_Stream_LE(&SignatureDescriptor, sizeof(SignatureDescriptor));
226 Endpoint_ClearOUT();
227 }
228 #endif
229
230 static void USB_Device_GetDescriptor(void)
231 {
232 const void* DescriptorPointer;
233 uint16_t DescriptorSize;
234
235 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
236 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
237 uint8_t DescriptorAddressSpace;
238 #endif
239
240 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
241 if (USB_ControlRequest.wValue == ((DTYPE_String << 8) | USE_INTERNAL_SERIAL))
242 {
243 USB_Device_GetInternalSerialDescriptor();
244 return;
245 }
246 #endif
247
248 if ((DescriptorSize = CALLBACK_USB_GetDescriptor(USB_ControlRequest.wValue, USB_ControlRequest.wIndex,
249 &DescriptorPointer
250 #if defined(ARCH_HAS_MULTI_ADDRESS_SPACE) && \
251 !(defined(USE_FLASH_DESCRIPTORS) || defined(USE_EEPROM_DESCRIPTORS) || defined(USE_RAM_DESCRIPTORS))
252 , &DescriptorAddressSpace
253 #endif
254 )) == NO_DESCRIPTOR)
255 {
256 return;
257 }
258
259 Endpoint_ClearSETUP();
260
261 #if defined(USE_RAM_DESCRIPTORS) || !defined(ARCH_HAS_MULTI_ADDRESS_SPACE)
262 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
263 #elif defined(USE_EEPROM_DESCRIPTORS)
264 Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
265 #elif defined(USE_FLASH_DESCRIPTORS)
266 Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
267 #else
268 if (DescriptorAddressSpace == MEMSPACE_FLASH)
269 Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
270 else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
271 Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
272 else
273 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
274 #endif
275
276 Endpoint_ClearOUT();
277 }
278
279 static void USB_Device_GetStatus(void)
280 {
281 uint8_t CurrentStatus = 0;
282
283 switch (USB_ControlRequest.bmRequestType)
284 {
285 #if !defined(NO_DEVICE_SELF_POWER) || !defined(NO_DEVICE_REMOTE_WAKEUP)
286 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE):
287 #if !defined(NO_DEVICE_SELF_POWER)
288 if (USB_CurrentlySelfPowered)
289 CurrentStatus |= FEATURE_SELFPOWERED_ENABLED;
290 #endif
291
292 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
293 if (USB_RemoteWakeupEnabled)
294 CurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
295 #endif
296 break;
297 #endif
298 #if !defined(CONTROL_ONLY_DEVICE)
299 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):
300 Endpoint_SelectEndpoint((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
301
302 CurrentStatus = Endpoint_IsStalled();
303
304 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
305
306 break;
307 #endif
308 default:
309 return;
310 }
311
312 Endpoint_ClearSETUP();
313
314 Endpoint_Write_16_LE(CurrentStatus);
315 Endpoint_ClearIN();
316
317 Endpoint_ClearStatusStage();
318 }
319
320 static void USB_Device_ClearSetFeature(void)
321 {
322 switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)
323 {
324 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
325 case REQREC_DEVICE:
326 if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_DeviceRemoteWakeup)
327 USB_RemoteWakeupEnabled = (USB_ControlRequest.bRequest == REQ_SetFeature);
328 else
329 return;
330
331 break;
332 #endif
333 #if !defined(CONTROL_ONLY_DEVICE)
334 case REQREC_ENDPOINT:
335 if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_EndpointHalt)
336 {
337 uint8_t EndpointIndex = ((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
338
339 if (EndpointIndex == ENDPOINT_CONTROLEP)
340 return;
341
342 Endpoint_SelectEndpoint(EndpointIndex);
343
344 if (Endpoint_IsEnabled())
345 {
346 if (USB_ControlRequest.bRequest == REQ_SetFeature)
347 {
348 Endpoint_StallTransaction();
349 }
350 else
351 {
352 Endpoint_ClearStall();
353 Endpoint_ResetEndpoint(EndpointIndex);
354 Endpoint_ResetDataToggle();
355 }
356 }
357 }
358
359 break;
360 #endif
361 default:
362 return;
363 }
364
365 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
366
367 Endpoint_ClearSETUP();
368
369 Endpoint_ClearStatusStage();
370 }
371
372 #endif
373