blob: 82ba6128e938dc7d4896d8ebfd7d094b2fd2c990 [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 Quast578daaa2015-03-13 23:46:01 +010056/* 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 Quasta4d1d162015-04-06 20:41:50 +020065// FIXME: Two times the same name for the define, which one is right?
66//#define WTX_req 0b10000011
67//#define WTX_req 0b10100011
Christina Quast578daaa2015-03-13 23:46:01 +010068// Alternatively:
69/* For T = 0 Protocol: The firmware on receiving the NULL (0x60) Procedure byte from the card, notifies
70it to the driver using the RDR_to_PC_DataBlock response. During this period, the reception of bytes
71from the smart card is still in progress and hence the device cannot indefinitely wait for IN tokens on
72the USB bulk-in endpoint. Hence, it is required of the driver to readily supply β€˜IN’ tokens on the USB
73bulk-in endpoint. On failure to do so, some of the wait time extension responses, will not be queued to
74the driver.
75*/
Christina Quast4db82e02015-04-11 18:14:41 +020076extern volatile uint8_t timeout_occured;
Christina Quast578daaa2015-03-13 23:46:01 +010077
Christina Quast32906bb2015-02-24 11:35:19 +010078/*------------------------------------------------------------------------------
79 * Internal variables
80 *------------------------------------------------------------------------------*/
81/** USB state: suspend, resume, idle */
82unsigned char USBState = STATE_IDLE;
83
84/** ISO7816 pins */
85static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE};
Christina Quast8043fdd2015-03-04 18:45:30 +010086/** Bus switch pins */
Christina Quast5c6a2992015-04-11 20:03:14 +020087
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
91static const Pin pins_bus[] = {PINS_BUS_SNIFF};
92#else
Christina Quast30418542015-04-05 10:08:06 +020093static const Pin pins_bus[] = {PINS_BUS_DEFAULT};
Christina Quast5c6a2992015-04-11 20:03:14 +020094#endif
Christina Quast4bcc0232015-03-24 21:59:32 +010095
Christina Quast32906bb2015-02-24 11:35:19 +010096/** ISO7816 RST pin */
97static const Pin pinIso7816RstMC = PIN_ISO7816_RST_PHONE;
98static uint8_t sim_inserted = 0;
99
100static 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 Quast8e5381c2015-04-03 11:37:17 +0200109static const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
110
Christina Quastec9c09e2015-04-16 10:45:39 +0200111static struct Usart_info usart_info = {.base = USART_PHONE, .id = ID_USART_PHONE, .state = USART_RCV};
Christina Quaste24b9ac2015-04-10 17:44:49 +0200112
Christina Quast8e5381c2015-04-03 11:37:17 +0200113#define PR TRACE_INFO
Christina Quast32906bb2015-02-24 11:35:19 +0100114
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 Quastce296b92015-03-18 18:41:19 +0100121
Christina Quast4db82e02015-04-11 18:14:41 +0200122// FIXME: Comments
Christina Quast32906bb2015-02-24 11:35:19 +0100123/*-----------------------------------------------------------------------------
124 * Internal variables
125 *-----------------------------------------------------------------------------*/
Christina Quast4db82e02015-04-11 18:14:41 +0200126static uint8_t host_to_sim_buf[BUFLEN];
Christina Quast0ca83902015-03-22 19:05:23 +0100127
Christina Quast32906bb2015-02-24 11:35:19 +0100128/*-----------------------------------------------------------------------------
129 * Interrupt routines
130 *-----------------------------------------------------------------------------*/
Christina Quastdcce4c32015-04-13 22:20:12 +0200131void Callback_PhoneRST_ISR( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
132{
133 printf("rstCB\n\r");
134 PIO_EnableIt( &pinPhoneRST ) ;
135}
Christina Quast32906bb2015-02-24 11:35:19 +0100136static void ISR_PhoneRST( const Pin *pPin)
137{
Christina Quast4db82e02015-04-11 18:14:41 +0200138 int ret;
139 // FIXME: no printfs in ISRs?
Christina Quastd3630cc2015-04-03 11:38:24 +0200140 printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR);
141 if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) )
142 {
143 if(PIO_Get( &pinPhoneRST ) == 0) {
144 printf(" 0 ");
145 } else {
146 printf(" 1 ");
147 }
148 }
Christina Quast4db82e02015-04-11 18:14:41 +0200149
Christina Quastdcce4c32015-04-13 22:20:12 +0200150 if ((ret = USBD_Write( PHONE_INT, "R", 1, (TransferCallback)&Callback_PhoneRST_ISR, 0 )) != USBD_STATUS_SUCCESS) {
Christina Quast4db82e02015-04-11 18:14:41 +0200151 TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__);
152 return;
153 }
Christina Quast32906bb2015-02-24 11:35:19 +0100154
Christina Quast95d66162015-04-09 22:38:47 +0200155 /* Interrupt enabled after ATR is sent to phone */
Christina Quastdcce4c32015-04-13 22:20:12 +0200156 PIO_DisableIt( &pinPhoneRST ) ;
Christina Quast32906bb2015-02-24 11:35:19 +0100157}
158
Christina Quast4db82e02015-04-11 18:14:41 +0200159void receive_from_host( void );
160void sendResponse_to_phone( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
161{
162 if (status != USBD_STATUS_SUCCESS) {
163 TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status);
164 return;
165 }
166 PR("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, transferred, remaining);
Christina Quast5c6a2992015-04-11 20:03:14 +0200167 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 Quast4db82e02015-04-11 18:14:41 +0200168
Christina Quastd52201f2015-04-13 22:38:05 +0200169 USART_SetReceiverEnabled(USART_PHONE, 0);
170 USART_SetTransmitterEnabled(USART_PHONE, 1);
Christina Quast4db82e02015-04-11 18:14:41 +0200171 for (uint32_t i = 0; i < transferred; i++ ) {
Christina Quastb595e7c2015-04-12 13:31:01 +0200172 ISO7816_SendChar(host_to_sim_buf[i], &usart_info);
Christina Quast4db82e02015-04-11 18:14:41 +0200173 }
Christina Quastd52201f2015-04-13 22:38:05 +0200174 USART_SetTransmitterEnabled(USART_PHONE, 0);
175 USART_SetReceiverEnabled(USART_PHONE, 1);
Christina Quast4db82e02015-04-11 18:14:41 +0200176
177 receive_from_host();
178}
179
180void receive_from_host()
181{
182 int ret;
183 if ((ret = USBD_Read(PHONE_DATAOUT, &host_to_sim_buf, sizeof(host_to_sim_buf),
184 (TransferCallback)&sendResponse_to_phone, 0)) == USBD_STATUS_SUCCESS) {
185 } else {
Christina Quast5c6a2992015-04-11 20:03:14 +0200186 TRACE_ERROR("USB Err: %X\n", ret);
Christina Quast4db82e02015-04-11 18:14:41 +0200187 }
188}
Christina Quast32906bb2015-02-24 11:35:19 +0100189
Christina Quast95d66162015-04-09 22:38:47 +0200190void Phone_configure( void ) {
191 PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
192 NVIC_EnableIRQ( PIOA_IRQn );
193}
194
195void Phone_exit( void ) {
196 PIO_DisableIt( &pinPhoneRST ) ;
Christina Quastd52201f2015-04-13 22:38:05 +0200197 NVIC_DisableIRQ(USART1_IRQn);
Christina Quast95d66162015-04-09 22:38:47 +0200198 USART_DisableIt( USART_PHONE, US_IER_RXRDY) ;
199 USART_SetTransmitterEnabled(USART_PHONE, 0);
200 USART_SetReceiverEnabled(USART_PHONE, 0);
201}
202
203void Phone_init( void ) {
Christina Quast32906bb2015-02-24 11:35:19 +0100204 PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ;
Christina Quast8043fdd2015-03-04 18:45:30 +0100205 PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
206
Christina Quast95d66162015-04-09 22:38:47 +0200207 PIO_Configure( &pinPhoneRST, 1);
Christina Quast32906bb2015-02-24 11:35:19 +0100208
Christina Quast95d66162015-04-09 22:38:47 +0200209 PIO_EnableIt( &pinPhoneRST ) ;
Christina Quaste24b9ac2015-04-10 17:44:49 +0200210 ISO7816_Init(&usart_info, CLK_SLAVE);
Christina Quastc0aa7692015-02-25 14:02:01 +0100211
Christina Quastd52201f2015-04-13 22:38:05 +0200212 USART_SetTransmitterEnabled(USART_PHONE, 0);
Christina Quastc0aa7692015-02-25 14:02:01 +0100213 USART_SetReceiverEnabled(USART_PHONE, 1);
Christina Quastfb524b92015-02-27 13:39:45 +0100214
Christina Quastd52201f2015-04-13 22:38:05 +0200215 USART_EnableIt(USART_PHONE, US_IER_RXRDY); // TODO: interrupt enable/disable is shared with sniffer
216 NVIC_EnableIRQ(USART1_IRQn);
217
Christina Quastfb524b92015-02-27 13:39:45 +0100218 /* Configure ISO7816 driver */
219 // FIXME: PIO_Configure(pPwr, PIO_LISTSIZE( pPwr ));
220
Christina Quast32906bb2015-02-24 11:35:19 +0100221// FIXME: Or do I need to call VBUS_CONFIGURE() here instead, which will call USBD_Connect() later?
222// USBD_Connect();
Christina Quast32906bb2015-02-24 11:35:19 +0100223
Christina Quast4db82e02015-04-11 18:14:41 +0200224 //Timer_Init();
225
226 receive_from_host();
Christina Quast32906bb2015-02-24 11:35:19 +0100227}
Christina Quastfb524b92015-02-27 13:39:45 +0100228
Christina Quastaab3d7c2015-04-09 17:02:17 +0200229
Christina Quastce296b92015-03-18 18:41:19 +0100230// Sniffed Phone to SIM card communication:
231// phone > sim : RST
232// phone < sim : ATR
233// phone > sim : A0 A4 00 00 02 (Select File)
234// phone < sim : A4 (INS repeated)
235// phone > sim : 7F 02 (= ??)
236// phone < sim : 9F 16 (9F: success, can deliver 0x16 (=22) byte)
237// phone > sim : ?? (A0 C0 00 00 16)
238// phone < sim : C0 (INS repeated)
239// 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)
240// phone <? sim : 90 00 (OK, everything went fine)
241// phone ? sim : 00 (??)
Christina Quast578daaa2015-03-13 23:46:01 +0100242
Christina Quastfb524b92015-02-27 13:39:45 +0100243void Phone_run( void )
244{
Christina Quast4db82e02015-04-11 18:14:41 +0200245 check_data_from_phone();
Christina Quastfb524b92015-02-27 13:39:45 +0100246}