blob: ca1dfec1cd966c7320ea3747f308966a2ab12bc8 [file] [log] [blame]
Christina Quast32906bb2015-02-24 11:35:19 +01001/* ----------------------------------------------------------------------------
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 Quast4db82e02015-04-11 18:14:41 +020056extern volatile uint8_t timeout_occured;
Christina Quast578daaa2015-03-13 23:46:01 +010057
Christina Quast32906bb2015-02-24 11:35:19 +010058/*------------------------------------------------------------------------------
59 * Internal variables
60 *------------------------------------------------------------------------------*/
61/** USB state: suspend, resume, idle */
62unsigned char USBState = STATE_IDLE;
63
64/** ISO7816 pins */
65static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE};
Christina Quast8043fdd2015-03-04 18:45:30 +010066/** Bus switch pins */
Christina Quast5c6a2992015-04-11 20:03:14 +020067
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
71static const Pin pins_bus[] = {PINS_BUS_SNIFF};
72#else
Christina Quast30418542015-04-05 10:08:06 +020073static const Pin pins_bus[] = {PINS_BUS_DEFAULT};
Christina Quast5c6a2992015-04-11 20:03:14 +020074#endif
Christina Quast4bcc0232015-03-24 21:59:32 +010075
Christina Quast32906bb2015-02-24 11:35:19 +010076/** ISO7816 RST pin */
Christina Quast32906bb2015-02-24 11:35:19 +010077static uint8_t sim_inserted = 0;
78
79static 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 Quaste4cbfe62015-04-20 13:07:28 +020087const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
Christina Quast8e5381c2015-04-03 11:37:17 +020088
Christina Quastec9c09e2015-04-16 10:45:39 +020089static struct Usart_info usart_info = {.base = USART_PHONE, .id = ID_USART_PHONE, .state = USART_RCV};
Christina Quaste24b9ac2015-04-10 17:44:49 +020090
Christina Quast32906bb2015-02-24 11:35:19 +010091/* ===================================================*/
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 Quastce296b92015-03-18 18:41:19 +010097
Christina Quast32906bb2015-02-24 11:35:19 +010098/*-----------------------------------------------------------------------------
99 * Internal variables
100 *-----------------------------------------------------------------------------*/
Christina Quast4db82e02015-04-11 18:14:41 +0200101static uint8_t host_to_sim_buf[BUFLEN];
Christina Quastcb646bc2015-05-03 14:21:26 +0200102static bool change_fidi = false;
Christina Quast0ca83902015-03-22 19:05:23 +0100103
Harald Welteec4fe232015-11-07 18:41:25 +0100104static void receive_from_host( void );
105static void sendResponse_to_phone( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
Christina Quast4db82e02015-04-11 18:14:41 +0200106{
107 if (status != USBD_STATUS_SUCCESS) {
108 TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status);
109 return;
110 }
Christina Quast4a30a372015-05-08 15:34:25 +0200111 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 Quast4db82e02015-04-11 18:14:41 +0200113
Christina Quastd52201f2015-04-13 22:38:05 +0200114 USART_SetReceiverEnabled(USART_PHONE, 0);
115 USART_SetTransmitterEnabled(USART_PHONE, 1);
Christina Quastcb646bc2015-05-03 14:21:26 +0200116 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 Quastb595e7c2015-04-12 13:31:01 +0200124 ISO7816_SendChar(host_to_sim_buf[i], &usart_info);
Christina Quast4db82e02015-04-11 18:14:41 +0200125 }
Christina Quastd52201f2015-04-13 22:38:05 +0200126 USART_SetTransmitterEnabled(USART_PHONE, 0);
127 USART_SetReceiverEnabled(USART_PHONE, 1);
Christina Quast4db82e02015-04-11 18:14:41 +0200128
Christina Quastcb646bc2015-05-03 14:21:26 +0200129 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 Quast4db82e02015-04-11 18:14:41 +0200135 receive_from_host();
136}
137
Harald Welteec4fe232015-11-07 18:41:25 +0100138static void receive_from_host()
Christina Quast4db82e02015-04-11 18:14:41 +0200139{
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 Quast5c6a2992015-04-11 20:03:14 +0200144 TRACE_ERROR("USB Err: %X\n", ret);
Christina Quast4db82e02015-04-11 18:14:41 +0200145 }
146}
Christina Quast32906bb2015-02-24 11:35:19 +0100147
Christina Quast95d66162015-04-09 22:38:47 +0200148void Phone_configure( void ) {
149 PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
150 NVIC_EnableIRQ( PIOA_IRQn );
151}
152
153void Phone_exit( void ) {
154 PIO_DisableIt( &pinPhoneRST ) ;
Christina Quastd52201f2015-04-13 22:38:05 +0200155 NVIC_DisableIRQ(USART1_IRQn);
Christina Quast95d66162015-04-09 22:38:47 +0200156 USART_DisableIt( USART_PHONE, US_IER_RXRDY) ;
157 USART_SetTransmitterEnabled(USART_PHONE, 0);
158 USART_SetReceiverEnabled(USART_PHONE, 0);
159}
160
161void Phone_init( void ) {
Christina Quast32906bb2015-02-24 11:35:19 +0100162 PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ;
Christina Quast8043fdd2015-03-04 18:45:30 +0100163 PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
164
Christina Quast95d66162015-04-09 22:38:47 +0200165 PIO_Configure( &pinPhoneRST, 1);
Christina Quast32906bb2015-02-24 11:35:19 +0100166
Christina Quast95d66162015-04-09 22:38:47 +0200167 PIO_EnableIt( &pinPhoneRST ) ;
Christina Quaste24b9ac2015-04-10 17:44:49 +0200168 ISO7816_Init(&usart_info, CLK_SLAVE);
Christina Quastc0aa7692015-02-25 14:02:01 +0100169
Christina Quastd52201f2015-04-13 22:38:05 +0200170 USART_SetTransmitterEnabled(USART_PHONE, 0);
Christina Quastc0aa7692015-02-25 14:02:01 +0100171 USART_SetReceiverEnabled(USART_PHONE, 1);
Christina Quastfb524b92015-02-27 13:39:45 +0100172
Christina Quast96025db2015-05-08 17:31:08 +0200173 USART_EnableIt(USART_PHONE, US_IER_RXRDY);
Christina Quastd52201f2015-04-13 22:38:05 +0200174 NVIC_EnableIRQ(USART1_IRQn);
175
Christina Quast4db82e02015-04-11 18:14:41 +0200176 receive_from_host();
Christina Quast32906bb2015-02-24 11:35:19 +0100177}
Christina Quastfb524b92015-02-27 13:39:45 +0100178
179void Phone_run( void )
180{
Christina Quast4db82e02015-04-11 18:14:41 +0200181 check_data_from_phone();
Christina Quastfb524b92015-02-27 13:39:45 +0100182}