Add CCID class driver and associated demos.
[pub/USBasp.git] / Demos / Device / ClassDriver / CCID / CCID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2018.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2018 Filipe Rodrigues (filipepazrodrigues [at] gmail [dot] com)
12
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.
21
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
29 this software.
30 */
31
32 /** \file
33 *
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.
36 *
37 * \warning
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.
42 *
43 * \warning
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.
49 */
50
51 #include "CCID.h"
52
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.
56 */
57 USB_ClassInfo_CCID_Device_t CCID_Interface =
58 {
59 .Config =
60 {
61 .InterfaceNumber = INTERFACE_ID_CCID,
62 .TotalSlots = 1,
63 .DataINEndpoint =
64 {
65 .Address = CCID_IN_EPADDR,
66 .Size = CCID_EPSIZE,
67 .Banks = 1,
68 },
69 .DataOUTEndpoint =
70 {
71 .Address = CCID_OUT_EPADDR,
72 .Size = CCID_EPSIZE,
73 .Banks = 1,
74 },
75 },
76 };
77
78
79 /** Main program entry point. This routine contains the overall program flow, including initial
80 * setup of all components and the main program loop.
81 */
82 int main(void)
83 {
84 SetupHardware();
85
86 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
87 GlobalInterruptEnable();
88
89 for (;;)
90 {
91 USB_USBTask();
92 CCID_Device_USBTask(&CCID_Interface);
93 }
94 }
95
96 /** Configures the board hardware and chip peripherals for the demo's functionality. */
97 void SetupHardware(void)
98 {
99 #if (ARCH == ARCH_AVR8)
100 /* Disable watchdog if enabled by bootloader/fuses */
101 MCUSR &= ~(1 << WDRF);
102 wdt_disable();
103
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);
110
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);
114
115 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
116 #endif
117
118 /* Hardware Initialization */
119 LEDs_Init();
120 USB_Init();
121 }
122
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)
125 {
126 /* Indicate USB enumerating */
127 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
128 }
129
130 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
131 * the status LEDs.
132 */
133 void EVENT_USB_Device_Disconnect(void)
134 {
135 /* Indicate USB not ready */
136 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
137 }
138
139 /** Event handler for the library USB Configuration Changed event. */
140 void EVENT_USB_Device_ConfigurationChanged(void)
141 {
142 bool ConfigSuccess = true;
143
144 ConfigSuccess &= CCID_Device_ConfigureEndpoints(&CCID_Interface);
145
146 /* Indicate endpoint configuration success or failure */
147 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
148 }
149
150 /** Event handler for the library USB Control Request reception event. */
151 void EVENT_USB_Device_ControlRequest(void)
152 {
153 CCID_Device_ProcessControlRequest(&CCID_Interface);
154 }
155
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)
159 */
160 uint8_t CALLBACK_CCID_IccPowerOn(uint8_t slot,
161 uint8_t* attr,
162 uint8_t* attrSize,
163 uint8_t* error)
164 {
165 if (slot < CCID_Interface.Config.TotalSlots)
166 {
167 Iso7816_CreateSimpleAtr(attr, attrSize);
168 *error = CCID_ERROR_NO_ERROR;
169 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
170 }
171
172 *error = CCID_ERROR_SLOT_NOT_FOUND;
173 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
174 }
175
176 /** Event handler for the CCID_PC_to_RDR_IccPowerOff message. This message is sent to the device
177 * whenever an application at the host wants to send a power off signal to a slot.
178 */
179 uint8_t CALLBACK_CCID_IccPowerOff(uint8_t slot, uint8_t* error)
180 {
181 if (slot < CCID_Interface.Config.TotalSlots)
182 {
183 *error = CCID_ERROR_NO_ERROR;
184 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_NOICCPRESENT;
185 }
186 else
187 {
188 *error = CCID_ERROR_SLOT_NOT_FOUND;
189 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
190 }
191 }
192
193 /** Event handler for the CCID_PC_to_RDR_GetSlotStatus. THis message is sent to the device
194 * whenever an application at the host wants to the get the current slot status
195 *
196 */
197 uint8_t CALLBACK_CCID_GetSlotStatus(uint8_t slot, uint8_t* error)
198 {
199 if (slot < CCID_Interface.Config.TotalSlots)
200 {
201 *error = CCID_ERROR_NO_ERROR;
202 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
203 }
204 else
205 {
206 *error = CCID_ERROR_SLOT_NOT_FOUND;
207 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
208 }
209 }
210
211 /** Event handler for the CCID_PC_to_RDR_XfrBlock. THis message is sent to the device
212 * whenever an application at the host wants to send a block of bytes to the device
213 * THe device reply back with an array of bytes
214 */
215 uint8_t CALLBACK_CCID_XfrBlock(uint8_t slot,
216 uint8_t* error,
217 uint8_t* receivedBuffer,
218 uint8_t receivedBufferSize,
219 uint8_t* sendBuffer,
220 uint8_t* sentBufferSize)
221 {
222 if (slot < CCID_Interface.Config.TotalSlots)
223 {
224 uint8_t okResponse[2] = {0x90, 0x00};
225 memcpy(sendBuffer, okResponse, sizeof(okResponse));
226 *sentBufferSize = sizeof(okResponse);
227
228 *error = CCID_ERROR_NO_ERROR;
229 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_NOICCPRESENT;
230 }
231 else
232 {
233 *error = CCID_ERROR_SLOT_NOT_FOUND;
234 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
235 }
236 }
237
238
239 uint8_t CALLBACK_CCID_Abort(uint8_t slot,
240 uint8_t seq,
241 uint8_t* error)
242 {
243 if (CCID_Interface.State.Aborted && slot == 0 && CCID_Interface.State.AbortedSeq == seq)
244 {
245 CCID_Interface.State.Aborted = false;
246 CCID_Interface.State.AbortedSeq = -1;
247 *error = CCID_ERROR_NO_ERROR;
248 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
249 }
250 else if (!CCID_Interface.State.Aborted)
251 {
252 *error = CCID_ERROR_CMD_NOT_ABORTED;
253 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
254 }
255 else if (slot != 0)
256 {
257 *error = CCID_ERROR_SLOT_NOT_FOUND;
258 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
259 }
260 else
261 {
262 *error = CCID_ERROR_NOT_SUPPORTED;
263 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
264 }
265 }