3 Copyright (C) Dean Camera, 2018.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2018 Filipe Rodrigues (filipepazrodrigues [at] gmail [dot] com)
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
22 The author disclaims all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
34 * Main source file for the CCID demo. This file contains the main tasks of
35 * the demo and is responsible for the initial application hardware configuration.
38 * LUFA is not a secure USB stack, and has not undergone, not is it expected to pass, any
39 * form of security audit. The CCID class here is presented as-is and is intended for
40 * research purposes only, and *should not* be used in a security critical application
41 * under any circumstances.
44 * This code is not production ready and should not by any means be considered safe.
45 * If you plan to integrate it into your application, you should seriously consider strong
46 * encryption algorithms or a secure microprocessor. Since Atmel AVR microprocessors do not
47 * have any security requirement (therefore they don't offer any known protection against
48 * side channel attacks or fault injection) a secure microprocessor is the best option.
53 /** LUFA CCID Class driver interface configuration and state information. This structure is
54 * passed to all CCID Class driver functions, so that multiple instances of the same class
55 * within a device can be differentiated from one another.
57 USB_ClassInfo_CCID_Device_t CCID_Interface
=
61 .InterfaceNumber
= INTERFACE_ID_CCID
,
65 .Address
= CCID_IN_EPADDR
,
71 .Address
= CCID_OUT_EPADDR
,
79 /** Main program entry point. This routine contains the overall program flow, including initial
80 * setup of all components and the main program loop.
86 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
87 GlobalInterruptEnable();
92 CCID_Device_USBTask(&CCID_Interface
);
96 /** Configures the board hardware and chip peripherals for the demo's functionality. */
97 void SetupHardware(void)
99 #if (ARCH == ARCH_AVR8)
100 /* Disable watchdog if enabled by bootloader/fuses */
101 MCUSR
&= ~(1 << WDRF
);
104 /* Disable clock division */
105 clock_prescale_set(clock_div_1
);
106 #elif (ARCH == ARCH_XMEGA)
107 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
108 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
);
109 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
);
111 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
112 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
);
113 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
);
115 PMIC
.CTRL
= PMIC_LOLVLEN_bm
| PMIC_MEDLVLEN_bm
| PMIC_HILVLEN_bm
;
118 /* Hardware Initialization */
123 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */
124 void EVENT_USB_Device_Connect(void)
126 /* Indicate USB enumerating */
127 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
130 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
133 void EVENT_USB_Device_Disconnect(void)
135 /* Indicate USB not ready */
136 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
139 /** Event handler for the library USB Configuration Changed event. */
140 void EVENT_USB_Device_ConfigurationChanged(void)
142 bool ConfigSuccess
= true;
144 ConfigSuccess
&= CCID_Device_ConfigureEndpoints(&CCID_Interface
);
146 /* Indicate endpoint configuration success or failure */
147 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
150 /** Event handler for the library USB Control Request reception event. */
151 void EVENT_USB_Device_ControlRequest(void)
153 CCID_Device_ProcessControlRequest(&CCID_Interface
);
156 /** Event handler for the CCID_PC_to_RDR_IccPowerOn message. This message is sent to the device
157 * whenever an application at the host wants to send a power off signal to a slot.
158 * THe slot must reply back with a recognizable ATR (answer to reset)
160 uint8_t CALLBACK_CCID_IccPowerOn(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
163 uint8_t* const attrSize
,
164 uint8_t* const error
)
166 if (slot
< CCID_Interface
.Config
.TotalSlots
)
168 Iso7816_CreateSimpleAtr(atr
, attrSize
);
169 *error
= CCID_ERROR_NO_ERROR
;
170 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_PRESENTANDACTIVE
;
174 *error
= CCID_ERROR_SLOT_NOT_FOUND
;
175 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
179 /** Event handler for the CCID_PC_to_RDR_IccPowerOff message. This message is sent to the device
180 * whenever an application at the host wants to send a power off signal to a slot.
182 uint8_t CALLBACK_CCID_IccPowerOff(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
184 uint8_t* const error
)
186 if (slot
< CCID_Interface
.Config
.TotalSlots
)
188 *error
= CCID_ERROR_NO_ERROR
;
189 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_NOICCPRESENT
;
193 *error
= CCID_ERROR_SLOT_NOT_FOUND
;
194 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
198 /** Event handler for the CCID_PC_to_RDR_GetSlotStatus. This message is sent to the device
199 * whenever an application at the host wants to the get the current slot status
202 uint8_t CALLBACK_CCID_GetSlotStatus(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
204 uint8_t* const error
)
206 if (slot
< CCID_Interface
.Config
.TotalSlots
)
208 *error
= CCID_ERROR_NO_ERROR
;
209 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_PRESENTANDACTIVE
;
213 *error
= CCID_ERROR_SLOT_NOT_FOUND
;
214 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
218 /** Event handler for the CCID_PC_to_RDR_SetParameters when T=0. This message is sent to
219 * the device whenever an application at the host wants to set the parameters for a
222 uint8_t CALLBACK_CCID_SetParameters_T0(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
224 uint8_t* const Error
,
225 USB_CCID_ProtocolData_T0_t
* const T0
)
230 memcpy(&CCIDInterfaceInfo
->ProtocolData
, T0
, sizeof(USB_CCID_ProtocolData_T0_t
));
232 *Error
= CCID_ERROR_NO_ERROR
;
233 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_PRESENTANDACTIVE
;
237 *Error
= CCID_ERROR_SLOT_NOT_FOUND
;
238 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
242 /** Event handler for the CCID_PC_to_RDR_GetParameters when T=0. This message is sent to
243 * the device whenever an application at the host wants to get the current parameters for
246 uint8_t CALLBACK_CCID_GetParameters_T0(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
248 uint8_t* const Error
,
249 uint8_t* const ProtocolNum
,
250 USB_CCID_ProtocolData_T0_t
* const T0
)
254 *ProtocolNum
= CCID_PROTOCOLNUM_T0
;
255 memcpy(T0
, &CCIDInterfaceInfo
->ProtocolData
, sizeof(USB_CCID_ProtocolData_T0_t
));
257 *Error
= CCID_ERROR_NO_ERROR
;
258 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_PRESENTANDACTIVE
;
262 *Error
= CCID_ERROR_SLOT_NOT_FOUND
;
263 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
267 /** Event handler for the CCID_PC_to_RDR_XfrBlock. This message is sent to the device
268 * whenever an application at the host wants to send a block of bytes to the device
269 * THe device reply back with an array of bytes
271 uint8_t CALLBACK_CCID_XfrBlock(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
273 const uint8_t* ReceivedBuffer
,
274 const uint8_t ReceivedBufferSize
,
275 uint8_t* const SendBuffer
,
276 uint8_t* const SentBufferSize
,
277 uint8_t* const Error
)
279 if (Slot
< CCID_Interface
.Config
.TotalSlots
)
281 uint8_t OkResponse
[2] = {0x90, 0x00};
283 memcpy(SendBuffer
, OkResponse
, sizeof(OkResponse
));
284 *SentBufferSize
= sizeof(OkResponse
);
286 *Error
= CCID_ERROR_NO_ERROR
;
287 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_NOICCPRESENT
;
291 *Error
= CCID_ERROR_SLOT_NOT_FOUND
;
292 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
296 uint8_t CALLBACK_CCID_Abort(USB_ClassInfo_CCID_Device_t
* const CCIDInterfaceInfo
,
299 uint8_t* const Error
)
301 if (CCID_Interface
.State
.Aborted
&& Slot
== 0 && CCID_Interface
.State
.AbortedSeq
== Seq
)
303 CCID_Interface
.State
.Aborted
= false;
304 CCID_Interface
.State
.AbortedSeq
= -1;
306 *Error
= CCID_ERROR_NO_ERROR
;
307 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_PRESENTANDACTIVE
;
309 else if (!CCID_Interface
.State
.Aborted
)
311 *Error
= CCID_ERROR_CMD_NOT_ABORTED
;
312 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR
| CCID_ICCSTATUS_PRESENTANDACTIVE
;
316 *Error
= CCID_ERROR_SLOT_NOT_FOUND
;
317 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;
321 *Error
= CCID_ERROR_NOT_SUPPORTED
;
322 return CCID_COMMANDSTATUS_FAILED
| CCID_ICCSTATUS_NOICCPRESENT
;