3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
34 #if defined(USB_CAN_BE_DEVICE)
36 #define __INCLUDE_FROM_DEVICESTDREQ_C
37 #include "DeviceStandardReq.h"
39 uint8_t USB_ConfigurationNumber
;
41 #if !defined(NO_DEVICE_SELF_POWER)
42 bool USB_CurrentlySelfPowered
;
45 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
46 bool USB_RemoteWakeupEnabled
;
49 void USB_Device_ProcessControlRequest(void)
51 uint8_t* RequestHeader
= (uint8_t*)&USB_ControlRequest
;
53 for (uint8_t RequestHeaderByte
= 0; RequestHeaderByte
< sizeof(USB_Request_Header_t
); RequestHeaderByte
++)
54 *(RequestHeader
++) = Endpoint_Read_Byte();
56 EVENT_USB_Device_ControlRequest();
58 if (Endpoint_IsSETUPReceived())
60 uint8_t bmRequestType
= USB_ControlRequest
.bmRequestType
;
62 switch (USB_ControlRequest
.bRequest
)
65 if ((bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
)) ||
66 (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_ENDPOINT
)))
68 USB_Device_GetStatus();
72 case REQ_ClearFeature
:
74 if ((bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
)) ||
75 (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_ENDPOINT
)))
77 USB_Device_ClearSetFeature();
82 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
))
83 USB_Device_SetAddress();
86 case REQ_GetDescriptor
:
87 if ((bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
)) ||
88 (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
)))
90 USB_Device_GetDescriptor();
94 case REQ_GetConfiguration
:
95 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
))
96 USB_Device_GetConfiguration();
99 case REQ_SetConfiguration
:
100 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
))
101 USB_Device_SetConfiguration();
107 if (Endpoint_IsSETUPReceived())
109 Endpoint_StallTransaction();
110 Endpoint_ClearSETUP();
114 static void USB_Device_SetAddress(void)
116 uint8_t DeviceAddress
= (USB_ControlRequest
.wValue
& 0x7F);
118 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
120 Endpoint_ClearSETUP();
122 Endpoint_ClearStatusStage();
124 while (!(Endpoint_IsINReady()));
126 USB_Device_SetDeviceAddress(DeviceAddress
);
129 USB_DeviceState
= (DeviceAddress
) ? DEVICE_STATE_Addressed
: DEVICE_STATE_Default
;
132 static void USB_Device_SetConfiguration(void)
134 #if defined(FIXED_NUM_CONFIGURATIONS)
135 if ((uint8_t)USB_ControlRequest
.wValue
> FIXED_NUM_CONFIGURATIONS
)
138 USB_Descriptor_Device_t
* DevDescriptorPtr
;
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
147 uint8_t MemoryAddressSpace
;
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
159 if (MemoryAddressSpace
== MEMSPACE_FLASH
)
161 if (((uint8_t)USB_ControlRequest
.wValue
> pgm_read_byte(&DevDescriptorPtr
->NumberOfConfigurations
)))
164 else if (MemoryAddressSpace
== MEMSPACE_EEPROM
)
166 if (((uint8_t)USB_ControlRequest
.wValue
> eeprom_read_byte(&DevDescriptorPtr
->NumberOfConfigurations
)))
171 if ((uint8_t)USB_ControlRequest
.wValue
> DevDescriptorPtr
->NumberOfConfigurations
)
176 Endpoint_ClearSETUP();
178 USB_ConfigurationNumber
= (uint8_t)USB_ControlRequest
.wValue
;
180 Endpoint_ClearStatusStage();
182 if (USB_ConfigurationNumber
)
183 USB_DeviceState
= DEVICE_STATE_Configured
;
185 USB_DeviceState
= (USB_Device_IsAddressSet()) ? DEVICE_STATE_Configured
: DEVICE_STATE_Powered
;
187 EVENT_USB_Device_ConfigurationChanged();
190 static void USB_Device_GetConfiguration(void)
192 Endpoint_ClearSETUP();
194 Endpoint_Write_Byte(USB_ConfigurationNumber
);
197 Endpoint_ClearStatusStage();
200 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
201 static void USB_Device_GetInternalSerialDescriptor(void)
205 USB_Descriptor_Header_t Header
;
206 wchar_t UnicodeString
[20];
207 } SignatureDescriptor
;
209 SignatureDescriptor
.Header
.Type
= DTYPE_String
;
210 SignatureDescriptor
.Header
.Size
= sizeof(SignatureDescriptor
);
212 ATOMIC_BLOCK(ATOMIC_RESTORESTATE
)
214 uint8_t SigReadAddress
= 0x0E;
216 for (uint8_t SerialCharNum
= 0; SerialCharNum
< 20; SerialCharNum
++)
218 uint8_t SerialByte
= boot_signature_byte_get(SigReadAddress
);
220 if (SerialCharNum
& 0x01)
228 SignatureDescriptor
.UnicodeString
[SerialCharNum
] = (SerialByte
>= 10) ?
229 (('A' - 10) + SerialByte
) : ('0' + SerialByte
);
233 Endpoint_ClearSETUP();
235 Endpoint_Write_Control_Stream_LE(&SignatureDescriptor
, sizeof(SignatureDescriptor
));
241 static void USB_Device_GetDescriptor(void)
243 const void* DescriptorPointer
;
244 uint16_t DescriptorSize
;
246 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
247 uint8_t DescriptorAddressSpace
;
250 #if !defined(NO_INTERNAL_SERIAL) && (USE_INTERNAL_SERIAL != NO_DESCRIPTOR)
251 if (USB_ControlRequest
.wValue
== ((DTYPE_String
<< 8) | USE_INTERNAL_SERIAL
))
253 USB_Device_GetInternalSerialDescriptor();
258 if ((DescriptorSize
= CALLBACK_USB_GetDescriptor(USB_ControlRequest
.wValue
, USB_ControlRequest
.wIndex
,
260 #if !defined(USE_FLASH_DESCRIPTORS) && !defined(USE_EEPROM_DESCRIPTORS) && !defined(USE_RAM_DESCRIPTORS)
261 , &DescriptorAddressSpace
268 Endpoint_ClearSETUP();
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
);
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
);
282 Endpoint_Write_Control_Stream_LE(DescriptorPointer
, DescriptorSize
);
288 static void USB_Device_GetStatus(void)
290 uint8_t CurrentStatus
= 0;
292 switch (USB_ControlRequest
.bmRequestType
)
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
;
301 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
302 if (USB_RemoteWakeupEnabled
)
303 CurrentStatus
|= FEATURE_REMOTE_WAKEUP_ENABLED
;
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
);
311 CurrentStatus
= Endpoint_IsStalled();
313 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
321 Endpoint_ClearSETUP();
323 Endpoint_Write_Word_LE(CurrentStatus
);
326 Endpoint_ClearStatusStage();
329 static void USB_Device_ClearSetFeature(void)
331 switch (USB_ControlRequest
.bmRequestType
& CONTROL_REQTYPE_RECIPIENT
)
333 #if !defined(NO_DEVICE_REMOTE_WAKEUP)
335 if ((uint8_t)USB_ControlRequest
.wValue
== FEATURE_SEL_DeviceRemoteWakeup
)
336 USB_RemoteWakeupEnabled
= (USB_ControlRequest
.bRequest
== REQ_SetFeature
);
342 #if !defined(CONTROL_ONLY_DEVICE)
343 case REQREC_ENDPOINT
:
344 if ((uint8_t)USB_ControlRequest
.wValue
== FEATURE_SEL_EndpointHalt
)
346 uint8_t EndpointIndex
= ((uint8_t)USB_ControlRequest
.wIndex
& ENDPOINT_EPNUM_MASK
);
348 if (EndpointIndex
== ENDPOINT_CONTROLEP
)
351 Endpoint_SelectEndpoint(EndpointIndex
);
353 if (Endpoint_IsEnabled())
355 if (USB_ControlRequest
.bRequest
== REQ_SetFeature
)
357 Endpoint_StallTransaction();
361 Endpoint_ClearStall();
362 Endpoint_ResetFIFO(EndpointIndex
);
363 Endpoint_ResetDataToggle();
374 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
376 Endpoint_ClearSETUP();
378 Endpoint_ClearStatusStage();