gnuk/src/usb-icc.c

1284 lines
32 KiB
C
Raw Normal View History

2010-08-18 03:57:45 +00:00
/*
2010-08-19 08:09:59 +00:00
* usb-icc.c -- USB CCID/ICCD protocol handling
2010-08-18 03:57:45 +00:00
*
2012-01-16 04:24:31 +00:00
* Copyright (C) 2010, 2011, 2012 Free Software Initiative of Japan
2010-08-18 03:57:45 +00:00
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Gnuk, a GnuPG USB Token implementation.
*
* Gnuk is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
2010-09-04 04:48:26 +00:00
#include "config.h"
2010-08-18 05:21:58 +00:00
#include "ch.h"
#include "hal.h"
2010-08-19 08:09:59 +00:00
#include "gnuk.h"
2010-08-18 05:21:58 +00:00
#include "usb_lib.h"
#include "usb_mem.h"
#include "hw_config.h"
#include "usb_istr.h"
2012-01-16 03:17:45 +00:00
#include "usb_lld.h"
2010-08-18 05:21:58 +00:00
2012-01-20 09:18:23 +00:00
/*
* USB buffer size of USB-ICC driver
*/
#if MAX_RES_APDU_DATA_SIZE > MAX_CMD_APDU_DATA_SIZE
#define USB_BUF_SIZE (MAX_RES_APDU_DATA_SIZE+5)
#else
#define USB_BUF_SIZE (MAX_CMD_APDU_DATA_SIZE+5)
#endif
2012-01-18 09:59:16 +00:00
struct apdu apdu;
2012-01-20 09:18:23 +00:00
/*
* There are three layers in USB CCID implementation
*
* +-------------------+
* | Application Layer |
* +-------------------+
* ^ command APDU |
* | v response APDU
* +-------------------+
* | CCID Layer |
* +-------------------+
* ^ CCID PC_to_RDR | CCID RDR_to_PC
* | Message v Message
* +-------------------+
* | USB Layer |
* +-------------------+
* ^ USB | USB
* | Bulk-OUT Packet v Bulk-IN Packet
*
*/
/*
* USB layer data structures
*/
struct ep_in {
uint8_t ep_num;
uint8_t tx_done;
void (*notify) (struct ep_in *epi);
const uint8_t *buf;
size_t cnt;
size_t buf_len;
void *priv;
void (*next_buf) (struct ep_in *epi, size_t len);
};
static void epi_init (struct ep_in *epi, int ep_num,
void (*notify) (struct ep_in *epi), void *priv)
{
epi->ep_num = ep_num;
epi->tx_done = 0;
epi->notify = notify;
epi->buf = NULL;
epi->cnt = 0;
epi->buf_len = 0;
epi->priv = priv;
epi->next_buf = NULL;
}
struct ep_out {
uint8_t ep_num;
uint8_t err;
void (*notify) (struct ep_out *epo);
uint8_t *buf;
size_t cnt;
size_t buf_len;
void *priv;
void (*next_buf) (struct ep_out *epo, size_t len);
int (*end_rx) (struct ep_out *epo, size_t orig_len);
};
static struct ep_out endpoint_out;
static struct ep_in endpoint_in;
static void epo_init (struct ep_out *epo, int ep_num,
void (*notify) (struct ep_out *epo), void *priv)
{
epo->ep_num = ep_num;
epo->err = 0;
epo->notify = notify;
epo->buf = NULL;
epo->cnt = 0;
epo->buf_len = 0;
epo->priv = priv;
epo->next_buf = NULL;
epo->end_rx = NULL;
}
/*
* CCID Layer
*/
/*
* Buffer of USB communication: for both of RX and TX
*
* The buffer will be filled by multiple RX packets (Bulk-OUT)
* or will be used for multiple TX packets (Bulk-IN)
*/
static uint8_t icc_buffer[USB_BUF_SIZE];
2010-11-12 14:19:27 +00:00
#define ICC_SET_PARAMS 0x61 /* non-ICCD command */
2010-08-23 05:40:33 +00:00
#define ICC_POWER_ON 0x62
#define ICC_POWER_OFF 0x63
2010-11-12 14:19:27 +00:00
#define ICC_SLOT_STATUS 0x65 /* non-ICCD command */
2011-01-04 12:06:55 +00:00
#define ICC_SECURE 0x69 /* non-ICCD command */
2010-11-12 14:19:27 +00:00
#define ICC_GET_PARAMS 0x6C /* non-ICCD command */
2010-08-23 05:40:33 +00:00
#define ICC_XFR_BLOCK 0x6F
#define ICC_DATA_BLOCK_RET 0x80
2010-11-12 14:19:27 +00:00
#define ICC_SLOT_STATUS_RET 0x81 /* non-ICCD result */
#define ICC_PARAMS_RET 0x82 /* non-ICCD result */
2010-08-23 05:40:33 +00:00
#define ICC_MSG_SEQ_OFFSET 6
#define ICC_MSG_STATUS_OFFSET 7
#define ICC_MSG_ERROR_OFFSET 8
#define ICC_MSG_CHAIN_OFFSET 9
2010-12-07 05:34:25 +00:00
#define ICC_MSG_DATA_OFFSET 10 /* == ICC_MSG_HEADER_SIZE */
2012-01-20 09:18:23 +00:00
#define ICC_MAX_MSG_DATA_SIZE USB_BUF_SIZE
2010-08-23 05:40:33 +00:00
#define ICC_STATUS_RUN 0x00
#define ICC_STATUS_PRESENT 0x01
#define ICC_STATUS_NOTPRESENT 0x02
#define ICC_CMD_STATUS_OK 0x00
#define ICC_CMD_STATUS_ERROR 0x40
#define ICC_CMD_STATUS_TIMEEXT 0x80
#define ICC_ERROR_XFR_OVERRUN 0xFC
2010-09-16 02:03:14 +00:00
/*
* Since command-byte is at offset 0,
* error with offset 0 means "command not supported".
*/
#define ICC_OFFSET_CMD_NOT_SUPPORTED 0
2012-01-20 09:18:23 +00:00
#define ICC_OFFSET_DATA_LEN 1
2010-09-16 02:03:14 +00:00
#define ICC_OFFSET_PARAM 8
2010-08-23 05:40:33 +00:00
struct icc_header {
uint8_t msg_type;
2012-01-20 09:18:23 +00:00
uint32_t data_len;
2010-08-23 05:40:33 +00:00
uint8_t slot;
uint8_t seq;
uint8_t rsvd;
uint16_t param;
} __attribute__((packed));
2012-01-20 09:18:23 +00:00
enum icc_state *icc_state_p;
/* Data structure handled by CCID layer */
struct ccid {
enum icc_state icc_state;
uint8_t state;
uint8_t *p;
size_t len;
uint8_t err;
struct icc_header icc_header;
uint8_t sw1sw2[2];
Thread *icc_thread;
Thread *application;
/* lower layer */
struct ep_out *epo;
struct ep_in *epi;
/* upper layer */
struct apdu *a;
};
2010-08-23 05:40:33 +00:00
2010-12-07 05:34:25 +00:00
/*
2012-01-20 09:18:23 +00:00
* APDU_STATE_WAIT_COMMAND +---------+
* | | | |
* | v v |
* | APDU_STATE_COMMAND_CHAINING --+
* | |
* v v
* APDU_STATE_COMMAND_RECEIVED
* |
* v
* ===================
* | Process COMMAND |
* ===================
* |
* v
* +-----+----------+ +---------+
* | | | |
* v v v |
* APDU_STATE_RESULT <---- APDU_STATE_RESULT_GET_RESPONSE --+
* |
* |
* v
* APDU_STATE_WAIT_COMMAND
*/
#define APDU_STATE_WAIT_COMMAND 0
#define APDU_STATE_COMMAND_CHAINING 1
#define APDU_STATE_COMMAND_RECEIVED 2
#define APDU_STATE_RESULT 3
#define APDU_STATE_RESULT_GET_RESPONSE 4
static void ccid_init (struct ccid *c, struct ep_in *epi, struct ep_out *epo,
struct apdu *a, Thread *t)
{
icc_state_p = &c->icc_state;
c->icc_state = ICC_STATE_START;
c->state = APDU_STATE_WAIT_COMMAND;
/*
* Note: a is not yet initialized yet, we can't use c->a->cmd_apdu_data here.
*/
c->p = &icc_buffer[5];
c->len = MAX_CMD_APDU_DATA_SIZE;
c->err = 0;
memset (&c->icc_header, 0, sizeof (struct icc_header));
c->sw1sw2[0] = 0x90;
c->sw1sw2[1] = 0x00;
c->icc_thread = t;
c->application = NULL;
c->epi = epi;
c->epo = epo;
c->a = a;
}
/*
* Application layer
*/
/*
* USB-CCID communication could be considered "half duplex".
2010-12-07 05:34:25 +00:00
*
2010-12-07 06:52:50 +00:00
* While the device is sending something, there is no possibility for
* the device to receive anything.
*
* While the device is receiving something, there is no possibility
* for the device to send anything.
*
2010-12-07 05:34:25 +00:00
* Thus, the buffer can be shared for RX and TX.
*
2012-01-20 09:18:23 +00:00
* Exception: When we support ABORT of CCID, it is possible to receive
* ABORT Class Specific Request to control pipe while we are
* receiving/sending something at OUT/IN endpoint.
*
2010-12-07 05:34:25 +00:00
*/
2012-01-20 09:18:23 +00:00
#define CMD_APDU_HEAD_SIZE 5
2010-08-23 05:40:33 +00:00
2012-01-20 09:18:23 +00:00
static void apdu_init (struct apdu *a)
{
a->seq = 0; /* will be set by lower layer */
a->cmd_apdu_head = &icc_buffer[0];
a->cmd_apdu_data = &icc_buffer[5];
a->cmd_apdu_data_len = 0; /* will be set by lower layer */
a->expected_res_size = 0; /* will be set by lower layer */
a->sw = 0x9000; /* will be set by upper layer */
a->res_apdu_data = &icc_buffer[5]; /* will be set by upper layer */
a->res_apdu_data_len = 0; /* will be set by upper layer */
}
2011-05-11 07:46:21 +00:00
2010-08-19 08:09:59 +00:00
#define EV_RX_DATA_READY (eventmask_t)1 /* USB Rx data available */
2010-12-07 05:34:25 +00:00
/* EV_EXEC_FINISHED == 2 */
#define EV_TX_FINISHED (eventmask_t)4 /* USB Tx finished */
2010-08-19 08:09:59 +00:00
2012-01-20 09:18:23 +00:00
static void notify_tx (struct ep_in *epi)
{
struct ccid *c = (struct ccid *)epi->priv;
/* The sequence of Bulk-IN transactions finished */
chEvtSignalI (c->icc_thread, EV_TX_FINISHED);
}
static void no_buf (struct ep_in *epi, size_t len)
{
(void)len;
epi->buf = NULL;
epi->cnt = 0;
epi->buf_len = 0;
}
static void set_sw1sw2 (struct ep_in *epi)
{
struct ccid *c = (struct ccid *)epi->priv;
if (c->a->expected_res_size >= c->len)
{
c->sw1sw2[0] = 0x90;
c->sw1sw2[1] = 0x00;
}
else
{
c->sw1sw2[0] = 0x61;
if (c->len >= 256)
c->sw1sw2[1] = 0;
else
c->sw1sw2[1] = (uint8_t)c->len;
}
}
static void get_sw1sw2 (struct ep_in *epi, size_t len)
{
struct ccid *c = (struct ccid *)epi->priv;
(void)len;
epi->buf = c->sw1sw2;
epi->cnt = 0;
epi->buf_len = 2;
epi->next_buf = no_buf;
}
2010-08-19 08:09:59 +00:00
/*
2010-12-07 05:34:25 +00:00
* Tx done callback
2010-08-19 08:09:59 +00:00
*/
2010-08-18 05:21:58 +00:00
void
2010-09-04 09:44:01 +00:00
EP1_IN_Callback (void)
2010-08-18 05:21:58 +00:00
{
2012-01-20 09:18:23 +00:00
struct ep_in *epi = &endpoint_in;
if (epi->buf == NULL)
if (epi->tx_done)
epi->notify (epi);
else
{
epi->tx_done = 1;
usb_lld_tx_enable (epi->ep_num, 0); /* send ZLP */
}
2010-12-07 05:34:25 +00:00
else
2010-08-23 05:40:33 +00:00
{
2012-01-20 09:18:23 +00:00
int tx_size = 0;
size_t remain = USB_LL_BUF_SIZE;
int offset = 0;
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
while (epi->buf)
if (epi->buf_len < remain)
{
usb_lld_txcpy (epi->buf, epi->ep_num, offset, epi->buf_len);
offset += epi->buf_len;
remain -= epi->buf_len;
tx_size += epi->buf_len;
epi->next_buf (epi, remain); /* Update epi->buf, cnt, buf_len */
}
else
{
usb_lld_txcpy (epi->buf, epi->ep_num, offset, remain);
epi->buf += remain;
epi->cnt += remain;
epi->buf_len -= remain;
tx_size += remain;
break;
}
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
if (tx_size < USB_LL_BUF_SIZE)
epi->tx_done = 1;
usb_lld_tx_enable (epi->ep_num, tx_size);
2010-08-23 05:40:33 +00:00
}
2010-12-07 05:34:25 +00:00
}
2012-01-20 09:18:23 +00:00
static void notify_icc (struct ep_out *epo)
2010-12-07 05:34:25 +00:00
{
2012-01-20 09:18:23 +00:00
struct ccid *c = (struct ccid *)epo->priv;
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
c->err = epo->err;
chEvtSignalI (c->icc_thread, EV_RX_DATA_READY);
2010-08-18 05:21:58 +00:00
}
2012-01-20 09:18:23 +00:00
static int end_icc_rx (struct ep_out *epo, size_t orig_len)
2010-08-18 05:21:58 +00:00
{
2012-01-20 09:18:23 +00:00
(void)orig_len;
if (epo->cnt < sizeof (struct icc_header))
/* short packet, just ignore */
return 1;
2010-08-18 05:21:58 +00:00
2012-01-20 09:18:23 +00:00
/* icc message with no abdata */
return 0;
}
static int end_notify (struct ep_out *epo, size_t orig_len)
{
(void)epo;
(void)orig_len;
return 0;
}
static int end_cmd_apdu_head (struct ep_out *epo, size_t orig_len)
{
struct ccid *c = (struct ccid *)epo->priv;
(void)orig_len;
if (epo->cnt < 4 || epo->cnt != c->icc_header.data_len)
epo->err = 1;
if (epo->cnt == 4)
{
/* No Lc and Le */
c->a->cmd_apdu_data_len = 0;
c->a->expected_res_size = 0;
}
else if (epo->cnt == 5)
{
/* No Lc but Le */
c->a->cmd_apdu_data_len = 0;
c->a->expected_res_size = c->a->cmd_apdu_head[4];
if (c->a->expected_res_size == 0)
c->a->expected_res_size = 256;
c->a->cmd_apdu_head[4] = 0;
2011-12-28 03:27:16 +00:00
}
2012-01-20 09:18:23 +00:00
return 0;
}
2011-12-28 03:27:16 +00:00
2012-01-20 09:18:23 +00:00
static int end_nomore_data (struct ep_out *epo, size_t orig_len)
{
(void)epo;
if (orig_len == USB_LL_BUF_SIZE)
return 1;
2011-12-28 03:27:16 +00:00
else
2012-01-20 09:18:23 +00:00
return 0;
}
static int end_cmd_apdu_data (struct ep_out *epo, size_t orig_len)
{
struct ccid *c = (struct ccid *)epo->priv;
size_t len = epo->cnt;
if (orig_len == USB_LL_BUF_SIZE
&& CMD_APDU_HEAD_SIZE + len < c->icc_header.data_len)
/* more packet comes */
return 1;
2011-12-28 03:27:16 +00:00
2012-01-20 09:18:23 +00:00
if (CMD_APDU_HEAD_SIZE + len != c->icc_header.data_len)
goto error;
2010-08-18 03:57:45 +00:00
2012-01-20 09:18:23 +00:00
if (len == c->a->cmd_apdu_head[4])
/* No Le field*/
c->a->expected_res_size = 0;
else if (len == (size_t)c->a->cmd_apdu_head[4] + 1)
2010-12-07 05:34:25 +00:00
{
2012-01-20 09:18:23 +00:00
/* it has Le field*/
c->a->expected_res_size = epo->buf[-1];
len--;
}
else
{
error:
epo->err = 1;
return 0;
}
c->a->cmd_apdu_data_len += len;
return 0;
}
static void nomore_data (struct ep_out *epo, size_t len)
{
(void)len;
epo->err = 1;
epo->end_rx = end_nomore_data;
epo->buf = NULL;
epo->buf_len = 0;
epo->cnt = 0;
epo->next_buf = nomore_data;
}
#define INS_GET_RESPONSE 0xc0
static void icc_cmd_apdu_data (struct ep_out *epo, size_t len)
{
struct ccid *c = (struct ccid *)epo->priv;
2011-12-28 03:27:16 +00:00
2012-01-20 09:18:23 +00:00
(void)len;
if (c->state == APDU_STATE_RESULT_GET_RESPONSE
&& c->a->cmd_apdu_head[1] != INS_GET_RESPONSE)
{
2011-12-28 03:27:16 +00:00
/*
2012-01-20 09:18:23 +00:00
* Handling exceptional request.
*
* Host didn't finish receiving the whole response APDU by GET RESPONSE,
* but initiates another command.
2011-12-28 03:27:16 +00:00
*/
2012-01-20 09:18:23 +00:00
c->state = APDU_STATE_WAIT_COMMAND;
c->p = c->a->cmd_apdu_data;
c->len = MAX_CMD_APDU_DATA_SIZE;
2010-12-07 05:34:25 +00:00
}
2010-08-26 10:50:06 +00:00
2012-01-20 09:18:23 +00:00
epo->end_rx = end_cmd_apdu_data;
epo->buf = c->p;
epo->buf_len = c->len;
epo->cnt = 0;
epo->next_buf = nomore_data;
}
static void icc_abdata (struct ep_out *epo, size_t len)
{
struct ccid *c = (struct ccid *)epo->priv;
(void)len;
if (c->icc_header.msg_type == ICC_XFR_BLOCK)
{
c->a->seq = c->icc_header.seq;
epo->end_rx = end_cmd_apdu_head;
epo->buf = c->a->cmd_apdu_head;
epo->buf_len = 5;
epo->cnt = 0;
epo->next_buf = icc_cmd_apdu_data;
2010-12-07 05:34:25 +00:00
}
2012-01-20 09:18:23 +00:00
else
{
epo->end_rx = end_notify;
epo->buf = c->p;
epo->buf_len = c->len;
epo->cnt = 0;
epo->next_buf = nomore_data;
}
}
static void
icc_prepare_receive (struct ccid *c)
{
DEBUG_INFO ("Rx ready\r\n");
c->epo->err = 0;
c->epo->buf = (uint8_t *)&c->icc_header;
c->epo->buf_len = sizeof (struct icc_header);
c->epo->cnt = 0;
c->epo->next_buf = icc_abdata;
c->epo->end_rx = end_icc_rx;
usb_lld_rx_enable (c->epo->ep_num);
}
/*
* Rx ready callback
*/
void
EP2_OUT_Callback (void)
{
struct ep_out *epo = &endpoint_out;
size_t len = usb_lld_rx_data_len (epo->ep_num);
int offset = 0;
int cont;
size_t orig_len = len;
while (epo->err == 0)
if (len == 0)
break;
else if (len <= epo->buf_len)
{
usb_lld_rxcpy (epo->buf, epo->ep_num, offset, len);
epo->buf += len;
epo->cnt += len;
epo->buf_len -= len;
break;
}
else /* len > buf_len */
{
usb_lld_rxcpy (epo->buf, epo->ep_num, offset, epo->buf_len);
len -= epo->buf_len;
offset += epo->buf_len;
epo->next_buf (epo, len); /* Update epo->buf, cnt, buf_len */
}
/*
* ORIG_LEN to distingush ZLP and the end of transaction
* (ORIG_LEN != USB_LL_BUF_SIZE)
*/
cont = epo->end_rx (epo, orig_len);
if (cont)
usb_lld_rx_enable (epo->ep_num);
else
epo->notify (epo);
2010-08-23 05:40:33 +00:00
}
2010-08-18 05:21:58 +00:00
2010-09-09 16:25:44 +00:00
/*
* ATR (Answer To Reset) string
*
2010-12-12 23:51:48 +00:00
* TS = 0x3b: Direct conversion
2010-12-10 07:31:25 +00:00
* T0 = 0xda: TA1, TC1 and TD1 follow, 10 historical bytes
2010-09-09 16:25:44 +00:00
* TA1 = 0x11: FI=1, DI=1
2010-12-10 07:31:25 +00:00
* TC1 = 0xff
2010-09-09 16:25:44 +00:00
* TD1 = 0x81: TD2 follows, T=1
2010-12-10 07:31:25 +00:00
* TD2 = 0xb1: TA3, TB3 and TD3 follow, T=1
2010-09-09 16:25:44 +00:00
* TA3 = 0xFE: IFSC = 254 bytes
* TB3 = 0x55: BWI = 5, CWI = 5 (BWT timeout 3.2 sec)
2010-12-10 07:31:25 +00:00
* TD3 = 0x1f: TA4 follows, T=15
* TA4 = 0x03: 5V or 3.3V
* Historical bytes: to be explained...
2010-09-09 16:25:44 +00:00
* XOR check
*
2010-12-14 08:36:12 +00:00
* Minimum: 0x3b, 0x8a, 0x80, 0x01, + historical bytes, xor check
*
2010-09-09 16:25:44 +00:00
*/
2012-01-20 09:18:23 +00:00
static const uint8_t ATR[] = {
2010-12-10 07:31:25 +00:00
0x3b, 0xda, 0x11, 0xff, 0x81, 0xb1, 0xfe, 0x55, 0x1f, 0x03,
0x00,
0x31, 0x84, /* full DF name, GET DATA, MF */
0x73,
2012-01-20 09:18:23 +00:00
0x80, /* DF full name */
0x01, /* 1-byte */
0x80, /* Command chaining, No extended Lc and extended Le */
2010-12-10 07:31:25 +00:00
0x00,
0x90, 0x00,
(0xda^0x11^0xff^0x81^0xb1^0xfe^0x55^0x1f^0x03
2012-01-20 09:18:23 +00:00
^0x00^0x31^0x84^0x73^0x80^0x01^0x80^0x00^0x90^0x00)
2010-09-09 16:25:44 +00:00
};
2010-08-18 08:02:04 +00:00
2010-09-16 02:03:14 +00:00
/*
* Send back error
*/
2012-01-20 09:18:23 +00:00
static void icc_error (struct ccid *c, int offset)
2010-09-16 02:03:14 +00:00
{
2012-01-20 09:18:23 +00:00
uint8_t icc_reply[ICC_MSG_HEADER_SIZE];
2010-12-07 05:34:25 +00:00
icc_reply[0] = ICC_SLOT_STATUS_RET; /* Any value should be OK */
icc_reply[1] = 0x00;
icc_reply[2] = 0x00;
icc_reply[3] = 0x00;
icc_reply[4] = 0x00;
icc_reply[5] = 0x00; /* Slot */
2012-01-20 09:18:23 +00:00
icc_reply[ICC_MSG_SEQ_OFFSET] = c->icc_header.seq;
if (c->icc_state == ICC_STATE_START)
2010-09-16 02:03:14 +00:00
/* 1: ICC present but not activated 2: No ICC present */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
2010-09-16 02:03:14 +00:00
else
/* An ICC is present and active */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
icc_reply[ICC_MSG_STATUS_OFFSET] |= ICC_CMD_STATUS_ERROR; /* Failed */
icc_reply[ICC_MSG_ERROR_OFFSET] = offset;
icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
2012-01-20 09:18:23 +00:00
/* This is a single packet Bulk-IN transaction */
c->epi->buf = NULL;
c->epi->tx_done = 1;
usb_lld_write (c->epi->ep_num, icc_reply, ICC_MSG_HEADER_SIZE);
2010-09-16 02:03:14 +00:00
}
2010-12-08 05:10:30 +00:00
static WORKING_AREA(waGPGthread, 128*16);
extern msg_t GPGthread (void *arg);
2010-08-19 08:09:59 +00:00
/* Send back ATR (Answer To Reset) */
enum icc_state
2012-01-20 09:18:23 +00:00
icc_power_on (struct ccid *c)
2010-08-18 08:02:04 +00:00
{
2012-01-20 09:18:23 +00:00
size_t size_atr = sizeof (ATR);
uint8_t p[ICC_MSG_HEADER_SIZE];
if (c->application == NULL)
c->application = chThdCreateStatic (waGPGthread, sizeof(waGPGthread),
NORMALPRIO, GPGthread,
(void *)c->icc_thread);
p[0] = ICC_DATA_BLOCK_RET;
p[1] = size_atr;
p[2] = 0x00;
p[3] = 0x00;
p[4] = 0x00;
p[5] = 0x00; /* Slot */
p[ICC_MSG_SEQ_OFFSET] = c->icc_header.seq;
p[ICC_MSG_STATUS_OFFSET] = 0x00;
p[ICC_MSG_ERROR_OFFSET] = 0x00;
p[ICC_MSG_CHAIN_OFFSET] = 0x00;
usb_lld_txcpy (p, c->epi->ep_num, 0, ICC_MSG_HEADER_SIZE);
usb_lld_txcpy (ATR, c->epi->ep_num, ICC_MSG_HEADER_SIZE, size_atr);
/* This is a single packet Bulk-IN transaction */
c->epi->buf = NULL;
c->epi->tx_done = 1;
usb_lld_tx_enable (c->epi->ep_num, ICC_MSG_HEADER_SIZE + size_atr);
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("ON\r\n");
2010-08-23 05:40:33 +00:00
2010-08-19 08:09:59 +00:00
return ICC_STATE_WAIT;
2010-08-18 08:02:04 +00:00
}
2010-08-19 08:09:59 +00:00
static void
2012-01-20 09:18:23 +00:00
icc_send_status (struct ccid *c)
2010-08-18 08:02:04 +00:00
{
2012-01-20 09:18:23 +00:00
uint8_t icc_reply[ICC_MSG_HEADER_SIZE];
2010-12-07 05:34:25 +00:00
icc_reply[0] = ICC_SLOT_STATUS_RET;
icc_reply[1] = 0x00;
icc_reply[2] = 0x00;
icc_reply[3] = 0x00;
icc_reply[4] = 0x00;
icc_reply[5] = 0x00; /* Slot */
2012-01-20 09:18:23 +00:00
icc_reply[ICC_MSG_SEQ_OFFSET] = c->icc_header.seq;
if (c->icc_state == ICC_STATE_START)
2010-08-23 05:40:33 +00:00
/* 1: ICC present but not activated 2: No ICC present */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 1;
2010-08-19 08:09:59 +00:00
else
2010-08-23 05:40:33 +00:00
/* An ICC is present and active */
2010-12-07 05:34:25 +00:00
icc_reply[ICC_MSG_STATUS_OFFSET] = 0;
icc_reply[ICC_MSG_ERROR_OFFSET] = 0x00;
icc_reply[ICC_MSG_CHAIN_OFFSET] = 0x00;
2012-01-20 09:18:23 +00:00
/* This is a single packet Bulk-IN transaction */
c->epi->buf = NULL;
c->epi->tx_done = 1;
usb_lld_write (c->epi->ep_num, icc_reply, ICC_MSG_HEADER_SIZE);
2010-08-18 08:02:04 +00:00
2010-09-05 16:55:29 +00:00
#ifdef DEBUG_MORE
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("St\r\n");
2010-09-05 16:55:29 +00:00
#endif
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
2010-08-19 08:09:59 +00:00
enum icc_state
2012-01-20 09:18:23 +00:00
icc_power_off (struct ccid *c)
2010-08-19 08:09:59 +00:00
{
2012-01-20 09:18:23 +00:00
if (c->application)
2010-12-08 05:10:30 +00:00
{
2012-01-20 09:18:23 +00:00
chThdTerminate (c->application);
chEvtSignal (c->application, EV_NOP);
chThdWait (c->application);
c->application = NULL;
2010-12-08 05:10:30 +00:00
}
2012-01-20 09:18:23 +00:00
c->icc_state = ICC_STATE_START; /* This status change should be here */
icc_send_status (c);
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("OFF\r\n");
2010-08-19 08:09:59 +00:00
return ICC_STATE_START;
2010-08-18 08:02:04 +00:00
}
2010-08-23 05:40:33 +00:00
static void
2012-01-20 09:18:23 +00:00
icc_send_data_block_0x9000 (struct ccid *c)
{
uint8_t p[ICC_MSG_HEADER_SIZE+2];
size_t len = 2;
p[0] = ICC_DATA_BLOCK_RET;
p[1] = len & 0xFF;
p[2] = (len >> 8)& 0xFF;
p[3] = (len >> 16)& 0xFF;
p[4] = (len >> 24)& 0xFF;
p[5] = 0x00; /* Slot */
p[ICC_MSG_SEQ_OFFSET] = c->a->seq;
p[ICC_MSG_STATUS_OFFSET] = 0;
p[ICC_MSG_ERROR_OFFSET] = 0;
p[ICC_MSG_CHAIN_OFFSET] = 0;
p[ICC_MSG_CHAIN_OFFSET+1] = 0x90;
p[ICC_MSG_CHAIN_OFFSET+2] = 0x00;
usb_lld_txcpy (p, c->epi->ep_num, 0, ICC_MSG_HEADER_SIZE + len);
c->epi->buf = NULL;
c->epi->tx_done = 1;
usb_lld_tx_enable (c->epi->ep_num, ICC_MSG_HEADER_SIZE + len);
#ifdef DEBUG_MORE
DEBUG_INFO ("DATA\r\n");
#endif
}
static void
icc_send_data_block (struct ccid *c, uint8_t status)
2010-08-23 05:40:33 +00:00
{
2010-12-07 05:34:25 +00:00
int tx_size = USB_LL_BUF_SIZE;
2012-01-20 09:18:23 +00:00
uint8_t p[ICC_MSG_HEADER_SIZE];
size_t len;
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
if (status == 0)
len = c->a->res_apdu_data_len + 2;
2011-01-11 00:16:40 +00:00
else
2012-01-20 09:18:23 +00:00
len = 0;
2011-01-11 00:16:40 +00:00
p[0] = ICC_DATA_BLOCK_RET;
p[1] = len & 0xFF;
p[2] = (len >> 8)& 0xFF;
p[3] = (len >> 16)& 0xFF;
p[4] = (len >> 24)& 0xFF;
p[5] = 0x00; /* Slot */
2012-01-20 09:18:23 +00:00
p[ICC_MSG_SEQ_OFFSET] = c->a->seq;
2011-01-11 00:16:40 +00:00
p[ICC_MSG_STATUS_OFFSET] = status;
p[ICC_MSG_ERROR_OFFSET] = 0;
2012-01-20 09:18:23 +00:00
p[ICC_MSG_CHAIN_OFFSET] = 0;
usb_lld_txcpy (p, c->epi->ep_num, 0, ICC_MSG_HEADER_SIZE);
if (len == 0)
{
usb_lld_tx_enable (c->epi->ep_num, ICC_MSG_HEADER_SIZE);
return;
}
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
if (ICC_MSG_HEADER_SIZE + len <= USB_LL_BUF_SIZE)
2010-12-07 05:34:25 +00:00
{
2012-01-20 09:18:23 +00:00
usb_lld_txcpy (c->a->res_apdu_data, c->epi->ep_num,
ICC_MSG_HEADER_SIZE, c->a->res_apdu_data_len);
usb_lld_txcpy (c->sw1sw2, c->epi->ep_num,
ICC_MSG_HEADER_SIZE + c->a->res_apdu_data_len, 2);
c->epi->buf = NULL;
if (ICC_MSG_HEADER_SIZE + len < USB_LL_BUF_SIZE)
c->epi->tx_done = 1;
tx_size = ICC_MSG_HEADER_SIZE + len;
}
else if (ICC_MSG_HEADER_SIZE + len - 1 == USB_LL_BUF_SIZE)
{
usb_lld_txcpy (c->a->res_apdu_data, c->epi->ep_num,
ICC_MSG_HEADER_SIZE, c->a->res_apdu_data_len);
usb_lld_txcpy (c->sw1sw2, c->epi->ep_num,
ICC_MSG_HEADER_SIZE + c->a->res_apdu_data_len, 1);
c->epi->buf = &c->sw1sw2[1];
c->epi->cnt = 1;
c->epi->buf_len = 1;
c->epi->next_buf = no_buf;
}
else if (ICC_MSG_HEADER_SIZE + len - 2 == USB_LL_BUF_SIZE)
{
usb_lld_txcpy (c->a->res_apdu_data, c->epi->ep_num,
ICC_MSG_HEADER_SIZE, c->a->res_apdu_data_len);
c->epi->buf = &c->sw1sw2[0];
c->epi->cnt = 0;
c->epi->buf_len = 2;
c->epi->next_buf = no_buf;
2010-08-23 05:40:33 +00:00
}
else
2012-01-20 09:18:23 +00:00
{
usb_lld_txcpy (c->a->res_apdu_data, c->epi->ep_num, ICC_MSG_HEADER_SIZE,
USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE);
c->epi->buf = c->a->res_apdu_data + USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE;
c->epi->cnt = USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE;
c->epi->buf_len = c->a->res_apdu_data_len
- (USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE);
c->epi->next_buf = get_sw1sw2;
}
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
usb_lld_tx_enable (c->epi->ep_num, tx_size);
2010-09-09 16:25:44 +00:00
#ifdef DEBUG_MORE
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("DATA\r\n");
2010-09-09 16:25:44 +00:00
#endif
}
static void
2012-01-20 09:18:23 +00:00
icc_send_data_block_gr (struct ccid *c, size_t chunk_len)
2010-09-09 16:25:44 +00:00
{
2012-01-20 09:18:23 +00:00
int tx_size = USB_LL_BUF_SIZE;
uint8_t p[ICC_MSG_HEADER_SIZE];
size_t len = chunk_len + 2;
p[0] = ICC_DATA_BLOCK_RET;
p[1] = len & 0xFF;
p[2] = (len >> 8)& 0xFF;
p[3] = (len >> 16)& 0xFF;
p[4] = (len >> 24)& 0xFF;
p[5] = 0x00; /* Slot */
p[ICC_MSG_SEQ_OFFSET] = c->a->seq;
p[ICC_MSG_STATUS_OFFSET] = 0;
p[ICC_MSG_ERROR_OFFSET] = 0;
p[ICC_MSG_CHAIN_OFFSET] = 0;
usb_lld_txcpy (p, c->epi->ep_num, 0, ICC_MSG_HEADER_SIZE);
set_sw1sw2 (c->epi);
if (chunk_len <= USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE)
{
int size_for_sw;
if (chunk_len <= USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE - 2)
size_for_sw = 2;
else if (chunk_len == USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE - 1)
size_for_sw = 1;
else
size_for_sw = 0;
usb_lld_txcpy (c->p, c->epi->ep_num, ICC_MSG_HEADER_SIZE, chunk_len);
if (size_for_sw)
usb_lld_txcpy (c->sw1sw2, c->epi->ep_num,
ICC_MSG_HEADER_SIZE + chunk_len, size_for_sw);
tx_size = ICC_MSG_HEADER_SIZE + chunk_len + size_for_sw;
if (size_for_sw == 2)
{
c->epi->buf = NULL;
if (tx_size < USB_LL_BUF_SIZE)
c->epi->tx_done = 1;
/* Don't set epi->tx_done = 1, when it requires ZLP */
}
else
{
c->epi->buf = c->sw1sw2 + size_for_sw;
c->epi->cnt = size_for_sw;
c->epi->buf_len = 2 - size_for_sw;
c->epi->next_buf = no_buf;
}
}
else
{
usb_lld_txcpy (c->p, c->epi->ep_num, ICC_MSG_HEADER_SIZE,
USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE);
c->epi->buf = c->p + USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE;
c->epi->cnt = 0;
c->epi->buf_len = chunk_len - (USB_LL_BUF_SIZE - ICC_MSG_HEADER_SIZE);
c->epi->next_buf = get_sw1sw2;
}
c->p += chunk_len;
c->len -= chunk_len;
usb_lld_tx_enable (c->epi->ep_num, tx_size);
2010-09-05 16:55:29 +00:00
#ifdef DEBUG_MORE
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("DATA\r\n");
2010-09-05 16:55:29 +00:00
#endif
2010-08-23 05:40:33 +00:00
}
2012-01-20 09:18:23 +00:00
static void
icc_send_params (struct ccid *c)
{
uint8_t p[ICC_MSG_HEADER_SIZE];
const uint8_t params[] = {
0x11, /* bmFindexDindex */
0x11, /* bmTCCKST1 */
0xFE, /* bGuardTimeT1 */
0x55, /* bmWaitingIntegersT1 */
0x03, /* bClockStop */
0xFE, /* bIFSC */
0 /* bNadValue */
};
p[0] = ICC_PARAMS_RET;
p[1] = 0x07; /* Length = 0x00000007 */
p[2] = 0;
p[3] = 0;
p[4] = 0;
p[5] = 0x00; /* Slot */
p[ICC_MSG_SEQ_OFFSET] = c->icc_header.seq;
p[ICC_MSG_STATUS_OFFSET] = 0;
p[ICC_MSG_ERROR_OFFSET] = 0;
p[ICC_MSG_CHAIN_OFFSET] = 0x01; /* ProtocolNum: T=1 */
usb_lld_txcpy (p, c->epi->ep_num, 0, ICC_MSG_HEADER_SIZE);
usb_lld_txcpy (params, c->epi->ep_num, ICC_MSG_HEADER_SIZE, sizeof params);
/* This is a single packet Bulk-IN transaction */
c->epi->buf = NULL;
c->epi->tx_done = 1;
usb_lld_tx_enable (c->epi->ep_num, ICC_MSG_HEADER_SIZE + sizeof params);
#ifdef DEBUG_MORE
DEBUG_INFO ("PARAMS\r\n");
#endif
}
2011-01-04 12:06:55 +00:00
2010-08-19 08:09:59 +00:00
static enum icc_state
2012-01-20 09:18:23 +00:00
icc_handle_data (struct ccid *c)
2010-08-18 05:21:58 +00:00
{
2012-01-20 09:18:23 +00:00
enum icc_state next_state = c->icc_state;
2010-12-07 05:34:25 +00:00
2012-01-20 09:18:23 +00:00
if (c->err != 0)
{
c->err = 0;
c->state = APDU_STATE_WAIT_COMMAND;
c->p = c->a->cmd_apdu_data;
c->len = MAX_CMD_APDU_DATA_SIZE;
c->a->cmd_apdu_data_len = 0;
c->a->expected_res_size = 0;
icc_error (c, ICC_OFFSET_DATA_LEN);
return next_state;
}
2010-08-18 08:02:04 +00:00
2012-01-20 09:18:23 +00:00
switch (c->icc_state)
2010-08-19 08:09:59 +00:00
{
case ICC_STATE_START:
2012-01-20 09:18:23 +00:00
if (c->icc_header.msg_type == ICC_POWER_ON)
next_state = icc_power_on (c);
else if (c->icc_header.msg_type == ICC_POWER_OFF)
next_state = icc_power_off (c);
else if (c->icc_header.msg_type == ICC_SLOT_STATUS)
icc_send_status (c);
2010-08-19 08:09:59 +00:00
else
2010-09-16 02:03:14 +00:00
{
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR01\r\n");
2012-01-20 09:18:23 +00:00
icc_error (c, ICC_OFFSET_CMD_NOT_SUPPORTED);
2010-08-19 08:09:59 +00:00
}
break;
case ICC_STATE_WAIT:
2012-01-20 09:18:23 +00:00
if (c->icc_header.msg_type == ICC_POWER_ON)
2010-09-09 16:25:44 +00:00
/* Not in the spec., but pcscd/libccid */
2012-01-20 09:18:23 +00:00
next_state = icc_power_on (c);
else if (c->icc_header.msg_type == ICC_POWER_OFF)
next_state = icc_power_off (c);
else if (c->icc_header.msg_type == ICC_SLOT_STATUS)
icc_send_status (c);
else if (c->icc_header.msg_type == ICC_XFR_BLOCK)
2010-08-18 08:02:04 +00:00
{
2012-01-20 09:18:23 +00:00
if (c->icc_header.param == 0)
{
DEBUG_INFO ("DUMP C\r\n");
DEBUG_SHORT (c->icc_state);
DEBUG_BYTE (c->state);
DEBUG_WORD (c->p);
DEBUG_WORD (c->len);
DEBUG_BYTE (c->err);
DEBUG_WORD (c->epo->buf);
DEBUG_WORD (c->epo->cnt);
DEBUG_WORD (c->epo->buf_len);
DEBUG_WORD (c->a->cmd_apdu_data);
DEBUG_BINARY (c->a->cmd_apdu_data, c->a->cmd_apdu_data_len);
if ((c->a->cmd_apdu_head[0] & 0x10) == 0)
2012-01-18 09:59:16 +00:00
{
2012-01-20 09:18:23 +00:00
if (c->state == APDU_STATE_COMMAND_CHAINING)
{ /* command chaining finished */
c->p += c->a->cmd_apdu_head[4];
c->a->cmd_apdu_head[4] = 0;
c->a->cmd_apdu_data_len = c->p - c->a->cmd_apdu_data;
DEBUG_INFO ("CMD chaning finished.\r\n");
2012-01-18 09:59:16 +00:00
}
2012-01-20 09:18:23 +00:00
if (c->a->cmd_apdu_head[1] == INS_GET_RESPONSE
&& c->state == APDU_STATE_RESULT_GET_RESPONSE)
2012-01-18 09:59:16 +00:00
{
2012-01-20 09:18:23 +00:00
size_t len = c->a->expected_res_size;
if (c->len <= c->a->expected_res_size)
len = c->len;
icc_send_data_block_gr (c, len);
if (c->len == 0)
c->state = APDU_STATE_RESULT;
c->icc_state = ICC_STATE_WAIT;
2012-01-18 09:59:16 +00:00
}
else
2012-01-20 09:18:23 +00:00
{ /* Give this message to GPG thread */
c->state = APDU_STATE_COMMAND_RECEIVED;
c->a->sw = 0x9000;
c->a->res_apdu_data_len = 0;
c->a->res_apdu_data = &icc_buffer[5];
chEvtSignal (c->application, EV_CMD_AVAILABLE);
next_state = ICC_STATE_EXECUTE;
2012-01-18 09:59:16 +00:00
}
}
2012-01-20 09:18:23 +00:00
else /* command chaining is ongoing*/
2012-01-18 09:59:16 +00:00
{
2012-01-20 09:18:23 +00:00
c->state = APDU_STATE_COMMAND_CHAINING;
c->p += c->a->cmd_apdu_head[4];
c->len -= c->a->cmd_apdu_head[4];
icc_send_data_block_0x9000 (c);
DEBUG_INFO ("CMD chaning...\r\n");
2012-01-18 09:59:16 +00:00
}
2010-08-19 08:09:59 +00:00
}
else
2012-01-20 09:18:23 +00:00
{ /* ICC block chaining is not supported. */
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR02\r\n");
2012-01-20 09:18:23 +00:00
icc_error (c, ICC_OFFSET_PARAM);
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
}
2012-01-20 09:18:23 +00:00
else if (c->icc_header.msg_type == ICC_SET_PARAMS
|| c->icc_header.msg_type == ICC_GET_PARAMS)
icc_send_params (c);
else if (c->icc_header.msg_type == ICC_SECURE)
2011-01-04 12:06:55 +00:00
{
2011-01-19 05:36:04 +00:00
if (icc_buffer[10] == 0x00) /* PIN verification */
{
2012-01-20 09:18:23 +00:00
icc_buffer[0] = icc_buffer[25-10];
icc_buffer[1] = icc_buffer[26-10];
icc_buffer[2] = icc_buffer[27-10];
icc_buffer[3] = icc_buffer[28-10];
2012-01-18 09:59:16 +00:00
/**/
2012-01-20 09:18:23 +00:00
icc_buffer[5] = 0; /* bConfirmPIN */
icc_buffer[6] = icc_buffer[17-10]; /* bEntryValidationCondition */
icc_buffer[7] = icc_buffer[18-10]; /* bNumberMessage */
icc_buffer[8] = icc_buffer[19-10]; /* wLangId L */
icc_buffer[9] = icc_buffer[20-10]; /* wLangId H */
icc_buffer[10] = icc_buffer[21-10]; /* bMsgIndex */
c->a->cmd_apdu_data_len = 6;
c->a->expected_res_size = 0;
c->a->sw = 0x9000;
c->a->res_apdu_data_len = 0;
c->a->res_apdu_data = &icc_buffer[5];
chEvtSignal (c->application, EV_VERIFY_CMD_AVAILABLE);
2011-01-19 05:36:04 +00:00
next_state = ICC_STATE_EXECUTE;
}
else if (icc_buffer[10] == 0x01) /* PIN Modification */
{
2012-01-20 09:18:23 +00:00
uint8_t num_msgs = icc_buffer[21-10];
2011-01-19 05:36:04 +00:00
if (num_msgs == 0x00)
num_msgs = 1;
else if (num_msgs == 0xff)
num_msgs = 3;
2012-01-20 09:18:23 +00:00
icc_buffer[0] = icc_buffer[27 + num_msgs-10];
icc_buffer[1] = icc_buffer[28 + num_msgs-10];
icc_buffer[2] = icc_buffer[29 + num_msgs-10];
icc_buffer[3] = icc_buffer[30 + num_msgs-10];
2012-01-18 09:59:16 +00:00
/**/
2012-01-20 09:18:23 +00:00
icc_buffer[5] = icc_buffer[19-10]; /* bConfirmPIN */
icc_buffer[6] = icc_buffer[20-10]; /* bEntryValidationCondition */
icc_buffer[7] = icc_buffer[21-10]; /* bNumberMessage */
icc_buffer[8] = icc_buffer[22-10]; /* wLangId L */
icc_buffer[9] = icc_buffer[23-10]; /* wLangId H */
icc_buffer[10] = icc_buffer[24-10]; /* bMsgIndex, bMsgIndex1 */
2011-12-07 00:38:48 +00:00
if (num_msgs >= 2)
2012-01-20 09:18:23 +00:00
icc_buffer[11] = icc_buffer[25-10]; /* bMsgIndex2 */
2011-12-07 00:38:48 +00:00
if (num_msgs == 3)
2012-01-20 09:18:23 +00:00
icc_buffer[12] = icc_buffer[26-10]; /* bMsgIndex3 */
2012-01-18 09:59:16 +00:00
2012-01-20 09:18:23 +00:00
c->a->cmd_apdu_data_len = 5 + num_msgs;
c->a->expected_res_size = 0;
2012-01-18 09:59:16 +00:00
2012-01-20 09:18:23 +00:00
c->a->sw = 0x9000;
c->a->res_apdu_data_len = 0;
c->a->res_apdu_data = &icc_buffer[5];
2012-01-18 09:59:16 +00:00
2012-01-20 09:18:23 +00:00
chEvtSignal (c->application, EV_MODIFY_CMD_AVAILABLE);
2011-01-19 05:36:04 +00:00
next_state = ICC_STATE_EXECUTE;
}
else
2012-01-20 09:18:23 +00:00
icc_error (c, ICC_MSG_DATA_OFFSET);
2011-01-04 12:06:55 +00:00
}
2010-08-19 08:09:59 +00:00
else
2010-09-16 02:03:14 +00:00
{
2010-09-03 15:42:36 +00:00
DEBUG_INFO ("ERR03\r\n");
2012-01-20 09:18:23 +00:00
DEBUG_BYTE (c->icc_header.msg_type);
icc_error (c, ICC_OFFSET_CMD_NOT_SUPPORTED);
2010-08-19 08:09:59 +00:00
}
break;
2010-12-07 05:34:25 +00:00
case ICC_STATE_EXECUTE:
2012-01-20 09:18:23 +00:00
if (c->icc_header.msg_type == ICC_POWER_OFF)
next_state = icc_power_off (c);
else if (c->icc_header.msg_type == ICC_SLOT_STATUS)
icc_send_status (c);
2010-08-19 08:09:59 +00:00
else
2010-09-16 02:03:14 +00:00
{
2010-12-07 05:34:25 +00:00
DEBUG_INFO ("ERR04\r\n");
2012-01-20 09:18:23 +00:00
DEBUG_BYTE (c->icc_header.msg_type);
icc_error (c, ICC_OFFSET_CMD_NOT_SUPPORTED);
2011-01-11 00:16:40 +00:00
}
2011-01-15 12:49:17 +00:00
break;
2010-12-07 05:34:25 +00:00
default:
next_state = ICC_STATE_START;
DEBUG_INFO ("ERR10\r\n");
break;
2010-08-19 08:09:59 +00:00
}
2010-08-18 08:02:04 +00:00
2010-08-19 08:09:59 +00:00
return next_state;
}
static enum icc_state
2012-01-20 09:18:23 +00:00
icc_handle_timeout (struct ccid *c)
2010-08-19 08:09:59 +00:00
{
2012-01-20 09:18:23 +00:00
enum icc_state next_state = c->icc_state;
2010-08-19 08:09:59 +00:00
2012-01-20 09:18:23 +00:00
switch (c->icc_state)
2010-08-19 08:09:59 +00:00
{
2010-08-23 05:40:33 +00:00
case ICC_STATE_EXECUTE:
2012-01-20 09:18:23 +00:00
icc_send_data_block (c, ICC_CMD_STATUS_TIMEEXT);
2010-08-23 05:40:33 +00:00
break;
2010-12-07 05:34:25 +00:00
default:
2010-08-23 05:40:33 +00:00
break;
2010-08-19 08:09:59 +00:00
}
return next_state;
}
2010-09-08 05:24:12 +00:00
#define USB_ICC_TIMEOUT MS2ST(1950)
2010-08-19 08:09:59 +00:00
2012-01-20 09:18:23 +00:00
static struct ccid ccid;
2010-08-19 08:09:59 +00:00
msg_t
USBthread (void *arg)
{
2012-01-20 09:18:23 +00:00
struct ep_in *epi = &endpoint_in;
struct ep_out *epo = &endpoint_out;
struct ccid *c = &ccid;
struct apdu *a = &apdu;
2010-08-19 08:09:59 +00:00
(void)arg;
2012-01-20 09:18:23 +00:00
epi_init (epi, ENDP1, notify_tx, c);
epo_init (epo, ENDP2, notify_icc, c);
ccid_init (c, epi, epo, a, chThdSelf ());
apdu_init (a);
2010-08-19 08:09:59 +00:00
2012-01-20 09:18:23 +00:00
chEvtClear (ALL_EVENTS);
2010-08-19 08:09:59 +00:00
2012-01-20 09:18:23 +00:00
icc_prepare_receive (c);
2010-08-19 08:09:59 +00:00
while (1)
{
eventmask_t m;
m = chEvtWaitOneTimeout (ALL_EVENTS, USB_ICC_TIMEOUT);
if (m == EV_RX_DATA_READY)
2012-01-20 09:18:23 +00:00
c->icc_state = icc_handle_data (c);
2010-08-19 08:09:59 +00:00
else if (m == EV_EXEC_FINISHED)
2012-01-20 09:18:23 +00:00
if (c->icc_state == ICC_STATE_EXECUTE)
2011-01-28 08:38:52 +00:00
{
2012-01-20 09:18:23 +00:00
c->a->cmd_apdu_data_len = 0;
c->sw1sw2[0] = c->a->sw >> 8;
c->sw1sw2[1] = c->a->sw & 0xff;
2011-01-28 08:38:52 +00:00
2012-01-20 09:18:23 +00:00
if (c->a->res_apdu_data_len <= c->a->expected_res_size)
2011-01-28 08:38:52 +00:00
{
2012-01-20 09:18:23 +00:00
c->state = APDU_STATE_RESULT;
icc_send_data_block (c, 0);
c->icc_state = ICC_STATE_WAIT;
2011-01-28 08:38:52 +00:00
}
else
{
2012-01-20 09:18:23 +00:00
c->state = APDU_STATE_RESULT_GET_RESPONSE;
c->p = c->a->res_apdu_data;
c->len = c->a->res_apdu_data_len;
icc_send_data_block_gr (c, c->a->expected_res_size);
c->icc_state = ICC_STATE_WAIT;
2011-01-28 08:38:52 +00:00
}
}
else
2012-01-20 09:18:23 +00:00
{
2011-01-28 08:38:52 +00:00
DEBUG_INFO ("ERR07\r\n");
}
2010-12-07 05:34:25 +00:00
else if (m == EV_TX_FINISHED)
{
2012-01-20 09:18:23 +00:00
if (c->state == APDU_STATE_RESULT)
{
c->state = APDU_STATE_WAIT_COMMAND;
c->p = c->a->cmd_apdu_data;
c->len = MAX_CMD_APDU_DATA_SIZE;
c->err = 0;
c->a->cmd_apdu_data_len = 0;
c->a->expected_res_size = 0;
}
if (c->state == APDU_STATE_WAIT_COMMAND
|| c->state == APDU_STATE_COMMAND_CHAINING
|| c->state == APDU_STATE_RESULT_GET_RESPONSE)
icc_prepare_receive (c);
2010-12-07 05:34:25 +00:00
}
2010-08-19 08:09:59 +00:00
else /* Timeout */
2012-01-20 09:18:23 +00:00
c->icc_state = icc_handle_timeout (c);
2010-08-18 05:21:58 +00:00
}
return 0;
}