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 | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 56 | /* WTX (Wait time extension): |
| 57 | * R-block PCB begins with (msb) 10 , ends with 000011 for WTX req, 100011 for WTX resp |
| 58 | * |
| 59 | * The standard says: |
| 60 | * Rule 3 β If the card requires more than BWT to process the previously received I-block, it transmits S(WTX |
| 61 | * request) where INF conveys one byte encoding an integer multiplier of the BWT value. The interface device |
| 62 | * shall acknowledge by S(WTX response) with the same INF. |
| 63 | * The time allocated starts at the leading edge of the last character of S(WTX response). |
| 64 | */ |
Christina Quast | a4d1d16 | 2015-04-06 20:41:50 +0200 | [diff] [blame] | 65 | // FIXME: Two times the same name for the define, which one is right? |
| 66 | //#define WTX_req 0b10000011 |
| 67 | //#define WTX_req 0b10100011 |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 68 | // Alternatively: |
| 69 | /* For T = 0 Protocol: The firmware on receiving the NULL (0x60) Procedure byte from the card, notifies |
| 70 | it to the driver using the RDR_to_PC_DataBlock response. During this period, the reception of bytes |
| 71 | from the smart card is still in progress and hence the device cannot indefinitely wait for IN tokens on |
| 72 | the USB bulk-in endpoint. Hence, it is required of the driver to readily supply βINβ tokens on the USB |
| 73 | bulk-in endpoint. On failure to do so, some of the wait time extension responses, will not be queued to |
| 74 | the driver. |
| 75 | */ |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 76 | extern volatile uint8_t timeout_occured; |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 77 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 78 | /*------------------------------------------------------------------------------ |
| 79 | * Internal variables |
| 80 | *------------------------------------------------------------------------------*/ |
| 81 | /** USB state: suspend, resume, idle */ |
| 82 | unsigned char USBState = STATE_IDLE; |
| 83 | |
| 84 | /** ISO7816 pins */ |
| 85 | static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE}; |
Christina Quast | 8043fdd | 2015-03-04 18:45:30 +0100 | [diff] [blame] | 86 | /** Bus switch pins */ |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 87 | |
| 88 | #if DEBUG_PHONE_SNIFF |
| 89 | # warning "Debug phone sniff via logic analyzer is enabled" |
| 90 | // Logic analyzer probes are easier to attach to the SIM card slot |
| 91 | static const Pin pins_bus[] = {PINS_BUS_SNIFF}; |
| 92 | #else |
Christina Quast | 3041854 | 2015-04-05 10:08:06 +0200 | [diff] [blame] | 93 | static const Pin pins_bus[] = {PINS_BUS_DEFAULT}; |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 94 | #endif |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 95 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 96 | /** ISO7816 RST pin */ |
| 97 | static const Pin pinIso7816RstMC = PIN_ISO7816_RST_PHONE; |
| 98 | static uint8_t sim_inserted = 0; |
| 99 | |
| 100 | static const Pin pPwr[] = { |
| 101 | /* Enable power converter 4.5-6V to 3.3V; low: off */ |
| 102 | {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}, |
| 103 | |
| 104 | /* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */ |
| 105 | {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT} |
| 106 | }; |
| 107 | |
| 108 | |
Christina Quast | 8e5381c | 2015-04-03 11:37:17 +0200 | [diff] [blame] | 109 | static const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE; |
| 110 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 111 | static struct Usart_info usart_info = {.base = USART_PHONE, .id = ID_USART_PHONE}; |
| 112 | |
Christina Quast | 8e5381c | 2015-04-03 11:37:17 +0200 | [diff] [blame] | 113 | #define PR TRACE_INFO |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 114 | |
| 115 | /* ===================================================*/ |
| 116 | /* Taken from iso7816_4.c */ |
| 117 | /* ===================================================*/ |
| 118 | /** Flip flop for send and receive char */ |
| 119 | #define USART_SEND 0 |
| 120 | #define USART_RCV 1 |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 121 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 122 | // FIXME: Comments |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 123 | /*----------------------------------------------------------------------------- |
| 124 | * Internal variables |
| 125 | *-----------------------------------------------------------------------------*/ |
| 126 | /** Variable for state of send and receive froom USART */ |
| 127 | static uint8_t StateUsartGlobal = USART_RCV; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 128 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 129 | static uint8_t host_to_sim_buf[BUFLEN]; |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 130 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 131 | /*----------------------------------------------------------------------------- |
| 132 | * Interrupt routines |
| 133 | *-----------------------------------------------------------------------------*/ |
Christina Quast | dcce4c3 | 2015-04-13 22:20:12 +0200 | [diff] [blame] | 134 | void Callback_PhoneRST_ISR( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining) |
| 135 | { |
| 136 | printf("rstCB\n\r"); |
| 137 | PIO_EnableIt( &pinPhoneRST ) ; |
| 138 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 139 | static void ISR_PhoneRST( const Pin *pPin) |
| 140 | { |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 141 | int ret; |
| 142 | // FIXME: no printfs in ISRs? |
Christina Quast | d3630cc | 2015-04-03 11:38:24 +0200 | [diff] [blame] | 143 | printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR); |
| 144 | if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) ) |
| 145 | { |
| 146 | if(PIO_Get( &pinPhoneRST ) == 0) { |
| 147 | printf(" 0 "); |
| 148 | } else { |
| 149 | printf(" 1 "); |
| 150 | } |
| 151 | } |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 152 | |
Christina Quast | dcce4c3 | 2015-04-13 22:20:12 +0200 | [diff] [blame] | 153 | if ((ret = USBD_Write( PHONE_INT, "R", 1, (TransferCallback)&Callback_PhoneRST_ISR, 0 )) != USBD_STATUS_SUCCESS) { |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 154 | TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__); |
| 155 | return; |
| 156 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 157 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 158 | /* Interrupt enabled after ATR is sent to phone */ |
Christina Quast | dcce4c3 | 2015-04-13 22:20:12 +0200 | [diff] [blame] | 159 | PIO_DisableIt( &pinPhoneRST ) ; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 162 | void receive_from_host( void ); |
| 163 | void sendResponse_to_phone( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining) |
| 164 | { |
| 165 | if (status != USBD_STATUS_SUCCESS) { |
| 166 | TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status); |
| 167 | return; |
| 168 | } |
| 169 | PR("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, transferred, remaining); |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 170 | PR("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] | 171 | |
Christina Quast | d52201f | 2015-04-13 22:38:05 +0200 | [diff] [blame] | 172 | USART_SetReceiverEnabled(USART_PHONE, 0); |
| 173 | USART_SetTransmitterEnabled(USART_PHONE, 1); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 174 | for (uint32_t i = 0; i < transferred; i++ ) { |
Christina Quast | b595e7c | 2015-04-12 13:31:01 +0200 | [diff] [blame] | 175 | ISO7816_SendChar(host_to_sim_buf[i], &usart_info); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 176 | } |
Christina Quast | d52201f | 2015-04-13 22:38:05 +0200 | [diff] [blame] | 177 | USART_SetTransmitterEnabled(USART_PHONE, 0); |
| 178 | USART_SetReceiverEnabled(USART_PHONE, 1); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 179 | |
| 180 | receive_from_host(); |
| 181 | } |
| 182 | |
| 183 | void receive_from_host() |
| 184 | { |
| 185 | int ret; |
| 186 | if ((ret = USBD_Read(PHONE_DATAOUT, &host_to_sim_buf, sizeof(host_to_sim_buf), |
| 187 | (TransferCallback)&sendResponse_to_phone, 0)) == USBD_STATUS_SUCCESS) { |
| 188 | } else { |
Christina Quast | 5c6a299 | 2015-04-11 20:03:14 +0200 | [diff] [blame] | 189 | TRACE_ERROR("USB Err: %X\n", ret); |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 192 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 193 | void Phone_configure( void ) { |
| 194 | PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ; |
| 195 | NVIC_EnableIRQ( PIOA_IRQn ); |
| 196 | } |
| 197 | |
| 198 | void Phone_exit( void ) { |
| 199 | PIO_DisableIt( &pinPhoneRST ) ; |
Christina Quast | d52201f | 2015-04-13 22:38:05 +0200 | [diff] [blame] | 200 | NVIC_DisableIRQ(USART1_IRQn); |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 201 | USART_DisableIt( USART_PHONE, US_IER_RXRDY) ; |
| 202 | USART_SetTransmitterEnabled(USART_PHONE, 0); |
| 203 | USART_SetReceiverEnabled(USART_PHONE, 0); |
| 204 | } |
| 205 | |
| 206 | void Phone_init( void ) { |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 207 | PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ; |
Christina Quast | 8043fdd | 2015-03-04 18:45:30 +0100 | [diff] [blame] | 208 | PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ; |
| 209 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 210 | PIO_Configure( &pinPhoneRST, 1); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 211 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 212 | PIO_EnableIt( &pinPhoneRST ) ; |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 213 | ISO7816_Init(&usart_info, CLK_SLAVE); |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 214 | |
Christina Quast | d52201f | 2015-04-13 22:38:05 +0200 | [diff] [blame] | 215 | USART_SetTransmitterEnabled(USART_PHONE, 0); |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 216 | USART_SetReceiverEnabled(USART_PHONE, 1); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 217 | |
Christina Quast | d52201f | 2015-04-13 22:38:05 +0200 | [diff] [blame] | 218 | USART_EnableIt(USART_PHONE, US_IER_RXRDY); // TODO: interrupt enable/disable is shared with sniffer |
| 219 | NVIC_EnableIRQ(USART1_IRQn); |
| 220 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 221 | /* Configure ISO7816 driver */ |
| 222 | // FIXME: PIO_Configure(pPwr, PIO_LISTSIZE( pPwr )); |
| 223 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 224 | // FIXME: Or do I need to call VBUS_CONFIGURE() here instead, which will call USBD_Connect() later? |
| 225 | // USBD_Connect(); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 226 | |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 227 | //Timer_Init(); |
| 228 | |
| 229 | receive_from_host(); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 230 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 231 | |
Christina Quast | aab3d7c | 2015-04-09 17:02:17 +0200 | [diff] [blame] | 232 | |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 233 | // Sniffed Phone to SIM card communication: |
| 234 | // phone > sim : RST |
| 235 | // phone < sim : ATR |
| 236 | // phone > sim : A0 A4 00 00 02 (Select File) |
| 237 | // phone < sim : A4 (INS repeated) |
| 238 | // phone > sim : 7F 02 (= ??) |
| 239 | // phone < sim : 9F 16 (9F: success, can deliver 0x16 (=22) byte) |
| 240 | // phone > sim : ?? (A0 C0 00 00 16) |
| 241 | // phone < sim : C0 (INS repeated) |
| 242 | // phone < sim : 00 00 00 00 7F 20 02 00 00 00 00 00 09 91 00 17 04 00 83 8A (data of length 22 -2) |
| 243 | // phone <? sim : 90 00 (OK, everything went fine) |
| 244 | // phone ? sim : 00 (??) |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 245 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 246 | void Phone_run( void ) |
| 247 | { |
Christina Quast | 4db82e0 | 2015-04-11 18:14:41 +0200 | [diff] [blame] | 248 | check_data_from_phone(); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 249 | } |