Add CCID class driver project description and Doxygen configuration files.
[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(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
161 const uint8_t slot,
162 uint8_t* const atr,
163 uint8_t* const attrSize,
164 uint8_t* const error)
165 {
166 if (slot < CCID_Interface.Config.TotalSlots)
167 {
168 Iso7816_CreateSimpleAtr(atr, attrSize);
169 *error = CCID_ERROR_NO_ERROR;
170 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
171 }
172 else
173 {
174 *error = CCID_ERROR_SLOT_NOT_FOUND;
175 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
176 }
177 }
178
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.
181 */
182 uint8_t CALLBACK_CCID_IccPowerOff(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
183 const uint8_t slot,
184 uint8_t* const error)
185 {
186 if (slot < CCID_Interface.Config.TotalSlots)
187 {
188 *error = CCID_ERROR_NO_ERROR;
189 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_NOICCPRESENT;
190 }
191 else
192 {
193 *error = CCID_ERROR_SLOT_NOT_FOUND;
194 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
195 }
196 }
197
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
200 *
201 */
202 uint8_t CALLBACK_CCID_GetSlotStatus(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
203 const uint8_t slot,
204 uint8_t* const error)
205 {
206 if (slot < CCID_Interface.Config.TotalSlots)
207 {
208 *error = CCID_ERROR_NO_ERROR;
209 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
210 }
211 else
212 {
213 *error = CCID_ERROR_SLOT_NOT_FOUND;
214 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
215 }
216 }
217
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
220 * given slot.
221 */
222 uint8_t CALLBACK_CCID_SetParameters_T0(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
223 const uint8_t Slot,
224 uint8_t* const Error,
225 USB_CCID_ProtocolData_T0_t* const T0)
226 {
227 if (Slot == 0)
228 {
229 // Set parameters
230 memcpy(&CCIDInterfaceInfo->ProtocolData, T0, sizeof(USB_CCID_ProtocolData_T0_t));
231
232 *Error = CCID_ERROR_NO_ERROR;
233 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
234 }
235 else
236 {
237 *Error = CCID_ERROR_SLOT_NOT_FOUND;
238 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
239 }
240 }
241
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
244 * a given slot.
245 */
246 uint8_t CALLBACK_CCID_GetParameters_T0(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
247 const uint8_t Slot,
248 uint8_t* const Error,
249 uint8_t* const ProtocolNum,
250 USB_CCID_ProtocolData_T0_t* const T0)
251 {
252 if (Slot == 0)
253 {
254 *ProtocolNum = CCID_PROTOCOLNUM_T0;
255 memcpy(T0, &CCIDInterfaceInfo->ProtocolData, sizeof(USB_CCID_ProtocolData_T0_t));
256
257 *Error = CCID_ERROR_NO_ERROR;
258 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
259 }
260 else
261 {
262 *Error = CCID_ERROR_SLOT_NOT_FOUND;
263 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
264 }
265 }
266
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
270 */
271 uint8_t CALLBACK_CCID_XfrBlock(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
272 const uint8_t Slot,
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)
278 {
279 if (Slot < CCID_Interface.Config.TotalSlots)
280 {
281 uint8_t OkResponse[2] = {0x90, 0x00};
282
283 memcpy(SendBuffer, OkResponse, sizeof(OkResponse));
284 *SentBufferSize = sizeof(OkResponse);
285
286 *Error = CCID_ERROR_NO_ERROR;
287 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_NOICCPRESENT;
288 }
289 else
290 {
291 *Error = CCID_ERROR_SLOT_NOT_FOUND;
292 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
293 }
294 }
295
296 uint8_t CALLBACK_CCID_Abort(USB_ClassInfo_CCID_Device_t* const CCIDInterfaceInfo,
297 const uint8_t Slot,
298 const uint8_t Seq,
299 uint8_t* const Error)
300 {
301 if (CCID_Interface.State.Aborted && Slot == 0 && CCID_Interface.State.AbortedSeq == Seq)
302 {
303 CCID_Interface.State.Aborted = false;
304 CCID_Interface.State.AbortedSeq = -1;
305
306 *Error = CCID_ERROR_NO_ERROR;
307 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
308 }
309 else if (!CCID_Interface.State.Aborted)
310 {
311 *Error = CCID_ERROR_CMD_NOT_ABORTED;
312 return CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR | CCID_ICCSTATUS_PRESENTANDACTIVE;
313 }
314 else if (Slot != 0)
315 {
316 *Error = CCID_ERROR_SLOT_NOT_FOUND;
317 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
318 }
319 else
320 {
321 *Error = CCID_ERROR_NOT_SUPPORTED;
322 return CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
323 }
324 }