Christina Quast | 32906bb | 2015-02-24 11:35:19 +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 | |
| 30 | /*------------------------------------------------------------------------------ |
| 31 | * Headers |
| 32 | *------------------------------------------------------------------------------*/ |
| 33 | |
| 34 | #include "board.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | /*------------------------------------------------------------------------------ |
| 39 | * Internal definitions |
| 40 | *------------------------------------------------------------------------------*/ |
| 41 | |
| 42 | /** Maximum ucSize in bytes of the smartcard answer to a command.*/ |
| 43 | #define MAX_ANSWER_SIZE 10 |
| 44 | |
| 45 | /** Maximum ATR ucSize in bytes.*/ |
| 46 | #define MAX_ATR_SIZE 55 |
| 47 | |
| 48 | /** USB states */ |
| 49 | /// Use for power management |
| 50 | #define STATE_IDLE 0 |
| 51 | /// The USB device is in suspend state |
| 52 | #define STATE_SUSPEND 4 |
| 53 | /// The USB device is in resume state |
| 54 | #define STATE_RESUME 5 |
| 55 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 56 | extern volatile uint8_t timeout_occured; |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 57 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 58 | /*------------------------------------------------------------------------------ |
| 59 | * Internal variables |
| 60 | *------------------------------------------------------------------------------*/ |
| 61 | /** USB state: suspend, resume, idle */ |
| 62 | unsigned char USBState = STATE_IDLE; |
| 63 | |
| 64 | /** ISO7816 pins */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 65 | static const Pin pinsISO7816_PHONE[] = { PINS_ISO7816_PHONE }; |
| 66 | |
Christina Quast | 8043fdd | 2015-03-04 18:45:30 +0100 | [diff] [blame] | 67 | /** Bus switch pins */ |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 68 | |
| 69 | #if DEBUG_PHONE_SNIFF |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 70 | #warning "Debug phone sniff via logic analyzer is enabled" |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 71 | // Logic analyzer probes are easier to attach to the SIM card slot |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 72 | static const Pin pins_bus[] = { PINS_BUS_SNIFF }; |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 73 | #else |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 74 | static const Pin pins_bus[] = { PINS_BUS_DEFAULT }; |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 75 | #endif |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 76 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 77 | /** ISO7816 RST pin */ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 78 | static uint8_t sim_inserted = 0; |
| 79 | |
| 80 | static const Pin pPwr[] = { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 81 | /* Enable power converter 4.5-6V to 3.3V; low: off */ |
| 82 | {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}, |
| 83 | |
| 84 | /* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */ |
| 85 | {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT} |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Christina Quast | e4cbfe6 | 2015-04-20 13:07:28 +0200 | [diff] [blame] | 88 | const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE; |
Christina Quast | 8e5381c | 2015-04-03 11:37:17 +0200 | [diff] [blame] | 89 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 90 | static struct Usart_info usart_info = { |
| 91 | .base = USART_PHONE, |
| 92 | .id = ID_USART_PHONE, |
| 93 | .state = USART_RCV, |
| 94 | }; |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 95 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 96 | /* ===================================================*/ |
| 97 | /* Taken from iso7816_4.c */ |
| 98 | /* ===================================================*/ |
| 99 | /** Flip flop for send and receive char */ |
| 100 | #define USART_SEND 0 |
| 101 | #define USART_RCV 1 |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 102 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 103 | /*----------------------------------------------------------------------------- |
| 104 | * Internal variables |
| 105 | *-----------------------------------------------------------------------------*/ |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 106 | static uint8_t host_to_sim_buf[BUFLEN]; |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 107 | static bool change_fidi = false; |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 108 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 109 | static void receive_from_host(void); |
| 110 | static void sendResponse_to_phone(uint8_t * pArg, uint8_t status, |
| 111 | uint32_t transferred, uint32_t remaining) |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 112 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 113 | if (status != USBD_STATUS_SUCCESS) { |
| 114 | TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status); |
| 115 | return; |
| 116 | } |
| 117 | TRACE_DEBUG("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, |
| 118 | transferred, remaining); |
| 119 | TRACE_DEBUG("Resp: %x %x %x .. %x\n", host_to_sim_buf[0], |
| 120 | host_to_sim_buf[1], host_to_sim_buf[2], |
| 121 | host_to_sim_buf[transferred - 1]); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 122 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 123 | USART_SetReceiverEnabled(USART_PHONE, 0); |
| 124 | USART_SetTransmitterEnabled(USART_PHONE, 1); |
| 125 | uint32_t i = 0; |
| 126 | if (host_to_sim_buf[0] == 0xff) { |
| 127 | printf("Change FIDI detected\n"); |
| 128 | // PTS command, change FIDI after command |
| 129 | i = 2; |
| 130 | change_fidi = true; |
| 131 | } |
| 132 | for (; i < transferred; i++) { |
| 133 | ISO7816_SendChar(host_to_sim_buf[i], &usart_info); |
| 134 | } |
| 135 | USART_SetTransmitterEnabled(USART_PHONE, 0); |
| 136 | USART_SetReceiverEnabled(USART_PHONE, 1); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 137 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 138 | if (change_fidi == true) { |
| 139 | printf("Change FIDI: %x\n", host_to_sim_buf[2]); |
| 140 | update_fidi(host_to_sim_buf[2]); |
| 141 | change_fidi = false; |
| 142 | } |
Christina Quast | cb646bc | 2015-05-03 14:21:26 +0200 | [diff] [blame] | 143 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 144 | receive_from_host(); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Harald Welte | ec4fe23 | 2015-11-07 18:41:25 +0100 | [diff] [blame] | 147 | static void receive_from_host() |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 148 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 149 | int ret; |
| 150 | if ((ret = USBD_Read(PHONE_DATAOUT, &host_to_sim_buf, |
| 151 | sizeof(host_to_sim_buf), |
| 152 | (TransferCallback) &sendResponse_to_phone, |
| 153 | 0)) == USBD_STATUS_SUCCESS) { |
| 154 | } else { |
| 155 | TRACE_ERROR("USB Err: %X\n", ret); |
| 156 | } |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 157 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 158 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 159 | void Phone_configure(void) |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 160 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 161 | PIO_ConfigureIt(&pinPhoneRST, ISR_PhoneRST); |
| 162 | NVIC_EnableIRQ(PIOA_IRQn); |
| 163 | } |
| 164 | |
| 165 | void Phone_exit(void) |
| 166 | { |
| 167 | PIO_DisableIt(&pinPhoneRST); |
| 168 | NVIC_DisableIRQ(USART1_IRQn); |
| 169 | USART_DisableIt(USART_PHONE, US_IER_RXRDY); |
| 170 | USART_SetTransmitterEnabled(USART_PHONE, 0); |
| 171 | USART_SetReceiverEnabled(USART_PHONE, 0); |
| 172 | } |
| 173 | |
| 174 | void Phone_init(void) |
| 175 | { |
| 176 | PIO_Configure(pinsISO7816_PHONE, PIO_LISTSIZE(pinsISO7816_PHONE)); |
| 177 | PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus)); |
| 178 | |
| 179 | PIO_Configure(&pinPhoneRST, 1); |
| 180 | |
| 181 | PIO_EnableIt(&pinPhoneRST); |
| 182 | ISO7816_Init(&usart_info, CLK_SLAVE); |
| 183 | |
| 184 | USART_SetTransmitterEnabled(USART_PHONE, 0); |
| 185 | USART_SetReceiverEnabled(USART_PHONE, 1); |
| 186 | |
| 187 | USART_EnableIt(USART_PHONE, US_IER_RXRDY); |
| 188 | NVIC_EnableIRQ(USART1_IRQn); |
| 189 | |
| 190 | receive_from_host(); |
| 191 | } |
| 192 | |
| 193 | void Phone_run(void) |
| 194 | { |
| 195 | check_data_from_phone(); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 196 | } |