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