blob: ac0edb79913e959b0a683f5be8902d82dbc55206 [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 */
Harald Welte7dd3dfd2016-03-03 12:32:04 +010065static const Pin pinsISO7816_PHONE[] = { PINS_ISO7816_PHONE };
66
Christina Quast8043fdd2015-03-04 18:45:30 +010067/** Bus switch pins */
Christina Quast5c6a2992015-04-11 20:03:14 +020068
69#if DEBUG_PHONE_SNIFF
Harald Welte7dd3dfd2016-03-03 12:32:04 +010070#warning "Debug phone sniff via logic analyzer is enabled"
Christina Quast5c6a2992015-04-11 20:03:14 +020071// Logic analyzer probes are easier to attach to the SIM card slot
Harald Welte7dd3dfd2016-03-03 12:32:04 +010072static const Pin pins_bus[] = { PINS_BUS_SNIFF };
Christina Quast5c6a2992015-04-11 20:03:14 +020073#else
Harald Welte7dd3dfd2016-03-03 12:32:04 +010074static const Pin pins_bus[] = { PINS_BUS_DEFAULT };
Christina Quast5c6a2992015-04-11 20:03:14 +020075#endif
Christina Quast4bcc0232015-03-24 21:59:32 +010076
Christina Quast32906bb2015-02-24 11:35:19 +010077/** ISO7816 RST pin */
Christina Quast32906bb2015-02-24 11:35:19 +010078static uint8_t sim_inserted = 0;
79
80static const Pin pPwr[] = {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010081 /* 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 Quast32906bb2015-02-24 11:35:19 +010086};
87
Christina Quaste4cbfe62015-04-20 13:07:28 +020088const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
Christina Quast8e5381c2015-04-03 11:37:17 +020089
Harald Welte7dd3dfd2016-03-03 12:32:04 +010090static struct Usart_info usart_info = {
91 .base = USART_PHONE,
92 .id = ID_USART_PHONE,
93 .state = USART_RCV,
94};
Christina Quaste24b9ac2015-04-10 17:44:49 +020095
Christina Quast32906bb2015-02-24 11:35:19 +010096/* ===================================================*/
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 Quastce296b92015-03-18 18:41:19 +0100102
Christina Quast32906bb2015-02-24 11:35:19 +0100103/*-----------------------------------------------------------------------------
104 * Internal variables
105 *-----------------------------------------------------------------------------*/
Christina Quast4db82e02015-04-11 18:14:41 +0200106static uint8_t host_to_sim_buf[BUFLEN];
Christina Quastcb646bc2015-05-03 14:21:26 +0200107static bool change_fidi = false;
Christina Quast0ca83902015-03-22 19:05:23 +0100108
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100109static void receive_from_host(void);
110static void sendResponse_to_phone(uint8_t * pArg, uint8_t status,
111 uint32_t transferred, uint32_t remaining)
Christina Quast4db82e02015-04-11 18:14:41 +0200112{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100113 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 Quast4db82e02015-04-11 18:14:41 +0200122
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100123 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 Quast4db82e02015-04-11 18:14:41 +0200137
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100138 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 Quastcb646bc2015-05-03 14:21:26 +0200143
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100144 receive_from_host();
Christina Quast4db82e02015-04-11 18:14:41 +0200145}
146
Harald Welteec4fe232015-11-07 18:41:25 +0100147static void receive_from_host()
Christina Quast4db82e02015-04-11 18:14:41 +0200148{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100149 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 Quast4db82e02015-04-11 18:14:41 +0200157}
Christina Quast32906bb2015-02-24 11:35:19 +0100158
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100159void Phone_configure(void)
Christina Quastfb524b92015-02-27 13:39:45 +0100160{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100161 PIO_ConfigureIt(&pinPhoneRST, ISR_PhoneRST);
162 NVIC_EnableIRQ(PIOA_IRQn);
163}
164
165void 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
174void 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
193void Phone_run(void)
194{
195 check_data_from_phone();
Christina Quastfb524b92015-02-27 13:39:45 +0100196}