Add experimental support for the AVR32 UC3A4 microcontrollers.
[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_Byte();
52 USB_ControlRequest.bRequest = Endpoint_Read_Byte();
53 USB_ControlRequest.wValue = Endpoint_Read_Word_LE();
54 USB_ControlRequest.wIndex = Endpoint_Read_Word_LE();
55 USB_ControlRequest.wLength = Endpoint_Read_Word_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
119 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
120 {
121 Endpoint_ClearSETUP();
122
123 Endpoint_ClearStatusStage();
124
125 while (!(Endpoint_IsINReady()));
126
127 USB_Device_SetDeviceAddress(DeviceAddress);
128 }
129
130 USB_DeviceState = (DeviceAddress) ? DEVICE_STATE_Addressed : DEVICE_STATE_Default;
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(USE_FLASH_DESCRIPTORS)
142 #define MemoryAddressSpace MEMSPACE_FLASH
143 #elif defined(USE_EEPROM_DESCRIPTORS)
144 #define MemoryAddressSpace MEMSPACE_EEPROM
145 #elif defined(USE_SRAM_DESCRIPTORS)
146 #define MemoryAddressSpace MEMSPACE_SRAM
147 #else
148 uint8_t MemoryAddressSpace;
149 #endif
150
151 if (CALLBACK_USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DevDescriptorPtr
152 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
153 , &MemoryAddressSpace
154 #endif
155 ) == NO_DESCRIPTOR)
156 {
157 return;
158 }
159
160 if (MemoryAddressSpace == MEMSPACE_FLASH)
161 {
162 if (((uint8_t)USB_ControlRequest.wValue > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
163 return;
164 }
165 else if (MemoryAddressSpace == MEMSPACE_EEPROM)
166 {
167 if (((uint8_t)USB_ControlRequest.wValue > eeprom_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
168 return;
169 }
170 else
171 {
172 if ((uint8_t)USB_ControlRequest.wValue > DevDescriptorPtr->NumberOfConfigurations)
173 return;
174 }
175 #endif
176
177 Endpoint_ClearSETUP();
178
179 USB_ConfigurationNumber = (uint8_t)USB_ControlRequest.wValue;
180
181 Endpoint_ClearStatusStage();
182
183 if (USB_ConfigurationNumber)
184 USB_DeviceState = DEVICE_STATE_Configured;
185 else
186 USB_DeviceState = (USB_Device_IsAddressSet()) ? DEVICE_STATE_Configured : DEVICE_STATE_Powered;
187
188 EVENT_USB_Device_ConfigurationChanged();
189 }
190
191 static void USB_Device_GetConfiguration(void)
192 {
193 Endpoint_ClearSETUP();
194
195 Endpoint_Write_Byte(USB_ConfigurationNumber);
196 Endpoint_ClearIN();
197
198 Endpoint_ClearStatusStage();
199 }
200
201 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
202 static void USB_Device_GetInternalSerialDescriptor(void)
203 {
204 struct
205 {
206 USB_Descriptor_Header_t Header;
207 uint16_t UnicodeString[20];
208 } SignatureDescriptor;
209
210 SignatureDescriptor.Header.Type = DTYPE_String;
211 SignatureDescriptor.Header.Size = USB_STRING_LEN(USB_Device_GetSerialString(SignatureDescriptor.UnicodeString,
212 sizeof(SignatureDescriptor.UnicodeString) / sizeof(SignatureDescriptor.UnicodeString[0])));
213
214 Endpoint_ClearSETUP();
215
216 Endpoint_Write_Control_Stream_LE(&SignatureDescriptor, sizeof(SignatureDescriptor));
217 Endpoint_ClearOUT();
218 }
219 #endif
220
221 static void USB_Device_GetDescriptor(void)
222 {
223 const void* DescriptorPointer;
224 uint16_t DescriptorSize;
225
226 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
227 uint8_t DescriptorAddressSpace;
228 #endif
229
230 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
231 if (USB_ControlRequest.wValue == ((DTYPE_String << 8) | USE_INTERNAL_SERIAL))
232 {
233 USB_Device_GetInternalSerialDescriptor();
234 return;
235 }
236 #endif
237
238 if ((DescriptorSize = CALLBACK_USB_GetDescriptor(USB_ControlRequest.wValue, USB_ControlRequest.wIndex,
239 &DescriptorPointer
240 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
241 , &DescriptorAddressSpace
242 #endif
243 )) == NO_DESCRIPTOR)
244 {
245 return;
246 }
247
248 Endpoint_ClearSETUP();
249
250 #if defined(USE_RAM_DESCRIPTORS)
251 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
252 #elif defined(USE_EEPROM_DESCRIPTORS)
253 Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
254 #elif defined(USE_FLASH_DESCRIPTORS)
255 Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
256 #else
257 if (DescriptorAddressSpace == MEMSPACE_FLASH)
258 Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
259 else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
260 Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
261 else
262 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
263 #endif
264
265 Endpoint_ClearOUT();
266 }
267
268 static void USB_Device_GetStatus(void)
269 {
270 uint8_t CurrentStatus = 0;
271
272 switch (USB_ControlRequest.bmRequestType)
273 {
274 #if !defined(NO_DEVICE_SELF_POWER) || !defined(NO_DEVICE_REMOTE_WAKEUP)
275 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE):
276 #if !defined(NO_DEVICE_SELF_POWER)
277 if (USB_CurrentlySelfPowered)
278 CurrentStatus |= FEATURE_SELFPOWERED_ENABLED;
279 #endif
280
281 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
282 if (USB_RemoteWakeupEnabled)
283 CurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
284 #endif
285 break;
286 #endif
287 #if !defined(CONTROL_ONLY_DEVICE)
288 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):
289 Endpoint_SelectEndpoint((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
290
291 CurrentStatus = Endpoint_IsStalled();
292
293 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
294
295 break;
296 #endif
297 default:
298 return;
299 }
300
301 Endpoint_ClearSETUP();
302
303 Endpoint_Write_Word_LE(CurrentStatus);
304 Endpoint_ClearIN();
305
306 Endpoint_ClearStatusStage();
307 }
308
309 static void USB_Device_ClearSetFeature(void)
310 {
311 switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)
312 {
313 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
314 case REQREC_DEVICE:
315 if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_DeviceRemoteWakeup)
316 USB_RemoteWakeupEnabled = (USB_ControlRequest.bRequest == REQ_SetFeature);
317 else
318 return;
319
320 break;
321 #endif
322 #if !defined(CONTROL_ONLY_DEVICE)
323 case REQREC_ENDPOINT:
324 if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_EndpointHalt)
325 {
326 uint8_t EndpointIndex = ((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
327
328 if (EndpointIndex == ENDPOINT_CONTROLEP)
329 return;
330
331 Endpoint_SelectEndpoint(EndpointIndex);
332
333 if (Endpoint_IsEnabled())
334 {
335 if (USB_ControlRequest.bRequest == REQ_SetFeature)
336 {
337 Endpoint_StallTransaction();
338 }
339 else
340 {
341 Endpoint_ClearStall();
342 Endpoint_ResetEndpoint(EndpointIndex);
343 Endpoint_ResetDataToggle();
344 }
345 }
346 }
347
348 break;
349 #endif
350 default:
351 return;
352 }
353
354 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
355
356 Endpoint_ClearSETUP();
357
358 Endpoint_ClearStatusStage();
359 }
360
361 #endif
362