Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, 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 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 30 | #ifdef HAVE_CCID |
| 31 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 32 | /*------------------------------------------------------------------------------ |
| 33 | * Headers |
| 34 | *------------------------------------------------------------------------------*/ |
| 35 | |
| 36 | #include "board.h" |
| 37 | |
| 38 | #include <string.h> |
| 39 | |
| 40 | /*------------------------------------------------------------------------------ |
| 41 | * Internal definitions |
| 42 | *------------------------------------------------------------------------------*/ |
| 43 | /** Maximum ucSize in bytes of the smartcard answer to a command.*/ |
| 44 | #define MAX_ANSWER_SIZE 10 |
| 45 | |
| 46 | /** Maximum ATR ucSize in bytes.*/ |
| 47 | #define MAX_ATR_SIZE 55 |
| 48 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 49 | /*------------------------------------------------------------------------------ |
| 50 | * Internal variables |
| 51 | *------------------------------------------------------------------------------*/ |
| 52 | |
| 53 | /** ISO7816 pins */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 54 | static const Pin pinsISO7816[] = { PINS_ISO7816 }; |
| 55 | |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 56 | /** Bus switch pins */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 57 | static const Pin pinsBus[] = { PINS_BUS_DEFAULT }; |
| 58 | |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 59 | /* SIMcard power pin */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 60 | static const Pin pinsPower[] = { PWR_PINS }; |
| 61 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 62 | /** ISO7816 RST pin */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 63 | static const Pin pinIso7816RstMC = PIN_ISO7816_RSTMC; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 64 | static uint8_t sim_inserted = 0; |
| 65 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 66 | static struct Usart_info usart_info = { |
| 67 | .base = USART_SIM, |
| 68 | .id = ID_USART_SIM, |
| 69 | .state = USART_RCV |
| 70 | }; |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 71 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 72 | /*------------------------------------------------------------------------------ |
| 73 | * Optional smartcard detection |
| 74 | *------------------------------------------------------------------------------*/ |
| 75 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 76 | /** Smartcard detection pin.*/ |
| 77 | static const Pin pinSmartCard = SMARTCARD_CONNECT_PIN; |
| 78 | |
| 79 | /** |
| 80 | * PIO interrupt service routine. Checks if the smartcard has been connected |
| 81 | * or disconnected. |
| 82 | */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 83 | static void ISR_PioSmartCard(const Pin * pPin) |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 84 | { |
| 85 | /* FIXME: why is pinSmartCard.pio->PIO_ISR the wrong number? |
| 86 | printf("+++++ Trying to check for pending interrupts (PIO ISR: 0x%X)\n\r", pinSmartCard.pio->PIO_ISR); |
| 87 | printf("+++++ Mask: 0x%X\n\r", pinSmartCard.mask); |
| 88 | Output: |
| 89 | +++++ Trying to check for pending interrupts (PIO ISR: 0x400)) = 1<<10 |
| 90 | +++++ Mask: 0x100 = 1<<8 |
| 91 | */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 92 | // PA10 is DTXD, which is the debug uart transmit pin |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 93 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 94 | printf("Interrupt!!\n\r"); |
| 95 | /* Check all pending interrupts */ |
| 96 | // FIXME: this if condition is not always true... |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 97 | // if ( (pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0 ) |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 98 | { |
| 99 | /* Check current level on pin */ |
| 100 | if (PIO_Get(&pinSmartCard) == 0) { |
| 101 | sim_inserted = 1; |
| 102 | printf("-I- Smartcard inserted\n\r"); |
| 103 | CCID_Insertion(); |
| 104 | } else { |
| 105 | sim_inserted = 0; |
| 106 | printf("-I- Smartcard removed\n\r"); |
| 107 | CCID_Removal(); |
| 108 | } |
| 109 | } |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Configures the smartcard detection pin to trigger an interrupt. |
| 114 | */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 115 | static void ConfigureCardDetection(void) |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 116 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 117 | printf("+++++ Configure PIOs\n\r"); |
| 118 | PIO_Configure(&pinSmartCard, 1); |
| 119 | NVIC_EnableIRQ(PIOA_IRQn); |
| 120 | PIO_EnableIt(&pinSmartCard); |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 123 | /*----------------------------------------------------------------------------- |
| 124 | * Initialization and run |
| 125 | *-----------------------------------------------------------------------------*/ |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 126 | extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 127 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 128 | void CCID_configure(void) |
| 129 | { |
| 130 | CCIDDriver_Initialize(); |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 131 | // FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 132 | PIO_ConfigureIt(&pinSmartCard, ISR_PioSmartCard); |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 135 | void CCID_exit(void) |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 136 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 137 | PIO_DisableIt(&pinSmartCard); |
| 138 | USART_SetTransmitterEnabled(usart_info.base, 0); |
| 139 | USART_SetReceiverEnabled(usart_info.base, 0); |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 142 | void CCID_init(void) |
| 143 | { |
| 144 | uint8_t pAtr[MAX_ATR_SIZE]; |
| 145 | uint8_t ucSize; |
| 146 | |
| 147 | // FIXME: do we want to print ATR? |
| 148 | /* Initialize Atr buffer */ |
| 149 | memset(pAtr, 0, sizeof(pAtr)); |
| 150 | |
| 151 | ConfigureCardDetection(); |
| 152 | |
| 153 | // Configure ISO7816 driver |
| 154 | PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816)); |
| 155 | PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus)); |
| 156 | PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower)); |
| 157 | |
| 158 | /* power up the card */ |
| 159 | // PIO_Set(&pinsPower[0]); |
| 160 | |
| 161 | ISO7816_Init(&usart_info, CLK_MASTER); |
| 162 | USART_SetTransmitterEnabled(usart_info.base, 1); |
| 163 | USART_SetReceiverEnabled(usart_info.base, 1); |
| 164 | |
| 165 | ISO7816_Set_Reset_Pin(&pinIso7816RstMC); |
| 166 | /* Read ATR */ |
| 167 | ISO7816_warm_reset(); |
| 168 | |
| 169 | ISO7816_Datablock_ATR(pAtr, &ucSize); |
| 170 | |
| 171 | /* Decode ATR and print it */ |
| 172 | ISO7816_Decode_ATR(pAtr); |
| 173 | |
| 174 | // FIXME. what if smcard is not inserted? |
| 175 | if (PIO_Get(&pinSmartCard) == 0) { |
| 176 | printf("SIM card inserted\n\r"); |
| 177 | CCID_Insertion(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void CCID_run(void) |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 182 | { |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 183 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 184 | //if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument); |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 185 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 186 | CCID_SmartCardRequest(); |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 187 | } |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 188 | #endif |