blob: c4ce6d8d0d49f986a11ecacadc801d1cce892038 [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*/
65#define WTX_req 0b10000011
66#define WTX_req 0b10100011
67// Alternatively:
68/* For T = 0 Protocol: The firmware on receiving the NULL (0x60) Procedure byte from the card, notifies
69it to the driver using the RDR_to_PC_DataBlock response. During this period, the reception of bytes
70from the smart card is still in progress and hence the device cannot indefinitely wait for IN tokens on
71the USB bulk-in endpoint. Hence, it is required of the driver to readily supply β€˜IN’ tokens on the USB
72bulk-in endpoint. On failure to do so, some of the wait time extension responses, will not be queued to
73the driver.
74*/
75
Christina Quast32906bb2015-02-24 11:35:19 +010076/*------------------------------------------------------------------------------
77 * Internal variables
78 *------------------------------------------------------------------------------*/
79/** USB state: suspend, resume, idle */
80unsigned char USBState = STATE_IDLE;
81
82/** ISO7816 pins */
83static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE};
Christina Quast8043fdd2015-03-04 18:45:30 +010084/** Bus switch pins */
Christina Quast4bcc0232015-03-24 21:59:32 +010085//static const Pin pins_bus[] = {PINS_BUS_DEFAULT};
86// FIXME: temporary enable bus switch
87static const Pin pins_bus[] = {PINS_BUS_SNIFF};
88
Christina Quast32906bb2015-02-24 11:35:19 +010089/** ISO7816 RST pin */
90static const Pin pinIso7816RstMC = PIN_ISO7816_RST_PHONE;
91static uint8_t sim_inserted = 0;
92
93static const Pin pPwr[] = {
94 /* Enable power converter 4.5-6V to 3.3V; low: off */
95 {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT},
96
97 /* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */
98 {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}
99};
100
101
Christina Quast8e5381c2015-04-03 11:37:17 +0200102static const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
103
104#define PR TRACE_INFO
Christina Quast32906bb2015-02-24 11:35:19 +0100105
106/* ===================================================*/
107/* Taken from iso7816_4.c */
108/* ===================================================*/
109/** Flip flop for send and receive char */
110#define USART_SEND 0
111#define USART_RCV 1
Christina Quastce296b92015-03-18 18:41:19 +0100112
113#define NONE 9
114#define RST_RCVD 10
115#define WAIT_CMD_PHONE 11
116#define WAIT_CMD_PC 12
117#define WAIT_ATR 13
Christina Quast32906bb2015-02-24 11:35:19 +0100118/*-----------------------------------------------------------------------------
119 * Internal variables
120 *-----------------------------------------------------------------------------*/
121/** Variable for state of send and receive froom USART */
122static uint8_t StateUsartGlobal = USART_RCV;
Christina Quast32906bb2015-02-24 11:35:19 +0100123
Christina Quastce296b92015-03-18 18:41:19 +0100124static uint32_t state;
Christina Quastc0aa7692015-02-25 14:02:01 +0100125extern uint8_t rcvdChar;
Christina Quast32906bb2015-02-24 11:35:19 +0100126
Christina Quast0ca83902015-03-22 19:05:23 +0100127extern volatile uint8_t timeout_occured;
128
Christina Quast4bcc0232015-03-24 21:59:32 +0100129static rst_cnt = 0;
130
Christina Quast32906bb2015-02-24 11:35:19 +0100131/*-----------------------------------------------------------------------------
132 * Interrupt routines
133 *-----------------------------------------------------------------------------*/
Christina Quast578daaa2015-03-13 23:46:01 +0100134#define RESET 'R'
Christina Quast32906bb2015-02-24 11:35:19 +0100135static void ISR_PhoneRST( const Pin *pPin)
136{
Christina Quast578daaa2015-03-13 23:46:01 +0100137 printf("+++ Int!!\n\r");
Christina Quast4bcc0232015-03-24 21:59:32 +0100138 state = RST_RCVD;
139
140/* if (state == NONE || rst_cnt > 2) {
Christina Quastce296b92015-03-18 18:41:19 +0100141 state = RST_RCVD;
142 }
Christina Quast4bcc0232015-03-24 21:59:32 +0100143 rst_cnt++;
144*/
Christina Quastc0aa7692015-02-25 14:02:01 +0100145 // FIXME: What to do on reset?
Christina Quastce296b92015-03-18 18:41:19 +0100146 // FIXME: It seems like the phone is constantly sending a lot of these RSTs
Christina Quast578daaa2015-03-13 23:46:01 +0100147// PIO_DisableIt( &pinPhoneRST ) ;
Christina Quast32906bb2015-02-24 11:35:19 +0100148}
149
150static void Config_PhoneRST_IrqHandler()
151{
152 PIO_Configure( &pinPhoneRST, 1);
153// PIO_Configure( &pinPhoneClk, 1);
154 PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
155// PIO_ConfigureIt( &pinPhoneClk, ISR_PhoneRST ) ;
156 PIO_EnableIt( &pinPhoneRST ) ;
157// PIO_EnableIt( &pinPhoneClk ) ;
158 NVIC_EnableIRQ( PIOA_IRQn );
159}
160
161/**
162 * Get a character from ISO7816
163 * \param pCharToReceive Pointer for store the received char
164 * \return 0: if timeout else status of US_CSR
165 */
166/* FIXME: This code is taken from cciddriver.c
167 --> Reuse the code!!! */
168uint32_t _ISO7816_GetChar( uint8_t *pCharToReceive )
169{
170 uint32_t status;
171 uint32_t timeout=0;
172
173 TRACE_DEBUG("--");
174
175 if( StateUsartGlobal == USART_SEND ) {
176 while((USART_PHONE->US_CSR & US_CSR_TXEMPTY) == 0) {}
177 USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
178 StateUsartGlobal = USART_RCV;
179 }
180
181 /* Wait USART ready for reception */
182 while( ((USART_PHONE->US_CSR & US_CSR_RXRDY) == 0) ) {
183 if(timeout++ > 12000 * (BOARD_MCK/1000000)) {
184 TRACE_DEBUG("TimeOut\n\r");
185 return( 0 );
186 }
187 }
188
189 /* At least one complete character has been received and US_RHR has not yet been read. */
190
191 /* Get a char */
192 *pCharToReceive = ((USART_PHONE->US_RHR) & 0xFF);
193
194 status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME|
195 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
196 (1<<10)));
197
198 if (status != 0 ) {
199 TRACE_DEBUG("R:0x%X\n\r", status);
200 TRACE_DEBUG("R:0x%X\n\r", USART_PHONE->US_CSR);
201 TRACE_DEBUG("Nb:0x%X\n\r", USART_PHONE->US_NER );
202 USART_PHONE->US_CR = US_CR_RSTSTA;
203 }
204
205 /* Return status */
206 return( status );
207}
208
209/**
210 * Send a char to ISO7816
211 * \param CharToSend char to be send
212 * \return status of US_CSR
213 */
214uint32_t _ISO7816_SendChar( uint8_t CharToSend )
215{
216 uint32_t status;
217
218 TRACE_DEBUG("********** Send char: %c (0x%X)\n\r", CharToSend, CharToSend);
219
220 if( StateUsartGlobal == USART_RCV ) {
221 USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
222 StateUsartGlobal = USART_SEND;
223 }
224
225 /* Wait USART ready for transmit */
226 while((USART_PHONE->US_CSR & US_CSR_TXRDY) == 0) {}
227 /* There is no character in the US_THR */
228
229 /* Transmit a char */
230 USART_PHONE->US_THR = CharToSend;
231
232 status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME|
233 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
234 (1<<10)));
235
236 if (status != 0 ) {
Christina Quastc0aa7692015-02-25 14:02:01 +0100237 TRACE_DEBUG("******* status: 0x%X (Overrun: %lu, NACK: %lu, Timeout: %lu, underrun: %lu)\n\r",
Christina Quast32906bb2015-02-24 11:35:19 +0100238 status, ((status & US_CSR_OVRE)>> 5), ((status & US_CSR_NACK) >> 13),
239 ((status & US_CSR_TIMEOUT) >> 8), ((status & (1 << 10)) >> 10));
240
241 TRACE_DEBUG("E (USART CSR reg):0x%X\n\r", USART_PHONE->US_CSR);
242 TRACE_DEBUG("Nb (Number of errors):0x%X\n\r", USART_PHONE->US_NER );
243 USART_PHONE->US_CR = US_CR_RSTSTA;
244 }
245
246 /* Return status */
247 return( status );
248}
249
Christina Quast32906bb2015-02-24 11:35:19 +0100250void Phone_Master_Init( void ) {
251
252 PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ;
Christina Quast8043fdd2015-03-04 18:45:30 +0100253 PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
254
Christina Quast32906bb2015-02-24 11:35:19 +0100255 Config_PhoneRST_IrqHandler();
Christina Quast32906bb2015-02-24 11:35:19 +0100256
Christina Quastc0aa7692015-02-25 14:02:01 +0100257 _ISO7816_Init();
258
259 USART_SetTransmitterEnabled(USART_PHONE, 1);
260 USART_SetReceiverEnabled(USART_PHONE, 1);
Christina Quastfb524b92015-02-27 13:39:45 +0100261
262 /* Configure ISO7816 driver */
263 // FIXME: PIO_Configure(pPwr, PIO_LISTSIZE( pPwr ));
264
Christina Quastce296b92015-03-18 18:41:19 +0100265 state = NONE;
Christina Quastfb524b92015-02-27 13:39:45 +0100266
267
Christina Quast32906bb2015-02-24 11:35:19 +0100268// FIXME: Or do I need to call VBUS_CONFIGURE() here instead, which will call USBD_Connect() later?
269// USBD_Connect();
270// FIXME: USB clock? USB PMC?
271// NVIC_EnableIRQ( UDP_IRQn );
272
Christina Quast578daaa2015-03-13 23:46:01 +0100273 USART_EnableIt( USART_PHONE, US_IER_RXRDY) ;
Christina Quast32906bb2015-02-24 11:35:19 +0100274
275 // FIXME: At some point USBD_IrqHandler() should get called and set USBD_STATE_CONFIGURED
276 /* while (USBD_GetState() < USBD_STATE_CONFIGURED) {
277 int i = 1;
278 if ((i%10000) == 0) {
279 TRACE_DEBUG("%d: USB State: %x\n\r", i, USBD_GetState());
280 }
281 i++;
282 }
283*/
284
Christina Quast0ca83902015-03-22 19:05:23 +0100285 Timer_Init();
Christina Quast32906bb2015-02-24 11:35:19 +0100286}
Christina Quastfb524b92015-02-27 13:39:45 +0100287
Christina Quastce296b92015-03-18 18:41:19 +0100288void send_ATR(uint8_t *ATR, uint8_t status, uint32_t transferred, uint32_t remaining)
Christina Quast578daaa2015-03-13 23:46:01 +0100289{
290 int i;
Christina Quastce296b92015-03-18 18:41:19 +0100291 TRACE_INFO("Send %x %x %x.. %x", ATR[0], ATR[1], ATR[2], ATR[transferred-1]);
292 for ( i = 0; i < transferred; i++ ) {
Christina Quast578daaa2015-03-13 23:46:01 +0100293 _ISO7816_SendChar(*(ATR++));
294 }
Christina Quastce296b92015-03-18 18:41:19 +0100295 state = WAIT_CMD_PHONE;
Christina Quast578daaa2015-03-13 23:46:01 +0100296}
297
Christina Quastce296b92015-03-18 18:41:19 +0100298void sendResponse( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
Christina Quast578daaa2015-03-13 23:46:01 +0100299{
Christina Quastce296b92015-03-18 18:41:19 +0100300 int i;
Christina Quast0ca83902015-03-22 19:05:23 +0100301 TRACE_INFO("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, transferred, remaining);
Christina Quastce296b92015-03-18 18:41:19 +0100302 TRACE_INFO("Resp: %x %x %x .. %x", pArg[0], pArg[1], pArg[2], pArg[transferred-1]);
Christina Quast578daaa2015-03-13 23:46:01 +0100303
Christina Quastce296b92015-03-18 18:41:19 +0100304 for ( i = 0; i < transferred; i++ ) {
305 _ISO7816_SendChar(*(pArg++));
Christina Quast578daaa2015-03-13 23:46:01 +0100306 }
Christina Quast4bcc0232015-03-24 21:59:32 +0100307/*
308 if (*(pArg-1) == 0x8A) {
309 for (i=0; i<20000; i++) ;
310 _ISO7816_SendChar(0x90);
311 _ISO7816_SendChar(0x00);
312 }
313*/
Christina Quastce296b92015-03-18 18:41:19 +0100314 state = WAIT_CMD_PHONE;
Christina Quast578daaa2015-03-13 23:46:01 +0100315}
316
Christina Quast578daaa2015-03-13 23:46:01 +0100317extern ring_buffer buf;
318#define MAX_MSG_LEN 64
Christina Quastce296b92015-03-18 18:41:19 +0100319#define PR printf
320
321void wait_for_response(uint8_t pBuffer[]) {
322 int ret = 0;
323// uint8_t msg[] = {0xa0, 0xa4, 0x0, 0x0, 0x2};
324 if (rcvdChar != 0) {
325 printf(" rr ");
326 /* DATA_IN for host side is data_out for simtrace side */
327 /* FIXME: Performancewise sending a USB packet for every byte is a disaster */
Christina Quast0ca83902015-03-22 19:05:23 +0100328 ret = USBD_Write( DATAIN, buf.buf, BUFLEN, 0, 0 );
Christina Quastce296b92015-03-18 18:41:19 +0100329 //USBD_Write( DATAIN, msg, BUFLEN, 0, 0 );
Christina Quast0ca83902015-03-22 19:05:23 +0100330 PR("b:%x %x %x %x %x.\n\r", buf.buf[0], buf.buf[1],buf.buf[2], buf.buf[3], buf.buf[4]);
Christina Quastce296b92015-03-18 18:41:19 +0100331
332 rcvdChar = 0;
Christina Quast4bcc0232015-03-24 21:59:32 +0100333 } else if (timeout_occured && buf.idx != 0) {
Christina Quast0ca83902015-03-22 19:05:23 +0100334 printf(" to ");
335 ret = USBD_Write( DATAIN, buf.buf, buf.idx, 0, 0 );
336 timeout_occured = 0;
337 buf.idx = 0;
338 rcvdChar = 0;
339 PR("b:%x %x %x %x %x.\n\r", buf.buf[0], buf.buf[1],buf.buf[2], buf.buf[3], buf.buf[4]);
340 } else {
341 printf(".");
342 return;
343 }
344 if ((ret = USBD_Read(DATAOUT, pBuffer, MAX_MSG_LEN,
345 (TransferCallback)&sendResponse, pBuffer)) == USBD_STATUS_SUCCESS) {
346 TRACE_INFO("wait_rsp\n\r");
347// state = WAIT_CMD_PC;
348 buf.idx = 0;
349 TC0_Counter_Reset();
350 } else {
351 TRACE_INFO("USB Err: %X", ret);
352 return;
Christina Quastce296b92015-03-18 18:41:19 +0100353 }
354}
355
356// Sniffed Phone to SIM card communication:
357// phone > sim : RST
358// phone < sim : ATR
359// phone > sim : A0 A4 00 00 02 (Select File)
360// phone < sim : A4 (INS repeated)
361// phone > sim : 7F 02 (= ??)
362// phone < sim : 9F 16 (9F: success, can deliver 0x16 (=22) byte)
363// phone > sim : ?? (A0 C0 00 00 16)
364// phone < sim : C0 (INS repeated)
365// 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)
366// phone <? sim : 90 00 (OK, everything went fine)
367// phone ? sim : 00 (??)
Christina Quast578daaa2015-03-13 23:46:01 +0100368
Christina Quastfb524b92015-02-27 13:39:45 +0100369void Phone_run( void )
370{
Christina Quast578daaa2015-03-13 23:46:01 +0100371 int ret;
372 uint8_t pBuffer[MAX_MSG_LEN];
Christina Quastce296b92015-03-18 18:41:19 +0100373 int msg = RESET;
374// FIXME: remove:
375// uint8_t ATR[] = {0x3B, 0x9A, 0x94, 0x00, 0x92, 0x02, 0x75, 0x93, 0x11, 0x00, 0x01, 0x02, 0x02, 0x19};
376// send_ATR(ATR, (sizeof(ATR)/sizeof(ATR[0])));
Christina Quastce296b92015-03-18 18:41:19 +0100377 switch (state) {
378 case RST_RCVD:
379 USBD_Write( INT, &msg, 1, 0, 0 );
Christina Quast4bcc0232015-03-24 21:59:32 +0100380 TC0_Counter_Reset();
Christina Quastce296b92015-03-18 18:41:19 +0100381 // send_ATR sets state to WAIT_CMD
382 if ((ret = USBD_Read(DATAOUT, pBuffer, MAX_MSG_LEN, (TransferCallback)&send_ATR, pBuffer)) == USBD_STATUS_SUCCESS) {
383 TRACE_INFO("Reading started sucessfully (ATR)");
384 state = WAIT_ATR;
385 } else {
386 TRACE_INFO("USB Error: %X", ret);
387//FIXME: state = ERR;
388 }
389 break;
390 case WAIT_CMD_PHONE:
Christina Quast0ca83902015-03-22 19:05:23 +0100391// FIXME: TC0_Counter_Reset();
Christina Quastce296b92015-03-18 18:41:19 +0100392 wait_for_response(pBuffer);
393 break;
Christina Quast0ca83902015-03-22 19:05:23 +0100394 case NONE:
395 break;
Christina Quastce296b92015-03-18 18:41:19 +0100396 default:
Christina Quast0ca83902015-03-22 19:05:23 +0100397// TRACE_INFO(":(");
Christina Quastce296b92015-03-18 18:41:19 +0100398 break;
Christina Quast578daaa2015-03-13 23:46:01 +0100399 }
400
Christina Quastfb524b92015-02-27 13:39:45 +0100401 // FIXME: Function Phone_run not implemented yet
402
403 /* Send and receive chars */
404 // ISO7816_GetChar(&rcv_char);
405 // ISO7816_SendChar(char_to_send);
406}