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