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