Fix documentation glitches from the restructuring changes.
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / 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 = sizeof(SignatureDescriptor);
211
212 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
213 {
214 uint8_t SigReadAddress = 0x0E;
215
216 for (uint8_t SerialCharNum = 0; SerialCharNum < 20; SerialCharNum++)
217 {
218 uint8_t SerialByte = boot_signature_byte_get(SigReadAddress);
219
220 if (SerialCharNum & 0x01)
221 {
222 SerialByte >>= 4;
223 SigReadAddress++;
224 }
225
226 SerialByte &= 0x0F;
227
228 SignatureDescriptor.UnicodeString[SerialCharNum] = (SerialByte >= 10) ?
229 (('A' - 10) + SerialByte) : ('0' + SerialByte);
230 }
231 }
232
233 Endpoint_ClearSETUP();
234
235 Endpoint_Write_Control_Stream_LE(&SignatureDescriptor, sizeof(SignatureDescriptor));
236
237 Endpoint_ClearOUT();
238 }
239 #endif
240
241 static void USB_Device_GetDescriptor(void)
242 {
243 const void* DescriptorPointer;
244 uint16_t DescriptorSize;
245
246 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
247 uint8_t DescriptorAddressSpace;
248 #endif
249
250 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
251 if (USB_ControlRequest.wValue == ((DTYPE_String << 8) | USE_INTERNAL_SERIAL))
252 {
253 USB_Device_GetInternalSerialDescriptor();
254 return;
255 }
256 #endif
257
258 if ((DescriptorSize = CALLBACK_USB_GetDescriptor(USB_ControlRequest.wValue, USB_ControlRequest.wIndex,
259 &DescriptorPointer
260 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
261 , &DescriptorAddressSpace
262 #endif
263 )) == NO_DESCRIPTOR)
264 {
265 return;
266 }
267
268 Endpoint_ClearSETUP();
269
270 #if defined(USE_RAM_DESCRIPTORS)
271 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
272 #elif defined(USE_EEPROM_DESCRIPTORS)
273 Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
274 #elif defined(USE_FLASH_DESCRIPTORS)
275 Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
276 #else
277 if (DescriptorAddressSpace == MEMSPACE_FLASH)
278 Endpoint_Write_Control_PStream_LE(DescriptorPointer, DescriptorSize);
279 else if (DescriptorAddressSpace == MEMSPACE_EEPROM)
280 Endpoint_Write_Control_EStream_LE(DescriptorPointer, DescriptorSize);
281 else
282 Endpoint_Write_Control_Stream_LE(DescriptorPointer, DescriptorSize);
283 #endif
284
285 Endpoint_ClearOUT();
286 }
287
288 static void USB_Device_GetStatus(void)
289 {
290 uint8_t CurrentStatus = 0;
291
292 switch (USB_ControlRequest.bmRequestType)
293 {
294 #if !defined(NO_DEVICE_SELF_POWER) || !defined(NO_DEVICE_REMOTE_WAKEUP)
295 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE):
296 #if !defined(NO_DEVICE_SELF_POWER)
297 if (USB_CurrentlySelfPowered)
298 CurrentStatus |= FEATURE_SELFPOWERED_ENABLED;
299 #endif
300
301 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
302 if (USB_RemoteWakeupEnabled)
303 CurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
304 #endif
305 break;
306 #endif
307 #if !defined(CONTROL_ONLY_DEVICE)
308 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):
309 Endpoint_SelectEndpoint((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
310
311 CurrentStatus = Endpoint_IsStalled();
312
313 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
314
315 break;
316 #endif
317 default:
318 return;
319 }
320
321 Endpoint_ClearSETUP();
322
323 Endpoint_Write_Word_LE(CurrentStatus);
324 Endpoint_ClearIN();
325
326 Endpoint_ClearStatusStage();
327 }
328
329 static void USB_Device_ClearSetFeature(void)
330 {
331 switch (USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT)
332 {
333 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
334 case REQREC_DEVICE:
335 if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_DeviceRemoteWakeup)
336 USB_RemoteWakeupEnabled = (USB_ControlRequest.bRequest == REQ_SetFeature);
337 else
338 return;
339
340 break;
341 #endif
342 #if !defined(CONTROL_ONLY_DEVICE)
343 case REQREC_ENDPOINT:
344 if ((uint8_t)USB_ControlRequest.wValue == FEATURE_SEL_EndpointHalt)
345 {
346 uint8_t EndpointIndex = ((uint8_t)USB_ControlRequest.wIndex & ENDPOINT_EPNUM_MASK);
347
348 if (EndpointIndex == ENDPOINT_CONTROLEP)
349 return;
350
351 Endpoint_SelectEndpoint(EndpointIndex);
352
353 if (Endpoint_IsEnabled())
354 {
355 if (USB_ControlRequest.bRequest == REQ_SetFeature)
356 {
357 Endpoint_StallTransaction();
358 }
359 else
360 {
361 Endpoint_ClearStall();
362 Endpoint_ResetFIFO(EndpointIndex);
363 Endpoint_ResetDataToggle();
364 }
365 }
366 }
367
368 break;
369 #endif
370 default:
371 return;
372 }
373
374 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
375
376 Endpoint_ClearSETUP();
377
378 Endpoint_ClearStatusStage();
379 }
380
381 #endif
382