blob: c3867102d06ffb1ffc8683d3f0ad417631eb0e53 [file] [log] [blame]
Christina Quastdb7b1ab2015-03-03 12:34:36 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2008, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30//------------------------------------------------------------------------------
31/// \unit
32///
33/// !Purpose
34///
35/// CCID driver
36///
37/// !Usage
38///
39/// Explanation on the usage of the code made available through the header file.
40//------------------------------------------------------------------------------
41
42//------------------------------------------------------------------------------
43// Headers
44//------------------------------------------------------------------------------
45
46#include "board.h"
47#include <USBDDriver.h>
48#include <USBRequests.h>
49#include <USBDescriptors.h>
50//#include <usb/device/dfu/dfu.h>
51#include <cciddriverdescriptors.h>
52
53// FIXME: Remove DFU related stuff
54/* no DFU bootloader is being used */
55#define DFU_NUM_IF 0
56#define DFU_IF_DESCRIPTORS_STRUCT
57#define DFU_IF_DESCRIPTORS
58
59#define DFU_NUM_STRINGS 0
60#define DFU_STRING_DESCRIPTORS
61
62
63//------------------------------------------------------------------------------
64// Local definition
65//------------------------------------------------------------------------------
66
67/// Constants: IDs: Device product ID.
68//#define CCIDDriverDescriptors_PRODUCTID 0x6129
69#define CCIDDriverDescriptors_PRODUCTID SIMTRACE_PRODUCT_ID
70/// Constants: IDs: Device vendor ID.
71#define CCIDDriverDescriptors_VENDORID ATMEL_VENDOR_ID
72//#define CCIDDriverDescriptors_VENDORID 0x03EB
73/// Constants: IDs: Device release number.
74#define CCIDDriverDescriptors_RELEASE 0x0100
75
76/// Returns the minimum between two values.
77#define MIN(a, b) ((a < b) ? a : b)
78
79//------------------------------------------------------------------------------
80// Types
81//------------------------------------------------------------------------------
82
83/// Driver structure for an CCID device
84typedef struct {
85
86 /// Standard USB device driver instance
87 USBDDriver usbdDriver;
88 /// CCID message
89 S_ccid_bulk_in_header sCcidMessage;
90 /// CCID command
91 S_ccid_bulk_out_header sCcidCommand;
92 /// Interrupt message answer
93 unsigned char BufferINT[4];
94 /// Buffer data of message
95 unsigned char ProtocolDataStructure[10];
96 /// Protocol used
97 unsigned char bProtocol;
98 /// SlotStatus
99 /// Bit 0 = Slot 0 current state
100 /// Bit 1 = Slot 0 changed status
101 /// Bit 2 = Slot 1 current state
102 /// Bit 3 = Slot 1 changed status
103 /// Bit 4 = Slot 2 current state
104 /// Bit 5 = Slot 2 changed status
105 unsigned char SlotStatus;
106
107} CCIDDriver;
108
109//------------------------------------------------------------------------------
110// Local variables
111//------------------------------------------------------------------------------
112
113/// Static instance of the CCID device driver.
114static CCIDDriver ccidDriver;
115static CCIDDriverConfigurationDescriptors *configurationDescriptorsFS;
116
117//------------------------------------------------------------------------------
118// Internal functions
119//------------------------------------------------------------------------------
120
121//------------------------------------------------------------------------------
122/// Initializes the CCID device driver.
123//------------------------------------------------------------------------------
124void CCIDDriver_Initialize( void )
125{
126 configurationDescriptorsFS = getConfigDesc(CFG_NUM_CCID);
127}
128
129//------------------------------------------------------------------------------
130/// Response Pipe, Bulk-IN Messages
131/// Return the Slot Status to the host
132/// Answer to:
133/// PC_to_RDR_IccPowerOff
134/// PC_to_RDR_GetSlotStatus
135/// PC_to_RDR_IccClock
136/// PC_to_RDR_T0APDU
137/// PC_to_RDR_Mechanical
138/// PC_to_RDR_Abort and Class specific ABORT request
139//------------------------------------------------------------------------------
140static void RDRtoPCSlotStatus( void )
141{
142 TRACE_DEBUG(".");
143
144 // Header fields settings
145 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_SLOTSTATUS;
146 ccidDriver.sCcidMessage.wLength = 0;
147 ccidDriver.sCcidMessage.bStatus = ccidDriver.SlotStatus;
148 ccidDriver.sCcidMessage.bError = 0;
149 // 00h Clock running
150 // 01h Clock stopped in state L
151 // 02h Clock stopped in state H
152 // 03h Clock stopped in an unknown state
153 // All other values are Reserved for Future Use.
154 ccidDriver.sCcidMessage.bSpecific = 0;
155}
156
157//------------------------------------------------------------------------------
158/// Response Pipe, Bulk-IN Messages
159/// Answer to PC_to_RDR_IccPowerOn
160//------------------------------------------------------------------------------
161static void RDRtoPCDatablock_ATR( void )
162{
163 unsigned char i;
164 unsigned char Atr[ATR_SIZE_MAX];
165 unsigned char length;
166 uint32_t status;
167
168 TRACE_DEBUG(".");
169
Christina Quast1a224af2015-03-10 15:11:37 +0100170 status = ISO7816_Datablock_ATR( Atr, &length );
Christina Quastb58434e2015-03-09 17:13:07 +0100171// ISO7816_Decode_ATR( Atr );
172
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100173 if (status == 0) {
174 TRACE_DEBUG("Timeout occured while reading ATR");
175// FIXME: react properly to timeout..
176// return;
177 }
178
Christina Quastb58434e2015-03-09 17:13:07 +0100179// FIXME: More tests? Is bProtocol = Atr[3] ?
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100180 if( length > 5 ) {
Christina Quastb58434e2015-03-09 17:13:07 +0100181 ccidDriver.ProtocolDataStructure[1] = Atr[3]&0x0F; // TD(1)
182 ccidDriver.bProtocol = Atr[3]&0x0F; // TD(1)
183 TRACE_INFO("Protocol data structure: 0x%x, bProtocol: 0x%x\n\r",
184 ccidDriver.ProtocolDataStructure[1], ccidDriver.bProtocol);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100185 }
186
187 // S_ccid_protocol_t0
188 // bmFindexDindex
189 ccidDriver.ProtocolDataStructure[0] = Atr[2]; // TA(1)
190
191 // bmTCCKST0
192 // For T=0 ,B0 – 0b, B7-2 – 000000b
193 // B1 – Convention used (b1=0 for direct, b1=1 for inverse)
194
195 // bGuardTimeT0
196 // Extra Guardtime between two characters. Add 0 to 254 etu to the normal
197 // guardtime of 12etu. FFh is the same as 00h.
198 ccidDriver.ProtocolDataStructure[2] = Atr[4]; // TC(1)
199 // AT91C_BASE_US0->US_TTGR = 0; // TC1
200
201 // bWaitingIntegerT0
202 // WI for T=0 used to define WWT
203 ccidDriver.ProtocolDataStructure[3] = Atr[7]; // TC(2)
204
205 // bClockStop
206 // ICC Clock Stop Support
207 // 00 = Stopping the Clock is not allowed
208 // 01 = Stop with Clock signal Low
209 // 02 = Stop with Clock signal High
210 // 03 = Stop with Clock either High or Low
211 ccidDriver.ProtocolDataStructure[4] = 0x00; // 0 to 3
212
213 // Header fields settings
214 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_DATABLOCK;
215 ccidDriver.sCcidMessage.wLength = length; // Size of ATR
216 ccidDriver.sCcidMessage.bSizeToSend += length; // Size of ATR
217 // bChainParameter: 00 the response APDU begins and ends in this command
218 ccidDriver.sCcidMessage.bSpecific = 0;
219
220 for( i=0; i<length; i++ ) {
221
222 ccidDriver.sCcidMessage.abData[i] = Atr[i];
223 }
224
225 // Set the slot to an active status
226 ccidDriver.sCcidMessage.bStatus = 0;
227 ccidDriver.sCcidMessage.bError = 0;
228}
229
230//------------------------------------------------------------------------------
231/// Response Pipe, Bulk-IN Messages
232/// In other cases, the response message has the following format:
233/// The response data will contain the optional data returned by the ICC,
234/// followed by the 2 byte-size status words SW1-SW2.
235///
236/// Answer to:
237/// PC_to_RDR_XfrBlock
238/// PC_to_RDR_Secure
239//------------------------------------------------------------------------------
240static void RDRtoPCDatablock( void )
241{
242 //TRACE_DEBUG(".");
243
244 // Header fields settings
245 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_DATABLOCK;
246 ccidDriver.sCcidMessage.bSizeToSend += ccidDriver.sCcidMessage.wLength;
247 // bChainParameter: 00 the response APDU begins and ends in this command
248 ccidDriver.sCcidMessage.bSpecific = 0;
249
250 // Set the slot to an active status
251 ccidDriver.sCcidMessage.bStatus = 0;
252 ccidDriver.sCcidMessage.bError = 0;
253}
254
255//------------------------------------------------------------------------------
256/// Response Pipe, Bulk-IN Messages
257/// Answer to:
258/// PC_to_RDR_GetParameters
259/// PC_to_RDR_ResetParameters
260/// PC_to_RDR_SetParameters
261//------------------------------------------------------------------------------
262static void RDRtoPCParameters( void )
263{
264 unsigned int i;
265
266 TRACE_DEBUG(".");
267
268 // Header fields settings
269 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_PARAMETERS;
270
271 //ccidDriver.sCcidMessage.bStatus = 0;
272 ccidDriver.sCcidMessage.bError = 0;
273
274 if( ccidDriver.ProtocolDataStructure[1] == PROTOCOL_TO ) {
275
276 // T=0
277 ccidDriver.sCcidMessage.wLength = sizeof(S_ccid_protocol_t0);
278 ccidDriver.sCcidMessage.bSpecific = PROTOCOL_TO;
279 }
280 else {
281
282 // T=1
283 ccidDriver.sCcidMessage.wLength = sizeof(S_ccid_protocol_t1);
284 ccidDriver.sCcidMessage.bSpecific = PROTOCOL_T1;
285 }
286
287 ccidDriver.sCcidMessage.bSizeToSend += ccidDriver.sCcidMessage.wLength;
288
289 for( i=0; i<ccidDriver.sCcidMessage.wLength; i++ ) {
290 ccidDriver.sCcidMessage.abData[i] = ccidDriver.ProtocolDataStructure[i];
291 }
292
293}
294
295//------------------------------------------------------------------------------
296/// Response Pipe, Bulk-IN Messages
297/// Answer to:
298/// PC_to_RDR_Escape
299//------------------------------------------------------------------------------
300static void RDRtoPCEscape( unsigned char length, unsigned char *data_send_from_CCID )
301{
302 unsigned int i;
303
304 TRACE_DEBUG(".");
305
306 // Header fields settings
307 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_ESCAPE;
308
309 ccidDriver.sCcidMessage.wLength = length;
310
311 ccidDriver.sCcidMessage.bStatus = 0;
312 ccidDriver.sCcidMessage.bError = 0;
313
314 ccidDriver.sCcidMessage.bSpecific = 0; // bRFU
315
316 for( i=0; i<length; i++ ) {
317 ccidDriver.sCcidMessage.abData[i] = data_send_from_CCID[i];
318 }
319}
320
321//------------------------------------------------------------------------------
322/// Response Pipe, Bulk-IN Messages
323/// Answer to:
324/// PC_to_RDR_SetDataRateAndClockFrequency
325//------------------------------------------------------------------------------
326static void RDRtoPCDataRateAndClockFrequency( unsigned int dwClockFrequency,
327 unsigned int dwDataRate )
328{
329 TRACE_DEBUG(".");
330
331 // Header fields settings
332 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_DATARATEANDCLOCKFREQUENCY;
333
334 ccidDriver.sCcidMessage.wLength = 8;
335
336 ccidDriver.sCcidMessage.bStatus = 0;
337 ccidDriver.sCcidMessage.bError = 0;
338
339 ccidDriver.sCcidMessage.bSpecific = 0; // bRFU
340
341 ccidDriver.sCcidMessage.abData[0] = dwClockFrequency;
342
343 ccidDriver.sCcidMessage.abData[4] = dwDataRate;
344}
345
346//------------------------------------------------------------------------------
347/// Command Pipe, Bulk-OUT Messages
348/// Power On Command - Cold Reset & Warm Reset
349/// Return the ATR to the host
350//------------------------------------------------------------------------------
351static void PCtoRDRIccPowerOn( void )
352{
353 TRACE_DEBUG(".");
354 if( CCID_FEATURES_AUTO_VOLT == (configurationDescriptorsFS->ccid.dwFeatures & CCID_FEATURES_AUTO_VOLT) ) {
355
356 //bPowerSelect = ccidDriver.sCcidCommand.bSpecific_0;
357 ccidDriver.sCcidCommand.bSpecific_0 = VOLTS_AUTO;
358 }
359
360 ISO7816_warm_reset();
361// ISO7816_cold_reset();
362
363 // for emulation only //JCB
364 if ( ccidDriver.sCcidCommand.bSpecific_0 != VOLTS_5_0 ) {
365
366 TRACE_ERROR("POWER_NOT_SUPPORTED\n\r");
367 }
368
369 else {
370
371 RDRtoPCDatablock_ATR();
372
373 }
374}
375
376//------------------------------------------------------------------------------
377/// Command Pipe, Bulk-OUT Messages
378/// Power Off Command - Set the ICC in an inactive state
379/// Return the slot status to the host
380//------------------------------------------------------------------------------
381static void PCtoRDRIccPowerOff( void )
382{
383 unsigned char bStatus;
384
385 TRACE_DEBUG(".");
386
387 ISO7816_IccPowerOff();
388
389 //JCB stub
390 bStatus = ICC_BS_PRESENT_NOTACTIVATED;
391
392 // Set the slot to an inactive status
393 ccidDriver.sCcidMessage.bStatus = 0;
394 ccidDriver.sCcidMessage.bError = 0;
395
396 // if error, see Table 6.1-2 errors
397
398 // Return the slot status to the host
399 RDRtoPCSlotStatus();
400}
401
402//------------------------------------------------------------------------------
403/// Command Pipe, Bulk-OUT Messages
404/// Get slot status
405//------------------------------------------------------------------------------
406static void PCtoRDRGetSlotStatus( void )
407{
408 TRACE_DEBUG(".");
409
410 ccidDriver.sCcidMessage.bStatus = 0;
411 ccidDriver.sCcidMessage.bError = 0;
412
413 // Return the slot status to the host
414 RDRtoPCSlotStatus();
415}
416
417//------------------------------------------------------------------------------
418/// Command Pipe, Bulk-OUT Messages
419/// If the command header is valid, an APDU command is received and can be read
420/// by the application
421//------------------------------------------------------------------------------
422static void PCtoRDRXfrBlock( void )
423{
424 unsigned char indexMessage = 0;
425
426 //TRACE_DEBUG(".");
427
428 // Check the block length
429 if ( ccidDriver.sCcidCommand.wLength > (configurationDescriptorsFS->ccid.dwMaxCCIDMessageLength-10) ) {
430
431 ccidDriver.sCcidMessage.bStatus = 1;
432 ccidDriver.sCcidMessage.bError = 0;
433 }
434 // check bBWI
435 else if ( 0 != ccidDriver.sCcidCommand.bSpecific_0 ) {
436
437 TRACE_ERROR("Bad bBWI\n\r");
438 }
439 else {
440
441 // APDU or TPDU
442 switch(configurationDescriptorsFS->ccid.dwFeatures
443 & (CCID_FEATURES_EXC_TPDU|CCID_FEATURES_EXC_SAPDU|CCID_FEATURES_EXC_APDU)) {
444
445 case CCID_FEATURES_EXC_TPDU:
446 if (ccidDriver.ProtocolDataStructure[1] == PROTOCOL_TO) {
Christina Quastb58434e2015-03-09 17:13:07 +0100447 TRACE_INFO("APDU cmd: %x %x %x ..", ccidDriver.sCcidCommand.APDU[0], ccidDriver.sCcidCommand.APDU[1],ccidDriver.sCcidCommand.APDU[2] );
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100448
449 // Send commande APDU
450 indexMessage = ISO7816_XfrBlockTPDU_T0( ccidDriver.sCcidCommand.APDU ,
451 ccidDriver.sCcidMessage.abData,
452 ccidDriver.sCcidCommand.wLength );
453 }
454 else {
455 if (ccidDriver.ProtocolDataStructure[1] == PROTOCOL_T1) {
456 TRACE_INFO("Not supported T=1\n\r");
457 }
458 else {
Christina Quastb58434e2015-03-09 17:13:07 +0100459 TRACE_INFO("Not supported 0x%x\n\r", ccidDriver.ProtocolDataStructure[1]);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100460 }
461 }
462 break;
463
464 case CCID_FEATURES_EXC_APDU:
Christina Quastb58434e2015-03-09 17:13:07 +0100465 TRACE_INFO("Not supported CCID_FEATURES_EXC_APDU\n\r");
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100466 break;
467
468 default:
469 break;
470 }
471
472 }
473
474 ccidDriver.sCcidMessage.wLength = indexMessage;
475 TRACE_DEBUG("USB: 0x%X, 0x%X, 0x%X, 0x%X, 0x%X\n\r", ccidDriver.sCcidMessage.abData[0],
476 ccidDriver.sCcidMessage.abData[1],
477 ccidDriver.sCcidMessage.abData[2],
478 ccidDriver.sCcidMessage.abData[3],
479 ccidDriver.sCcidMessage.abData[4] );
480 RDRtoPCDatablock();
481
482}
483
484//------------------------------------------------------------------------------
485/// Command Pipe, Bulk-OUT Messages
486/// return parameters by the command: RDR_to_PC_Parameters
487//------------------------------------------------------------------------------
488static void PCtoRDRGetParameters( void )
489{
490 TRACE_DEBUG(".");
491
492 // We support only one slot
493
494 // bmIccStatus
495 if( ISO7816_StatusReset() ) {
496 // 0: An ICC is present and active (power is on and stable, RST is inactive
497 ccidDriver.sCcidMessage.bStatus = 0;
498 }
499 else {
500 // 1: An ICC is present and inactive (not activated or shut down by hardware error)
501 ccidDriver.sCcidMessage.bStatus = 1;
502 }
503
504 RDRtoPCParameters();
505}
506
507//------------------------------------------------------------------------------
508/// Command Pipe, Bulk-OUT Messages
509/// This command resets the slot parameters to their default values
510//------------------------------------------------------------------------------
511static void PCtoRDRResetParameters( void )
512{
513 TRACE_DEBUG(".");
514
515 ccidDriver.SlotStatus = ICC_NOT_PRESENT;
516 ccidDriver.sCcidMessage.bStatus = ccidDriver.SlotStatus;
517
518 RDRtoPCParameters();
519}
520
521//------------------------------------------------------------------------------
522/// Command Pipe, Bulk-OUT Messages
523/// This command is used to change the parameters for a given slot.
524//------------------------------------------------------------------------------
525static void PCtoRDRSetParameters( void )
526{
527 TRACE_DEBUG(".");
528
529 ccidDriver.SlotStatus = ccidDriver.sCcidCommand.bSlot;
530 ccidDriver.sCcidMessage.bStatus = ccidDriver.SlotStatus;
531 // Not all feature supported
532
533 RDRtoPCParameters();
534}
535
536//------------------------------------------------------------------------------
537/// Command Pipe, Bulk-OUT Messages
538/// This command allows the CCID manufacturer to define and access extended
539/// features.
540/// Information sent via this command is processed by the CCID control logic.
541//------------------------------------------------------------------------------
542static void PCtoRDREscape( void )
543{
544 TRACE_DEBUG(".");
545
546 // If needed by the user
547 ISO7816_Escape();
548
549 // stub, return all value send
550 RDRtoPCEscape( ccidDriver.sCcidCommand.wLength, ccidDriver.sCcidCommand.APDU);
551}
552
553//------------------------------------------------------------------------------
554/// Command Pipe, Bulk-OUT Messages
555/// This command stops or restarts the clock.
556//------------------------------------------------------------------------------
557static void PCtoRDRICCClock( void )
558{
559 TRACE_DEBUG(".");
560
561 if( 0 == ccidDriver.sCcidCommand.bSpecific_0 ) {
562 // restarts the clock
563 ISO7816_RestartClock();
564 }
565 else {
566 // stop clock in the state shown in the bClockStop field
567 ISO7816_StopClock();
568 }
569
570 RDRtoPCSlotStatus( );
571}
572
573//------------------------------------------------------------------------------
574/// Command Pipe, Bulk-OUT Messages
575/// This command changes the parameters used to perform the transportation of
576/// APDU messages by the T=0 protocol.
577//------------------------------------------------------------------------------
578static void PCtoRDRtoAPDU( void )
579{
580 unsigned char bmChanges;
581 unsigned char bClassGetResponse;
582 unsigned char bClassEnvelope;
583
584 TRACE_DEBUG(".");
585
586 if( configurationDescriptorsFS->ccid.dwFeatures == (CCID_FEATURES_EXC_SAPDU|CCID_FEATURES_EXC_APDU) ) {
587
588 bmChanges = ccidDriver.sCcidCommand.bSpecific_0;
589 bClassGetResponse = ccidDriver.sCcidCommand.bSpecific_1;
590 bClassEnvelope = ccidDriver.sCcidCommand.bSpecific_2;
591
592 ISO7816_toAPDU();
593 }
594
595 RDRtoPCSlotStatus();
596}
597
598//------------------------------------------------------------------------------
599/// Command Pipe, Bulk-OUT Messages
600/// This is a command message to allow entering the PIN for verification or
601/// modification.
602//------------------------------------------------------------------------------
603static void PCtoRDRSecure( void )
604{
605 TRACE_DEBUG(".");
606
607 TRACE_DEBUG("For user\n\r");
608}
609
610//------------------------------------------------------------------------------
611/// Command Pipe, Bulk-OUT Messages
612/// This command is used to manage motorized type CCID functionality.
613/// The Lock Card function is used to hold the ICC.
614/// This prevents an ICC from being easily removed from the CCID.
615/// The Unlock Card function is used to remove the hold initiated by the Lock
616/// Card function
617//------------------------------------------------------------------------------
618static void PCtoRDRMechanical( void )
619{
620 TRACE_DEBUG(".");
621 TRACE_DEBUG("Not implemented\n\r");
622
623 RDRtoPCSlotStatus();
624}
625
626//------------------------------------------------------------------------------
627/// Command Pipe, Bulk-OUT Messages
628/// This command is used with the Control pipe Abort request to tell the CCID
629/// to stop any current transfer at the specified slot and return to a state
630/// where the slot is ready to accept a new command pipe Bulk-OUT message.
631//------------------------------------------------------------------------------
632static void PCtoRDRAbort( void )
633{
634 TRACE_DEBUG(".");
635
636 RDRtoPCSlotStatus();
637}
638
639//------------------------------------------------------------------------------
640/// Command Pipe, Bulk-OUT Messages
641/// This command is used to manually set the data rate and clock frequency of
642/// a specific slot.
643//------------------------------------------------------------------------------
644static void PCtoRDRSetDataRateAndClockFrequency( void )
645{
646 unsigned int dwClockFrequency;
647 unsigned int dwDataRate;
648
649 TRACE_DEBUG(".");
650
651 dwClockFrequency = ccidDriver.sCcidCommand.APDU[0]
652 + (ccidDriver.sCcidCommand.APDU[1]<<8)
653 + (ccidDriver.sCcidCommand.APDU[2]<<16)
654 + (ccidDriver.sCcidCommand.APDU[3]<<24);
655
656 dwDataRate = ccidDriver.sCcidCommand.APDU[4]
657 + (ccidDriver.sCcidCommand.APDU[5]<<8)
658 + (ccidDriver.sCcidCommand.APDU[6]<<16)
659 + (ccidDriver.sCcidCommand.APDU[7]<<24);
660
661 ISO7816_SetDataRateandClockFrequency( dwClockFrequency, dwDataRate );
662
663 RDRtoPCDataRateAndClockFrequency( dwClockFrequency, dwDataRate );
664
665}
666
667//------------------------------------------------------------------------------
668/// Report the CMD_NOT_SUPPORTED error to the host
669//------------------------------------------------------------------------------
670static void vCCIDCommandNotSupported( void )
671{
672 // Command not supported
673 // vCCIDReportError(CMD_NOT_SUPPORTED);
674
675 TRACE_DEBUG("CMD_NOT_SUPPORTED\n\r");
676
677 // Header fields settings
678 ccidDriver.sCcidMessage.bMessageType = RDR_TO_PC_SLOTSTATUS;
679 ccidDriver.sCcidMessage.wLength = 0;
680 ccidDriver.sCcidMessage.bSpecific = 0;
681
682 ccidDriver.sCcidMessage.bStatus |= ICC_CS_FAILED;
683
684 // Send the response to the host
685 //vCCIDSendResponse();
686}
687
688//------------------------------------------------------------------------------
689/// Sent CCID response on USB
690//------------------------------------------------------------------------------
691static void vCCIDSendResponse( void )
692{
693 unsigned char bStatus;
694 TRACE_DEBUG(".");
695
696 do {
697 bStatus = USBD_Write( CCID_EPT_DATA_IN, (void*)&ccidDriver.sCcidMessage,
698 ccidDriver.sCcidMessage.bSizeToSend, 0, 0 );
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100699 } while (bStatus != USBD_STATUS_SUCCESS);
Christina Quast1a224af2015-03-10 15:11:37 +0100700
701 TRACE_DEBUG("bStatus: 0x%x\n\r", bStatus);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100702}
703
704
705//------------------------------------------------------------------------------
706/// Description: CCID Command dispatcher
707//------------------------------------------------------------------------------
708static void CCIDCommandDispatcher( void )
709{
710 unsigned char MessageToSend = 0;
711
712 TRACE_DEBUG("Command: 0x%X 0x%x 0x%X 0x%X 0x%X 0x%X 0x%X\n\r\n\r",
713 (unsigned int)ccidDriver.sCcidCommand.bMessageType,
714 (unsigned int)ccidDriver.sCcidCommand.wLength,
715 (unsigned int)ccidDriver.sCcidCommand.bSlot,
716 (unsigned int)ccidDriver.sCcidCommand.bSeq,
717 (unsigned int)ccidDriver.sCcidCommand.bSpecific_0,
718 (unsigned int)ccidDriver.sCcidCommand.bSpecific_1,
719 (unsigned int)ccidDriver.sCcidCommand.bSpecific_2);
720
721 // Check the slot number
722 if ( ccidDriver.sCcidCommand.bSlot > 0 ) {
723
724 TRACE_ERROR("BAD_SLOT_NUMBER\n\r");
725 }
726
Christina Quastb58434e2015-03-09 17:13:07 +0100727 TRACE_INFO("typ=0x%X\n\r", ccidDriver.sCcidCommand.bMessageType);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100728
729 ccidDriver.sCcidMessage.bStatus = 0;
730
731 ccidDriver.sCcidMessage.bSeq = ccidDriver.sCcidCommand.bSeq;
732 ccidDriver.sCcidMessage.bSlot = ccidDriver.sCcidCommand.bSlot;
733
734 ccidDriver.sCcidMessage.bSizeToSend = sizeof(S_ccid_bulk_in_header)-(ABDATA_SIZE+1);
735
736
737 // Command dispatcher
738 switch ( ccidDriver.sCcidCommand.bMessageType ) {
739
740 case PC_TO_RDR_ICCPOWERON:
741 PCtoRDRIccPowerOn();
742 MessageToSend = 1;
743 break;
744
745 case PC_TO_RDR_ICCPOWEROFF:
746 PCtoRDRIccPowerOff();
747 MessageToSend = 1;
748 break;
749
750 case PC_TO_RDR_GETSLOTSTATUS:
751 PCtoRDRGetSlotStatus();
752 MessageToSend = 1;
753 break;
754
755 case PC_TO_RDR_XFRBLOCK:
756 PCtoRDRXfrBlock();
757 MessageToSend = 1;
758 break;
759
760 case PC_TO_RDR_GETPARAMETERS:
761 PCtoRDRGetParameters();
762 MessageToSend = 1;
763 break;
764
765 case PC_TO_RDR_RESETPARAMETERS:
766 PCtoRDRResetParameters();
767 MessageToSend = 1;
768 break;
769
770 case PC_TO_RDR_SETPARAMETERS:
771 PCtoRDRSetParameters();
772 MessageToSend = 1;
773 break;
774
775 case PC_TO_RDR_ESCAPE:
776 PCtoRDREscape();
777 MessageToSend = 1;
778 break;
779
780 case PC_TO_RDR_ICCCLOCK:
781 PCtoRDRICCClock();
782 MessageToSend = 1;
783 break;
784
785 case PC_TO_RDR_T0APDU:
786 // Only CCIDs reporting a short or extended APDU level in the dwFeatures
787 // field of the CCID class descriptor may take this command into account.
788 if( (CCID_FEATURES_EXC_SAPDU == (CCID_FEATURES_EXC_SAPDU&configurationDescriptorsFS->ccid.dwFeatures))
789 || (CCID_FEATURES_EXC_APDU == (CCID_FEATURES_EXC_APDU &configurationDescriptorsFS->ccid.dwFeatures)) ) {
790
791 // command supported
792 PCtoRDRtoAPDU();
793 }
794 else {
795 // command not supported
Christina Quastb58434e2015-03-09 17:13:07 +0100796 TRACE_INFO("Not supported: PC_TO_RDR_T0APDU\n\r");
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100797 vCCIDCommandNotSupported();
798 }
799 MessageToSend = 1;
800 break;
801
802 case PC_TO_RDR_SECURE:
803 PCtoRDRSecure();
804 MessageToSend = 1;
805 break;
806
807 case PC_TO_RDR_MECHANICAL:
808 PCtoRDRMechanical();
809 MessageToSend = 1;
810 break;
811
812 case PC_TO_RDR_ABORT:
813 PCtoRDRAbort();
814 MessageToSend = 1;
815 break;
816
817 case PC_TO_RDR_SETDATARATEANDCLOCKFREQUENCY:
818 PCtoRDRSetDataRateAndClockFrequency();
819 MessageToSend = 1;
820 break;
821
822 default:
Christina Quastb58434e2015-03-09 17:13:07 +0100823 TRACE_DEBUG("default: Not supported: 0x%X\n\r", ccidDriver.sCcidCommand.bMessageType);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100824 vCCIDCommandNotSupported();
825 MessageToSend = 1;
826 break;
827
828 }
829
830 if( MessageToSend == 1 ) {
831 vCCIDSendResponse();
832 }
833}
834
835
836//------------------------------------------------------------------------------
837/// SETUP request handler for a CCID device
838/// \param pRequest Pointer to a USBGenericRequest instance
839//------------------------------------------------------------------------------
840static void CCID_RequestHandler(const USBGenericRequest *pRequest)
841{
842 TRACE_DEBUG("CCID_RHl\n\r");
843
844 // Check if this is a class request
845 if (USBGenericRequest_GetType(pRequest) == USBGenericRequest_CLASS) {
846
847 // Check if the request is supported
848 switch (USBGenericRequest_GetRequest(pRequest)) {
849
850 case CCIDGenericRequest_ABORT:
851 TRACE_DEBUG("CCIDGenericRequest_ABORT\n\r");
852 break;
853
854 case CCIDGenericRequest_GET_CLOCK_FREQUENCIES:
Christina Quastb58434e2015-03-09 17:13:07 +0100855 TRACE_DEBUG("Not supported: CCIDGenericRequest_GET_CLOCK_FREQUENCIES\n\r");
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100856 // A CCID with bNumClockSupported equal to 00h does not have
857 // to support this request
858 break;
859
860 case CCIDGenericRequest_GET_DATA_RATES:
Christina Quastb58434e2015-03-09 17:13:07 +0100861 TRACE_DEBUG("Not supported: CCIDGenericRequest_GET_DATA_RATES\n\r");
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100862 // A CCID with bNumDataRatesSupported equal to 00h does not have
863 // to support this request.
864 break;
865
866 default:
867 TRACE_WARNING( "CCIDDriver_RequestHandler: Unsupported request (%d)\n\r",
868 USBGenericRequest_GetRequest(pRequest));
869 USBD_Stall(0);
870 }
871 }
872
873 else if (USBGenericRequest_GetType(pRequest) == USBGenericRequest_STANDARD) {
874
875 // Forward request to the standard handler
876 USBDDriver_RequestHandler(&(ccidDriver.usbdDriver), pRequest);
877 }
878 else {
879
880 // Unsupported request type
881 TRACE_WARNING( "CCIDDriver_RequestHandler: Unsupported request type (%d)\n\r",
882 USBGenericRequest_GetType(pRequest));
883 USBD_Stall(0);
884 }
885}
886
887
888//------------------------------------------------------------------------------
889// Exported functions
890//------------------------------------------------------------------------------
891
892//------------------------------------------------------------------------------
893/// Optional callback re-implementation
894//------------------------------------------------------------------------------
895#if 0
896#if !defined(NOAUTOCALLBACK)
897// not static function
898void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
899{
900 CCID_RequestHandler(request);
901}
902#endif
903#endif
904
905
906//------------------------------------------------------------------------------
907/// Handles SmartCart request
908//------------------------------------------------------------------------------
909void CCID_SmartCardRequest( void )
910{
911 unsigned char bStatus;
912 TRACE_DEBUG(".");
913
914 do {
915
916 bStatus = CCID_Read( (void*)&ccidDriver.sCcidCommand,
917 sizeof(S_ccid_bulk_out_header),
918 (TransferCallback)&CCIDCommandDispatcher,
919 (void*)0 );
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100920 }
921 while (bStatus != USBD_STATUS_SUCCESS);
Christina Quast1a224af2015-03-10 15:11:37 +0100922 TRACE_DEBUG("bStat: %x\n\r", bStatus);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100923}
924
925
926//------------------------------------------------------------------------------
927/// Reads data from the Data OUT endpoint
928/// \param pBuffer Buffer to store the received data
929/// \param dLength data buffer length
930/// \param fCallback Optional callback function
931/// \param pArgument Optional parameter for the callback function
932/// \return USBD_STATUS_LOCKED or USBD_STATUS_SUCCESS
933//------------------------------------------------------------------------------
934unsigned char CCID_Read(void *pBuffer,
935 unsigned int dLength,
936 TransferCallback fCallback,
937 void *pArgument)
938{
939 return USBD_Read(CCID_EPT_DATA_OUT, pBuffer, dLength, fCallback, pArgument);
940}
941
942//------------------------------------------------------------------------------
943/// Sends data through the Data IN endpoint
944/// \param pBuffer Buffer holding the data to transmit
945/// \param dLength Length of data buffer
946/// \param fCallback Optional callback function
947/// \param pArgument Optional parameter for the callback function
948/// \return USBD_STATUS_LOCKED or USBD_STATUS_SUCCESS
949//------------------------------------------------------------------------------
950unsigned char CCID_Write(void *pBuffer,
951 unsigned int dLength,
952 TransferCallback fCallback,
953 void *pArgument)
954{
955 return USBD_Write(CCID_EPT_DATA_IN, pBuffer, dLength, fCallback, pArgument);
956}
957
958//------------------------------------------------------------------------------
959/// Sends data through the interrupt endpoint, ICC insertion event
960/// RDR_to_PC_NotifySlotChange
961/// \return USBD_STATUS_LOCKED or USBD_STATUS_SUCCESS
962//------------------------------------------------------------------------------
963unsigned char CCID_Insertion( void )
964{
965 TRACE_DEBUG(".");
966
967 // Build the Interrupt-IN message
968 ccidDriver.BufferINT[0] = RDR_TO_PC_NOTIFYSLOTCHANGE;
969 ccidDriver.BufferINT[1] = ICC_INSERTED_EVENT;
970 ccidDriver.SlotStatus = ICC_INSERTED_EVENT;
971
972 // Notify the host that a ICC is inserted
973 return USBD_Write( CCID_EPT_NOTIFICATION, ccidDriver.BufferINT, 2, 0, 0 );
974}
975
976//------------------------------------------------------------------------------
977/// Sends data through the interrupt endpoint, ICC removal event
978/// RDR_to_PC_NotifySlotChange
979/// \return USBD_STATUS_LOCKED or USBD_STATUS_SUCCESS
980//------------------------------------------------------------------------------
981unsigned char CCID_Removal( void )
982{
983 TRACE_DEBUG(".");
984
985 // Build the Interrupt-IN message
986 ccidDriver.BufferINT[0] = RDR_TO_PC_NOTIFYSLOTCHANGE;
987 ccidDriver.BufferINT[1] = ICC_NOT_PRESENT;
988 ccidDriver.SlotStatus = ICC_NOT_PRESENT;
989
990 // Notify the host that a ICC is inserted
991 return USBD_Write( CCID_EPT_NOTIFICATION, ccidDriver.BufferINT, 2, 0, 0 );
992}
993
994//------------------------------------------------------------------------------
995/// Interrupt-IN Messages
996/// This message is sent when any bit in the bHardwareErrorCode field is set.
997/// If this message is sent when there is no “outstanding” command, the bSeq
998/// field will be undefined.
999/// \param bSlot ICC slot number
1000/// \param bSeq Sequence number of the bulk OUT command when the hardware error
1001/// occured
1002/// \param bHardwareErrorCode Hardware error code
1003/// \return USBD_STATUS_LOCKED or USBD_STATUS_SUCCESS
1004//------------------------------------------------------------------------------
1005unsigned char RDRtoPCHardwareError( unsigned char bSlot,
1006 unsigned char bSeq,
1007 unsigned char bHardwareErrorCode )
1008{
1009 TRACE_DEBUG(".");
1010
1011 // Build the Interrupt-IN message
1012 ccidDriver.BufferINT[0] = RDR_TO_PC_HARDWAREERROR;
1013 ccidDriver.BufferINT[1] = bSlot;
1014 ccidDriver.BufferINT[2] = bSeq;
1015 ccidDriver.BufferINT[3] = bHardwareErrorCode;
1016
1017 // Notify the host that a ICC is inserted
1018 return USBD_Write( CCID_EPT_NOTIFICATION, ccidDriver.BufferINT, 4, 0, 0 );
1019}
1020
1021