Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame^] | 1 | /* ISO7816-3 state machine for the card side |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 2 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame^] | 3 | * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 4 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame^] | 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 9 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame^] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 14 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame^] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 18 | */ |
Harald Welte | c430ac1 | 2017-02-28 01:25:12 +0100 | [diff] [blame] | 19 | #include <stdio.h> |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 20 | #include <assert.h> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <string.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include "utils.h" |
| 27 | #include "trace.h" |
| 28 | #include "iso7816_fidi.h" |
| 29 | #include "tc_etu.h" |
| 30 | #include "card_emu.h" |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 31 | #include "simtrace_prot.h" |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 32 | #include "usb_buf.h" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 33 | #include <osmocom/core/linuxlist.h> |
| 34 | #include <osmocom/core/msgb.h> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 35 | |
| 36 | |
| 37 | #define NUM_SLOTS 2 |
| 38 | |
| 39 | #define ISO7816_3_INIT_WTIME 9600 |
| 40 | #define ISO7816_3_DEFAULT_WI 10 |
| 41 | #define ISO7816_3_ATR_LEN_MAX (1+32) /* TS plus 32 chars */ |
| 42 | |
| 43 | #define ISO7816_3_PB_NULL 0x60 |
| 44 | |
| 45 | enum iso7816_3_card_state { |
| 46 | ISO_S_WAIT_POWER, /* waiting for power being applied */ |
| 47 | ISO_S_WAIT_CLK, /* waiting for clock being applied */ |
| 48 | ISO_S_WAIT_RST, /* waiting for reset being released */ |
| 49 | ISO_S_WAIT_ATR, /* waiting for start of ATR */ |
| 50 | ISO_S_IN_ATR, /* transmitting ATR to reader */ |
| 51 | ISO_S_IN_PTS, /* transmitting ATR to reader */ |
| 52 | ISO_S_WAIT_TPDU, /* waiting for data from reader */ |
| 53 | ISO_S_IN_TPDU, /* inside a TPDU */ |
| 54 | }; |
| 55 | |
| 56 | /* detailed sub-states of ISO_S_IN_PTS */ |
| 57 | enum pts_state { |
| 58 | PTS_S_WAIT_REQ_PTSS, |
| 59 | PTS_S_WAIT_REQ_PTS0, |
| 60 | PTS_S_WAIT_REQ_PTS1, |
| 61 | PTS_S_WAIT_REQ_PTS2, |
| 62 | PTS_S_WAIT_REQ_PTS3, |
| 63 | PTS_S_WAIT_REQ_PCK, |
| 64 | PTS_S_WAIT_RESP_PTSS = PTS_S_WAIT_REQ_PTSS | 0x10, |
| 65 | PTS_S_WAIT_RESP_PTS0 = PTS_S_WAIT_REQ_PTS0 | 0x10, |
| 66 | PTS_S_WAIT_RESP_PTS1 = PTS_S_WAIT_REQ_PTS1 | 0x10, |
| 67 | PTS_S_WAIT_RESP_PTS2 = PTS_S_WAIT_REQ_PTS2 | 0x10, |
| 68 | PTS_S_WAIT_RESP_PTS3 = PTS_S_WAIT_REQ_PTS3 | 0x10, |
| 69 | PTS_S_WAIT_RESP_PCK = PTS_S_WAIT_REQ_PCK | 0x10, |
| 70 | }; |
| 71 | |
| 72 | #define _PTSS 0 |
| 73 | #define _PTS0 1 |
| 74 | #define _PTS1 2 |
| 75 | #define _PTS2 3 |
| 76 | #define _PTS3 4 |
| 77 | #define _PCK 5 |
| 78 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 79 | /* T-PDU state machine states */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 80 | enum tpdu_state { |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 81 | TPDU_S_WAIT_CLA, /* waiting for CLA byte from reader */ |
| 82 | TPDU_S_WAIT_INS, /* waiting for INS byte from reader */ |
| 83 | TPDU_S_WAIT_P1, /* waiting for P1 byte from reader */ |
| 84 | TPDU_S_WAIT_P2, /* waiting for P2 byte from reader */ |
| 85 | TPDU_S_WAIT_P3, /* waiting for P3 byte from reader */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 86 | TPDU_S_WAIT_PB, /* waiting for Tx of procedure byte */ |
| 87 | TPDU_S_WAIT_RX, /* waiitng for more data from reader */ |
| 88 | TPDU_S_WAIT_TX, /* waiting for more data to reader */ |
| 89 | }; |
| 90 | |
| 91 | #define _CLA 0 |
| 92 | #define _INS 1 |
| 93 | #define _P1 2 |
| 94 | #define _P2 3 |
| 95 | #define _P3 4 |
| 96 | |
| 97 | struct card_handle { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 98 | uint32_t num; |
| 99 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 100 | enum iso7816_3_card_state state; |
| 101 | |
| 102 | /* signal levels */ |
| 103 | uint8_t vcc_active; /* 1 = on, 0 = off */ |
| 104 | uint8_t in_reset; /* 1 = RST low, 0 = RST high */ |
| 105 | uint8_t clocked; /* 1 = active, 0 = inactive */ |
| 106 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 107 | /* timing parameters, from PTS */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 108 | uint8_t fi; |
| 109 | uint8_t di; |
| 110 | uint8_t wi; |
| 111 | |
| 112 | uint8_t tc_chan; /* TC channel number */ |
| 113 | uint8_t uart_chan; /* UART channel */ |
| 114 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 115 | uint8_t in_ep; /* USB IN EP */ |
| 116 | uint8_t irq_ep; /* USB IN EP */ |
| 117 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 118 | uint32_t waiting_time; /* in clocks */ |
| 119 | |
| 120 | /* ATR state machine */ |
| 121 | struct { |
| 122 | uint8_t idx; |
| 123 | uint8_t len; |
| 124 | //uint8_t hist_len; |
| 125 | //uint8_t last_td; |
| 126 | uint8_t atr[ISO7816_3_ATR_LEN_MAX]; |
| 127 | } atr; |
| 128 | |
| 129 | /* PPS / PTS support */ |
| 130 | struct { |
| 131 | enum pts_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 132 | uint8_t req[6]; /* request bytes */ |
| 133 | uint8_t resp[6]; /* response bytes */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 134 | } pts; |
| 135 | |
| 136 | /* TPDU */ |
| 137 | struct { |
| 138 | enum tpdu_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 139 | uint8_t hdr[5]; /* CLA INS P1 P2 P3 */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 140 | } tpdu; |
| 141 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 142 | struct msgb *uart_rx_msg; /* UART RX -> USB TX */ |
| 143 | struct msgb *uart_tx_msg; /* USB RX -> UART TX */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 144 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 145 | struct llist_head uart_tx_queue; |
| 146 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 147 | struct { |
| 148 | uint32_t tx_bytes; |
| 149 | uint32_t rx_bytes; |
| 150 | uint32_t pps; |
| 151 | } stats; |
| 152 | }; |
| 153 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 154 | struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch) |
| 155 | { |
| 156 | return &ch->uart_tx_queue; |
| 157 | } |
| 158 | |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 159 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts); |
Harald Welte | e7194ab | 2015-11-14 21:03:25 +0100 | [diff] [blame] | 160 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 161 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 162 | /* update simtrace header msg_len and submit USB buffer */ |
| 163 | void usb_buf_upd_len_and_submit(struct msgb *msg) |
| 164 | { |
Harald Welte | b91f6ad | 2017-05-10 22:51:30 +0200 | [diff] [blame] | 165 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 166 | |
| 167 | sh->msg_len = msgb_length(msg); |
| 168 | |
| 169 | usb_buf_submit(msg); |
| 170 | } |
| 171 | |
| 172 | /* Allocate USB buffer and push + initialize simtrace_msg_hdr */ |
| 173 | struct msgb *usb_buf_alloc_st(uint8_t ep, uint8_t msg_class, uint8_t msg_type) |
| 174 | { |
| 175 | struct msgb *msg; |
| 176 | struct simtrace_msg_hdr *sh; |
| 177 | |
| 178 | msg = usb_buf_alloc(ep); |
| 179 | if (!msg) |
| 180 | return NULL; |
| 181 | |
| 182 | msg->l1h = msgb_put(msg, sizeof(*sh)); |
| 183 | sh = (struct simtrace_msg_hdr *) msg->l1h; |
| 184 | memset(sh, 0, sizeof(*sh)); |
| 185 | sh->msg_class = msg_class; |
| 186 | sh->msg_type = msg_type; |
| 187 | msg->l2h = msg->l1h + sizeof(*sh); |
| 188 | |
| 189 | return msg; |
| 190 | } |
| 191 | |
| 192 | /* Update cardemu_usb_msg_rx_data length + submit bufffer */ |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 193 | static void flush_rx_buffer(struct card_handle *ch) |
| 194 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 195 | struct msgb *msg; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 196 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 197 | uint32_t data_len; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 198 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 199 | msg = ch->uart_rx_msg; |
| 200 | if (!msg) |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 201 | return; |
| 202 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 203 | ch->uart_rx_msg = NULL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 204 | |
| 205 | /* store length of data payload fild in header */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 206 | rd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
| 207 | rd->data_len = msgb_l2len(msg) - sizeof(*rd); |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 208 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 209 | usb_buf_upd_len_and_submit(msg); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 212 | /* convert a non-contiguous PTS request/responsei into a contiguous |
| 213 | * buffer, returning the number of bytes used in the buffer */ |
| 214 | static int serialize_pts(uint8_t *out, const uint8_t *in) |
| 215 | { |
| 216 | int i = 0; |
| 217 | |
| 218 | out[i++] = in[_PTSS]; |
| 219 | out[i++] = in[_PTS0]; |
| 220 | if (in[_PTS0] & (1 << 4)) |
| 221 | out[i++] = in[_PTS1]; |
| 222 | if (in[_PTS0] & (1 << 5)) |
| 223 | out[i++] = in[_PTS2]; |
| 224 | if (in[_PTS0] & (1 << 6)) |
| 225 | out[i++] = in[_PTS3]; |
| 226 | out[i++] = in[_PCK]; |
| 227 | |
| 228 | return i; |
| 229 | } |
| 230 | |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 231 | static uint8_t csum_pts(const uint8_t *in) |
| 232 | { |
| 233 | uint8_t out[6]; |
| 234 | int len = serialize_pts(out, in); |
| 235 | uint8_t csum = 0; |
| 236 | int i; |
| 237 | |
| 238 | /* we don't include the PCK byte in the checksumming process */ |
| 239 | len -= 1; |
| 240 | |
| 241 | for (i = 0; i < len; i++) |
| 242 | csum = csum ^ out[i]; |
| 243 | |
| 244 | return csum; |
| 245 | } |
| 246 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 247 | static void flush_pts(struct card_handle *ch) |
| 248 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 249 | struct msgb *msg; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 250 | struct cardemu_usb_msg_pts_info *ptsi; |
| 251 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 252 | msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DO_CEMU_PTS); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 253 | if (!msg) |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 254 | return; |
| 255 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 256 | ptsi = (struct cardemu_usb_msg_pts_info *) msgb_put(msg, sizeof(*ptsi)); |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 257 | ptsi->pts_len = serialize_pts(ptsi->req, ch->pts.req); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 258 | serialize_pts(ptsi->resp, ch->pts.resp); |
| 259 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 260 | usb_buf_upd_len_and_submit(msg); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 263 | static void emu_update_fidi(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 264 | { |
| 265 | int rc; |
| 266 | |
| 267 | rc = compute_fidi_ratio(ch->fi, ch->di); |
| 268 | if (rc > 0 && rc < 0x400) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 269 | TRACE_INFO("%u: computed Fi(%u) Di(%u) ratio: %d\r\n", |
| 270 | ch->num, ch->fi, ch->di, rc); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 271 | /* make sure UART uses new F/D ratio */ |
| 272 | card_emu_uart_update_fidi(ch->uart_chan, rc); |
| 273 | /* notify ETU timer about this */ |
| 274 | tc_etu_set_etu(ch->tc_chan, rc); |
| 275 | } else |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 276 | TRACE_INFO("%u: computed FiDi ration %d unsupported\r\n", |
| 277 | ch->num, rc); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* Update the ISO 7816-3 TPDU receiver state */ |
| 281 | static void card_set_state(struct card_handle *ch, |
| 282 | enum iso7816_3_card_state new_state) |
| 283 | { |
Harald Welte | 903d63a | 2016-03-20 13:38:39 +0100 | [diff] [blame] | 284 | if (ch->state == new_state) |
| 285 | return; |
| 286 | |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 287 | TRACE_DEBUG("%u: 7816 card state %u -> %u\r\n", ch->num, |
| 288 | ch->state, new_state); |
Harald Welte | 903d63a | 2016-03-20 13:38:39 +0100 | [diff] [blame] | 289 | ch->state = new_state; |
| 290 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 291 | switch (new_state) { |
| 292 | case ISO_S_WAIT_POWER: |
| 293 | case ISO_S_WAIT_CLK: |
| 294 | case ISO_S_WAIT_RST: |
| 295 | /* disable Rx and Tx of UART */ |
| 296 | card_emu_uart_enable(ch->uart_chan, 0); |
| 297 | break; |
| 298 | case ISO_S_WAIT_ATR: |
Harald Welte | e7194ab | 2015-11-14 21:03:25 +0100 | [diff] [blame] | 299 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 300 | /* Reset to initial Fi / Di ratio */ |
| 301 | ch->fi = 1; |
| 302 | ch->di = 1; |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 303 | emu_update_fidi(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 304 | /* initialize todefault WI, this will be overwritten if we |
| 305 | * receive TC2, and it will be programmed into hardware after |
| 306 | * ATR is finished */ |
| 307 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 308 | /* update waiting time to initial waiting time */ |
| 309 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 310 | tc_etu_set_wtime(ch->tc_chan, ch->waiting_time); |
| 311 | /* Set ATR sub-state to initial state */ |
| 312 | ch->atr.idx = 0; |
| 313 | //set_atr_state(ch, ATR_S_WAIT_TS); |
| 314 | /* Notice that we are just coming out of reset */ |
| 315 | //ch->sh.flags |= SIMTRACE_FLAG_ATR; |
| 316 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 317 | break; |
| 318 | break; |
| 319 | case ISO_S_WAIT_TPDU: |
| 320 | /* enable the receiver, disable transmitter */ |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 321 | set_tpdu_state(ch, TPDU_S_WAIT_CLA); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 322 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 323 | break; |
| 324 | case ISO_S_IN_ATR: |
| 325 | case ISO_S_IN_PTS: |
| 326 | case ISO_S_IN_TPDU: |
| 327 | /* do nothing */ |
| 328 | break; |
| 329 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | /********************************************************************** |
| 334 | * PTS / PPS handling |
| 335 | **********************************************************************/ |
| 336 | |
| 337 | /* Update the ATR sub-state */ |
| 338 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss) |
| 339 | { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 340 | TRACE_DEBUG("%u: 7816 PTS state %u -> %u\r\n", |
| 341 | ch->num, ch->pts.state, new_ptss); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 342 | ch->pts.state = new_ptss; |
| 343 | } |
| 344 | |
| 345 | /* Determine the next PTS state */ |
| 346 | static enum pts_state next_pts_state(struct card_handle *ch) |
| 347 | { |
| 348 | uint8_t is_resp = ch->pts.state & 0x10; |
| 349 | uint8_t sstate = ch->pts.state & 0x0f; |
| 350 | uint8_t *pts_ptr; |
| 351 | |
| 352 | if (!is_resp) |
| 353 | pts_ptr = ch->pts.req; |
| 354 | else |
| 355 | pts_ptr = ch->pts.resp; |
| 356 | |
| 357 | switch (sstate) { |
| 358 | case PTS_S_WAIT_REQ_PTSS: |
| 359 | goto from_ptss; |
| 360 | case PTS_S_WAIT_REQ_PTS0: |
| 361 | goto from_pts0; |
| 362 | case PTS_S_WAIT_REQ_PTS1: |
| 363 | goto from_pts1; |
| 364 | case PTS_S_WAIT_REQ_PTS2: |
| 365 | goto from_pts2; |
| 366 | case PTS_S_WAIT_REQ_PTS3: |
| 367 | goto from_pts3; |
| 368 | } |
| 369 | |
| 370 | if (ch->pts.state == PTS_S_WAIT_REQ_PCK) |
| 371 | return PTS_S_WAIT_RESP_PTSS; |
| 372 | |
| 373 | from_ptss: |
| 374 | return PTS_S_WAIT_REQ_PTS0 | is_resp; |
| 375 | from_pts0: |
| 376 | if (pts_ptr[_PTS0] & (1 << 4)) |
| 377 | return PTS_S_WAIT_REQ_PTS1 | is_resp; |
| 378 | from_pts1: |
| 379 | if (pts_ptr[_PTS0] & (1 << 5)) |
| 380 | return PTS_S_WAIT_REQ_PTS2 | is_resp; |
| 381 | from_pts2: |
| 382 | if (pts_ptr[_PTS0] & (1 << 6)) |
| 383 | return PTS_S_WAIT_REQ_PTS3 | is_resp; |
| 384 | from_pts3: |
| 385 | return PTS_S_WAIT_REQ_PCK | is_resp; |
| 386 | } |
| 387 | |
| 388 | |
Harald Welte | ccb8a22 | 2016-03-20 13:37:11 +0100 | [diff] [blame] | 389 | static int |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 390 | process_byte_pts(struct card_handle *ch, uint8_t byte) |
| 391 | { |
| 392 | switch (ch->pts.state) { |
| 393 | case PTS_S_WAIT_REQ_PTSS: |
| 394 | ch->pts.req[_PTSS] = byte; |
| 395 | break; |
| 396 | case PTS_S_WAIT_REQ_PTS0: |
| 397 | ch->pts.req[_PTS0] = byte; |
| 398 | break; |
| 399 | case PTS_S_WAIT_REQ_PTS1: |
| 400 | ch->pts.req[_PTS1] = byte; |
| 401 | break; |
| 402 | case PTS_S_WAIT_REQ_PTS2: |
| 403 | ch->pts.req[_PTS2] = byte; |
| 404 | break; |
| 405 | case PTS_S_WAIT_REQ_PTS3: |
| 406 | ch->pts.req[_PTS3] = byte; |
| 407 | break; |
| 408 | case PTS_S_WAIT_REQ_PCK: |
| 409 | ch->pts.req[_PCK] = byte; |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 410 | if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 411 | TRACE_ERROR("%u: Error in PTS Checksum!\r\n", |
| 412 | ch->num); |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 413 | /* Wait for the next TPDU */ |
| 414 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 415 | return ISO_S_WAIT_TPDU; |
| 416 | } |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 417 | /* FIXME: check if proposal matches capabilities in ATR */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 418 | memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp)); |
| 419 | break; |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 420 | default: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 421 | TRACE_ERROR("%u: process_byte_pts() in invalid state %u\r\n", |
| 422 | ch->num, ch->pts.state); |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 423 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 424 | } |
| 425 | /* calculate the next state and set it */ |
| 426 | set_pts_state(ch, next_pts_state(ch)); |
| 427 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 428 | if (ch->pts.state == PTS_S_WAIT_RESP_PTSS) { |
| 429 | flush_pts(ch); |
| 430 | /* activate UART TX to transmit PTS response */ |
| 431 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 432 | /* don't fall-through to the 'return ISO_S_IN_PTS' |
| 433 | * below, rather keep ISO7816 state as-is, it will be |
| 434 | * further updated by the tx-completion handler */ |
Harald Welte | ccb8a22 | 2016-03-20 13:37:11 +0100 | [diff] [blame] | 435 | return -1; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 438 | return ISO_S_IN_PTS; |
| 439 | } |
| 440 | |
| 441 | /* return a single byte to be transmitted to the reader */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 442 | static int tx_byte_pts(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 443 | { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 444 | uint8_t byte; |
| 445 | |
| 446 | /* 1: Determine the next transmit byte */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 447 | switch (ch->pts.state) { |
| 448 | case PTS_S_WAIT_RESP_PTSS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 449 | byte = ch->pts.resp[_PTSS]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 450 | break; |
| 451 | case PTS_S_WAIT_RESP_PTS0: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 452 | byte = ch->pts.resp[_PTS0]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 453 | break; |
| 454 | case PTS_S_WAIT_RESP_PTS1: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 455 | byte = ch->pts.resp[_PTS1]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 456 | /* This must be TA1 */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 457 | ch->fi = byte >> 4; |
| 458 | ch->di = byte & 0xf; |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 459 | TRACE_DEBUG("%u: found Fi=%u Di=%u\r\n", ch->num, |
| 460 | ch->fi, ch->di); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 461 | break; |
| 462 | case PTS_S_WAIT_RESP_PTS2: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 463 | byte = ch->pts.resp[_PTS2]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 464 | break; |
| 465 | case PTS_S_WAIT_RESP_PTS3: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 466 | byte = ch->pts.resp[_PTS3]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 467 | break; |
| 468 | case PTS_S_WAIT_RESP_PCK: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 469 | byte = ch->pts.resp[_PCK]; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 470 | break; |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 471 | default: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 472 | TRACE_ERROR("%u: get_byte_pts() in invalid state %u\r\n", |
| 473 | ch->num, ch->pts.state); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* 2: Transmit the byte */ |
| 478 | card_emu_uart_tx(ch->uart_chan, byte); |
| 479 | |
| 480 | /* 3: Update the state */ |
| 481 | |
| 482 | switch (ch->pts.state) { |
| 483 | case PTS_S_WAIT_RESP_PCK: |
Harald Welte | c58bba0 | 2016-03-20 14:57:53 +0100 | [diff] [blame] | 484 | card_emu_uart_wait_tx_idle(ch->uart_chan); |
Harald Welte | 52d5546 | 2016-03-20 13:38:05 +0100 | [diff] [blame] | 485 | /* update baud rate generator with Fi/Di */ |
| 486 | emu_update_fidi(ch); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 487 | /* Wait for the next TPDU */ |
| 488 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 489 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 490 | break; |
| 491 | default: |
| 492 | /* calculate the next state and set it */ |
| 493 | set_pts_state(ch, next_pts_state(ch)); |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 494 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 495 | } |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 496 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 497 | /* return number of bytes transmitted */ |
| 498 | return 1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | /********************************************************************** |
| 503 | * TPDU handling |
| 504 | **********************************************************************/ |
| 505 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 506 | |
| 507 | /* compute number of data bytes according to Chapter 10.3.2 of 7816-3 */ |
| 508 | static unsigned int t0_num_data_bytes(uint8_t p3, int reader_to_card) |
| 509 | { |
| 510 | if (reader_to_card) { |
| 511 | return p3; |
| 512 | } else { |
| 513 | if (p3 == 0) |
| 514 | return 256; |
| 515 | else |
| 516 | return p3; |
| 517 | } |
| 518 | } |
| 519 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 520 | /* add a just-received TPDU byte (from reader) to USB buffer */ |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 521 | static void add_tpdu_byte(struct card_handle *ch, uint8_t byte) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 522 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 523 | struct msgb *msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 524 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 525 | unsigned int num_data_bytes = t0_num_data_bytes(ch->tpdu.hdr[_P3], 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 526 | |
| 527 | /* ensure we have a buffer */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 528 | if (!ch->uart_rx_msg) { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 529 | msg = ch->uart_rx_msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, |
| 530 | SIMTRACE_MSGT_DO_CEMU_RX_DATA); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 531 | if (!ch->uart_rx_msg) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 532 | TRACE_ERROR("%u: Received UART byte but ENOMEM\r\n", |
| 533 | ch->num); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 534 | return; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 535 | } |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 536 | msgb_put(msg, sizeof(*rd)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 537 | } else |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 538 | msg = ch->uart_rx_msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 539 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 540 | rd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 541 | msgb_put_u8(msg, byte); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 542 | |
| 543 | /* check if the buffer is full. If so, send it */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 544 | if (msgb_l2len(msg) >= sizeof(*rd) + num_data_bytes) { |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 545 | rd->flags |= CEMU_DATA_F_FINAL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 546 | flush_rx_buffer(ch); |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 547 | /* We need to transmit the SW now, */ |
| 548 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 549 | } else if (msgb_tailroom(msg) <= 0) |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 550 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts) |
| 554 | { |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 555 | if (ch->tpdu.state == new_ts) |
| 556 | return; |
| 557 | |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 558 | TRACE_DEBUG("%u: 7816 TPDU state %u -> %u\r\n", ch->num, |
| 559 | ch->tpdu.state, new_ts); |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 560 | |
Harald Welte | 903d63a | 2016-03-20 13:38:39 +0100 | [diff] [blame] | 561 | ch->tpdu.state = new_ts; |
| 562 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 563 | switch (new_ts) { |
| 564 | case TPDU_S_WAIT_CLA: |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 565 | case TPDU_S_WAIT_RX: |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 566 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 567 | break; |
| 568 | case TPDU_S_WAIT_PB: |
| 569 | /* we just completed the TPDU header from reader to card |
| 570 | * and now need to disable the receiver, enable the |
| 571 | * transmitter and transmit the procedure byte */ |
| 572 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 573 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 574 | default: |
| 575 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 576 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static enum tpdu_state next_tpdu_state(struct card_handle *ch) |
| 580 | { |
| 581 | switch (ch->tpdu.state) { |
| 582 | case TPDU_S_WAIT_CLA: |
| 583 | return TPDU_S_WAIT_INS; |
| 584 | case TPDU_S_WAIT_INS: |
| 585 | return TPDU_S_WAIT_P1; |
| 586 | case TPDU_S_WAIT_P1: |
| 587 | return TPDU_S_WAIT_P2; |
| 588 | case TPDU_S_WAIT_P2: |
| 589 | return TPDU_S_WAIT_P3; |
| 590 | case TPDU_S_WAIT_P3: |
| 591 | return TPDU_S_WAIT_PB; |
| 592 | /* simply stay in Rx or Tx by default */ |
| 593 | case TPDU_S_WAIT_PB: |
| 594 | return TPDU_S_WAIT_PB; |
| 595 | case TPDU_S_WAIT_RX: |
| 596 | return TPDU_S_WAIT_RX; |
| 597 | case TPDU_S_WAIT_TX: |
| 598 | return TPDU_S_WAIT_TX; |
| 599 | } |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 600 | /* we should never reach here */ |
| 601 | assert(0); |
| 602 | return -1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void send_tpdu_header(struct card_handle *ch) |
| 606 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 607 | struct msgb *msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 608 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 609 | uint8_t *cur; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 610 | |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 611 | TRACE_INFO("%u: %s: %02x %02x %02x %02x %02x\r\n", |
| 612 | ch->num, __func__, |
Harald Welte | 43f7949 | 2016-02-29 10:06:54 +0100 | [diff] [blame] | 613 | ch->tpdu.hdr[0], ch->tpdu.hdr[1], |
| 614 | ch->tpdu.hdr[2], ch->tpdu.hdr[3], |
| 615 | ch->tpdu.hdr[4]); |
| 616 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 617 | /* if we already/still have a context, send it off */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 618 | if (ch->uart_rx_msg) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 619 | TRACE_DEBUG("%u: have old buffer\r\n", ch->num); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 620 | if (msgb_l2len(ch->uart_rx_msg)) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 621 | TRACE_DEBUG("%u: flushing old buffer\r\n", ch->num); |
Harald Welte | f1697e2 | 2016-03-02 10:28:54 +0100 | [diff] [blame] | 622 | flush_rx_buffer(ch); |
| 623 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 624 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 625 | TRACE_DEBUG("%u: allocating new buffer\r\n", ch->num); |
| 626 | /* ensure we have a new buffer */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 627 | ch->uart_rx_msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, |
| 628 | SIMTRACE_MSGT_DO_CEMU_RX_DATA); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 629 | if (!ch->uart_rx_msg) { |
| 630 | TRACE_ERROR("%u: %s: ENOMEM\r\n", ch->num, __func__); |
| 631 | return; |
| 632 | } |
| 633 | msg = ch->uart_rx_msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 634 | rd = (struct cardemu_usb_msg_rx_data *) msgb_put(msg, sizeof(*rd)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 635 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 636 | /* initialize header */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 637 | rd->flags = CEMU_DATA_F_TPDU_HDR; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 638 | |
| 639 | /* copy TPDU header to data field */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 640 | cur = msgb_put(msg, sizeof(ch->tpdu.hdr)); |
| 641 | memcpy(cur, ch->tpdu.hdr, sizeof(ch->tpdu.hdr)); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 642 | /* rd->data_len is set in flush_rx_buffer() */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 643 | |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 644 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static enum iso7816_3_card_state |
| 648 | process_byte_tpdu(struct card_handle *ch, uint8_t byte) |
| 649 | { |
| 650 | switch (ch->tpdu.state) { |
| 651 | case TPDU_S_WAIT_CLA: |
| 652 | ch->tpdu.hdr[_CLA] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 653 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 654 | break; |
| 655 | case TPDU_S_WAIT_INS: |
| 656 | ch->tpdu.hdr[_INS] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 657 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 658 | break; |
| 659 | case TPDU_S_WAIT_P1: |
| 660 | ch->tpdu.hdr[_P1] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 661 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 662 | break; |
| 663 | case TPDU_S_WAIT_P2: |
| 664 | ch->tpdu.hdr[_P2] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 665 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 666 | break; |
| 667 | case TPDU_S_WAIT_P3: |
| 668 | ch->tpdu.hdr[_P3] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 669 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 670 | /* FIXME: start timer to transmit further 0x60 */ |
| 671 | /* send the TPDU header as part of a procedure byte |
| 672 | * request to the USB host */ |
| 673 | send_tpdu_header(ch); |
| 674 | break; |
| 675 | case TPDU_S_WAIT_RX: |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 676 | add_tpdu_byte(ch, byte); |
| 677 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 678 | default: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 679 | TRACE_ERROR("%u: process_byte_tpdu() in invalid state %u\r\n", |
| 680 | ch->num, ch->tpdu.state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 681 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 682 | |
| 683 | /* ensure we stay in TPDU ISO state */ |
| 684 | return ISO_S_IN_TPDU; |
| 685 | } |
| 686 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 687 | /* tx a single byte to be transmitted to the reader */ |
| 688 | static int tx_byte_tpdu(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 689 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 690 | struct msgb *msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 691 | struct cardemu_usb_msg_tx_data *td; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 692 | uint8_t byte; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 693 | |
| 694 | /* ensure we are aware of any data that might be pending for |
| 695 | * transmit */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 696 | if (!ch->uart_tx_msg) { |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 697 | /* uart_tx_queue is filled from main loop, so no need |
| 698 | * for irq-safe operations */ |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 699 | if (llist_empty(&ch->uart_tx_queue)) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 700 | return 0; |
| 701 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 702 | /* dequeue first at head */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 703 | ch->uart_tx_msg = msgb_dequeue(&ch->uart_tx_queue); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 704 | ch->uart_tx_msg->l1h = ch->uart_tx_msg->head; |
| 705 | ch->uart_tx_msg->l2h = ch->uart_tx_msg->l1h + sizeof(struct simtrace_msg_hdr); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 706 | msg = ch->uart_tx_msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 707 | /* remove the header */ |
| 708 | msgb_pull(msg, sizeof(struct simtrace_msg_hdr) + sizeof(*td)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 709 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 710 | msg = ch->uart_tx_msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 711 | td = (struct cardemu_usb_msg_tx_data *) msg->l2h; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 712 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 713 | /* take the next pending byte out of the msgb */ |
| 714 | byte = msgb_pull_u8(msg); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 715 | |
| 716 | card_emu_uart_tx(ch->uart_chan, byte); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 717 | |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 718 | /* this must happen _after_ the byte has been transmittd */ |
| 719 | switch (ch->tpdu.state) { |
| 720 | case TPDU_S_WAIT_PB: |
| 721 | /* if we just transmitted the procedure byte, we need to decide |
| 722 | * if we want to continue to receive or transmit */ |
| 723 | if (td->flags & CEMU_DATA_F_PB_AND_TX) |
| 724 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
| 725 | else if (td->flags & CEMU_DATA_F_PB_AND_RX) |
| 726 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
| 727 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 728 | default: |
| 729 | break; |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 732 | /* check if the buffer has now been fully transmitted */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 733 | if (msgb_length(msg) == 0) { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 734 | if (td->flags & CEMU_DATA_F_PB_AND_RX) { |
| 735 | /* we have just sent the procedure byte and now |
| 736 | * need to continue receiving */ |
| 737 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 738 | } else { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 739 | /* we have transmitted all bytes */ |
| 740 | if (td->flags & CEMU_DATA_F_FINAL) { |
| 741 | /* this was the final part of the APDU, go |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 742 | * back to state one */ |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 743 | card_set_state(ch, ISO_S_WAIT_TPDU); |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 744 | } |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 745 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 746 | usb_buf_free(msg); |
| 747 | ch->uart_tx_msg = NULL; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | /********************************************************************** |
| 754 | * Public API |
| 755 | **********************************************************************/ |
| 756 | |
| 757 | /* process a single byte received from the reader */ |
| 758 | void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte) |
| 759 | { |
| 760 | int new_state = -1; |
| 761 | |
| 762 | ch->stats.rx_bytes++; |
| 763 | |
| 764 | switch (ch->state) { |
| 765 | case ISO_S_WAIT_POWER: |
| 766 | case ISO_S_WAIT_CLK: |
| 767 | case ISO_S_WAIT_RST: |
| 768 | case ISO_S_WAIT_ATR: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 769 | TRACE_ERROR("%u: Received UART char in invalid 7816 state " |
| 770 | "%u\r\n", ch->num, ch->state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 771 | /* we shouldn't receive any data from the reader yet! */ |
| 772 | break; |
| 773 | case ISO_S_WAIT_TPDU: |
| 774 | if (byte == 0xff) { |
| 775 | new_state = process_byte_pts(ch, byte); |
| 776 | ch->stats.pps++; |
| 777 | goto out_silent; |
| 778 | } |
| 779 | /* fall-through */ |
| 780 | case ISO_S_IN_TPDU: |
| 781 | new_state = process_byte_tpdu(ch, byte); |
| 782 | break; |
| 783 | case ISO_S_IN_PTS: |
| 784 | new_state = process_byte_pts(ch, byte); |
| 785 | goto out_silent; |
| 786 | } |
| 787 | |
| 788 | out_silent: |
| 789 | if (new_state != -1) |
| 790 | card_set_state(ch, new_state); |
| 791 | } |
| 792 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 793 | /* transmit a single byte to the reader */ |
| 794 | int card_emu_tx_byte(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 795 | { |
| 796 | int rc = 0; |
| 797 | |
| 798 | switch (ch->state) { |
| 799 | case ISO_S_IN_ATR: |
| 800 | if (ch->atr.idx < ch->atr.len) { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 801 | uint8_t byte; |
| 802 | byte = ch->atr.atr[ch->atr.idx++]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 803 | rc = 1; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 804 | |
| 805 | card_emu_uart_tx(ch->uart_chan, byte); |
| 806 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 807 | /* detect end of ATR */ |
| 808 | if (ch->atr.idx >= ch->atr.len) |
| 809 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 810 | } |
| 811 | break; |
| 812 | case ISO_S_IN_PTS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 813 | rc = tx_byte_pts(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 814 | break; |
| 815 | case ISO_S_IN_TPDU: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 816 | rc = tx_byte_tpdu(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 817 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 818 | default: |
| 819 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | if (rc) |
| 823 | ch->stats.tx_bytes++; |
| 824 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 825 | /* if we return 0 here, the UART needs to disable transmit-ready |
| 826 | * interrupts */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 827 | return rc; |
| 828 | } |
| 829 | |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 830 | void card_emu_have_new_uart_tx(struct card_handle *ch) |
| 831 | { |
| 832 | switch (ch->state) { |
| 833 | case ISO_S_IN_TPDU: |
| 834 | switch (ch->tpdu.state) { |
| 835 | case TPDU_S_WAIT_TX: |
| 836 | case TPDU_S_WAIT_PB: |
| 837 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 838 | break; |
| 839 | default: |
| 840 | break; |
| 841 | } |
| 842 | default: |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 847 | void card_emu_report_status(struct card_handle *ch) |
| 848 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 849 | struct msgb *msg; |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 850 | struct cardemu_usb_msg_status *sts; |
| 851 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 852 | msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, |
| 853 | SIMTRACE_MSGT_BD_CEMU_STATUS); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 854 | if (!msg) |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 855 | return; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 856 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 857 | sts = (struct cardemu_usb_msg_status *) msgb_put(msg, sizeof(*sts)); |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 858 | sts->flags = 0; |
| 859 | if (ch->vcc_active) |
| 860 | sts->flags |= CEMU_STATUS_F_VCC_PRESENT; |
| 861 | if (ch->clocked) |
| 862 | sts->flags |= CEMU_STATUS_F_CLK_ACTIVE; |
| 863 | if (ch->in_reset) |
| 864 | sts->flags |= CEMU_STATUS_F_RESET_ACTIVE; |
| 865 | /* FIXME: voltage + card insert */ |
| 866 | sts->fi = ch->fi; |
| 867 | sts->di = ch->di; |
| 868 | sts->wi = ch->wi; |
| 869 | sts->waiting_time = ch->waiting_time; |
| 870 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 871 | usb_buf_upd_len_and_submit(msg); |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 872 | } |
| 873 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 874 | /* hardware driver informs us that a card I/O signal has changed */ |
| 875 | void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active) |
| 876 | { |
| 877 | switch (io) { |
| 878 | case CARD_IO_VCC: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 879 | if (active == 0 && ch->vcc_active == 1) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 880 | TRACE_INFO("%u: VCC deactivated\r\n", ch->num); |
Harald Welte | 22cdf2a | 2016-02-24 22:18:11 +0100 | [diff] [blame] | 881 | tc_etu_disable(ch->tc_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 882 | card_set_state(ch, ISO_S_WAIT_POWER); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 883 | } else if (active == 1 && ch->vcc_active == 0) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 884 | TRACE_INFO("%u: VCC activated\r\n", ch->num); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 885 | card_set_state(ch, ISO_S_WAIT_CLK); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 886 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 887 | ch->vcc_active = active; |
| 888 | break; |
| 889 | case CARD_IO_CLK: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 890 | if (active == 1 && ch->clocked == 0) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 891 | TRACE_INFO("%u: CLK activated\r\n", ch->num); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 892 | if (ch->state == ISO_S_WAIT_CLK) |
| 893 | card_set_state(ch, ISO_S_WAIT_RST); |
| 894 | } else if (active == 0 && ch->clocked == 1) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 895 | TRACE_INFO("%u: CLK deactivated\r\n", ch->num); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 896 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 897 | ch->clocked = active; |
| 898 | break; |
| 899 | case CARD_IO_RST: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 900 | if (active == 0 && ch->in_reset) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 901 | TRACE_INFO("%u: RST released\r\n", ch->num); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 902 | if (ch->vcc_active && ch->clocked) { |
| 903 | /* enable the TC/ETU counter once reset has been released */ |
| 904 | tc_etu_enable(ch->tc_chan); |
| 905 | card_set_state(ch, ISO_S_WAIT_ATR); |
| 906 | /* FIXME: wait 400 to 40k clock cycles before sending ATR */ |
| 907 | card_set_state(ch, ISO_S_IN_ATR); |
| 908 | } |
| 909 | } else if (active && !ch->in_reset) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 910 | TRACE_INFO("%u: RST asserted\r\n", ch->num); |
Harald Welte | 22cdf2a | 2016-02-24 22:18:11 +0100 | [diff] [blame] | 911 | tc_etu_disable(ch->tc_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 912 | } |
| 913 | ch->in_reset = active; |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /* User sets a new ATR to be returned during next card reset */ |
| 919 | int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len) |
| 920 | { |
| 921 | if (len > sizeof(ch->atr.atr)) |
| 922 | return -1; |
| 923 | |
| 924 | memcpy(ch->atr.atr, atr, len); |
| 925 | ch->atr.len = len; |
| 926 | ch->atr.idx = 0; |
| 927 | |
| 928 | /* FIXME: race condition with trasmitting ATR to reader? */ |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /* hardware driver informs us that one (more) ETU has expired */ |
| 934 | void tc_etu_wtime_half_expired(void *handle) |
| 935 | { |
| 936 | struct card_handle *ch = handle; |
| 937 | /* transmit NULL procedure byte well before waiting time expires */ |
Harald Welte | dda7355 | 2016-03-02 10:29:55 +0100 | [diff] [blame] | 938 | switch (ch->state) { |
| 939 | case ISO_S_IN_TPDU: |
| 940 | switch (ch->tpdu.state) { |
| 941 | case TPDU_S_WAIT_PB: |
| 942 | case TPDU_S_WAIT_TX: |
| 943 | putchar('N'); |
| 944 | card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL); |
| 945 | break; |
| 946 | default: |
| 947 | break; |
| 948 | } |
| 949 | break; |
| 950 | default: |
| 951 | break; |
| 952 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* hardware driver informs us that one (more) ETU has expired */ |
| 956 | void tc_etu_wtime_expired(void *handle) |
| 957 | { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 958 | struct card_handle *ch = handle; |
| 959 | TRACE_ERROR("%u: wtime_exp\r\n", ch->num); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* shortest ATR found in smartcard_list.txt */ |
| 963 | static const uint8_t default_atr[] = { 0x3B, 0x02, 0x14, 0x50 }; |
| 964 | |
| 965 | static struct card_handle card_handles[NUM_SLOTS]; |
| 966 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 967 | struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan, |
| 968 | uint8_t in_ep, uint8_t irq_ep) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 969 | { |
| 970 | struct card_handle *ch; |
| 971 | |
| 972 | if (slot_num >= ARRAY_SIZE(card_handles)) |
| 973 | return NULL; |
| 974 | |
| 975 | ch = &card_handles[slot_num]; |
| 976 | |
| 977 | memset(ch, 0, sizeof(*ch)); |
| 978 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 979 | INIT_LLIST_HEAD(&ch->uart_tx_queue); |
| 980 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 981 | /* initialize the card_handle with reasonabe defaults */ |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 982 | ch->num = slot_num; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 983 | ch->irq_ep = irq_ep; |
| 984 | ch->in_ep = in_ep; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 985 | ch->state = ISO_S_WAIT_POWER; |
| 986 | ch->vcc_active = 0; |
| 987 | ch->in_reset = 1; |
| 988 | ch->clocked = 0; |
| 989 | |
| 990 | ch->fi = 0; |
| 991 | ch->di = 1; |
| 992 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 993 | |
| 994 | ch->tc_chan = tc_chan; |
| 995 | ch->uart_chan = uart_chan; |
| 996 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 997 | |
| 998 | ch->atr.idx = 0; |
| 999 | ch->atr.len = sizeof(default_atr); |
| 1000 | memcpy(ch->atr.atr, default_atr, ch->atr.len); |
| 1001 | |
| 1002 | ch->pts.state = PTS_S_WAIT_REQ_PTSS; |
| 1003 | ch->tpdu.state = TPDU_S_WAIT_CLA; |
| 1004 | |
| 1005 | tc_etu_init(ch->tc_chan, ch); |
| 1006 | |
| 1007 | return ch; |
| 1008 | } |