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