Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 1 | /* Code providing a ccid_slot_ops implementation based on iso7716_fsm, |
| 2 | * (which in turn sits on top of card_uart) */ |
| 3 | |
Harald Welte | a67be5f | 2020-09-03 10:04:36 +0200 | [diff] [blame] | 4 | /* (C) 2019-2020 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 19 | */ |
| 20 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <errno.h> |
Harald Welte | 6def1cf | 2019-10-10 15:40:02 +0200 | [diff] [blame] | 23 | #include <string.h> |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 24 | |
| 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/logging.h> |
| 28 | #include <osmocom/core/fsm.h> |
| 29 | |
| 30 | #include "ccid_device.h" |
| 31 | #include "cuart.h" |
| 32 | #include "iso7816_fsm.h" |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 33 | #include "iso7816_3.h" |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 34 | |
| 35 | struct iso_fsm_slot { |
| 36 | /* CCID slot above us */ |
| 37 | struct ccid_slot *cs; |
| 38 | /* main ISO7816-3 FSM instance beneath us */ |
| 39 | struct osmo_fsm_inst *fi; |
| 40 | /* UART beneath the ISO7816-3 FSM */ |
| 41 | struct card_uart *cuart; |
| 42 | /* bSeq of the operation currently in progress */ |
| 43 | uint8_t seq; |
| 44 | }; |
| 45 | |
| 46 | struct iso_fsm_slot_instance { |
| 47 | struct iso_fsm_slot slot[NR_SLOTS]; |
| 48 | }; |
| 49 | |
| 50 | static struct iso_fsm_slot_instance g_si; |
| 51 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 52 | static struct iso_fsm_slot *ccid_slot2iso_fsm_slot(struct ccid_slot *cs) |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 53 | { |
| 54 | OSMO_ASSERT(cs->slot_nr < ARRAY_SIZE(g_si.slot)); |
| 55 | return &g_si.slot[cs->slot_nr]; |
| 56 | } |
| 57 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 58 | struct card_uart *cuart4slot_nr(uint8_t slot_nr) |
| 59 | { |
| 60 | OSMO_ASSERT(slot_nr < ARRAY_SIZE(g_si.slot)); |
| 61 | return g_si.slot[slot_nr].cuart; |
| 62 | } |
| 63 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 64 | static const uint8_t sysmousim_sjs1_atr[] = { |
| 65 | 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31, |
| 66 | 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20, |
| 67 | 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 }; |
| 68 | |
| 69 | static const struct ccid_pars_decoded iso_fsm_def_pars = { |
| 70 | .fi = 372, |
| 71 | .di = 1, |
| 72 | .clock_stop = CCID_CLOCK_STOP_NOTALLOWED, |
| 73 | .inverse_convention = false, |
| 74 | .t0 = { |
| 75 | .guard_time_etu = 0, |
| 76 | .waiting_integer = 0, |
| 77 | }, |
| 78 | /* FIXME: T=1 */ |
| 79 | }; |
| 80 | |
| 81 | static void iso_fsm_slot_pre_proc_cb(struct ccid_slot *cs, struct msgb *msg) |
| 82 | { |
| 83 | /* do nothing; real hardware would update the slot related state here */ |
| 84 | } |
| 85 | |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 86 | static void iso_fsm_slot_icc_set_insertion_status(struct ccid_slot *cs, bool present) { |
| 87 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
| 88 | |
| 89 | if(present == cs->icc_present) |
| 90 | return; |
| 91 | |
| 92 | cs->icc_present = present; |
| 93 | |
| 94 | if (!present) { |
| 95 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_CARD_REMOVAL, NULL); |
| 96 | card_uart_ctrl(ss->cuart, CUART_CTL_RST, true); |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 97 | card_uart_ctrl(ss->cuart, CUART_CTL_POWER_5V0, false); |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 98 | cs->icc_powered = false; |
| 99 | cs->cmd_busy = false; |
| 100 | } |
| 101 | } |
| 102 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 103 | static void iso_fsm_slot_icc_power_on_async(struct ccid_slot *cs, struct msgb *msg, |
| 104 | const struct ccid_pc_to_rdr_icc_power_on *ipo) |
| 105 | { |
| 106 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 107 | enum ccid_power_select pwrsel = ipo->bPowerSelect; |
| 108 | enum card_uart_ctl cctl; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 109 | |
| 110 | ss->seq = ipo->hdr.bSeq; |
| 111 | LOGPCS(cs, LOGL_DEBUG, "scheduling power-up\n"); |
| 112 | |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 113 | switch (pwrsel) { |
| 114 | case CCID_PWRSEL_5V0: cctl = CUART_CTL_POWER_5V0; break; |
| 115 | case CCID_PWRSEL_3V0: cctl = CUART_CTL_POWER_3V0; break; |
| 116 | case CCID_PWRSEL_1V8: cctl = CUART_CTL_POWER_1V8; break; |
| 117 | default: cctl = CUART_CTL_POWER_5V0; |
| 118 | } |
| 119 | |
Eric Wild | aed26cc | 2020-03-30 04:22:36 +0200 | [diff] [blame] | 120 | if (! cs->icc_powered) { |
| 121 | /* FIXME: do this via a FSM? */ |
| 122 | card_uart_ctrl(ss->cuart, CUART_CTL_RST, true); |
| 123 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_ACT_IND, NULL); |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 124 | card_uart_ctrl(ss->cuart, cctl, true); |
Eric Wild | aed26cc | 2020-03-30 04:22:36 +0200 | [diff] [blame] | 125 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_POWER_UP_IND, NULL); |
| 126 | cs->icc_powered = true; |
| 127 | card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true); |
| 128 | #ifdef OCTSIMFWBUILD |
| 129 | delay_us(10000); |
| 130 | #else |
| 131 | usleep(10000); |
| 132 | #endif |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 133 | |
Eric Wild | aed26cc | 2020-03-30 04:22:36 +0200 | [diff] [blame] | 134 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_REL_IND, NULL); |
| 135 | card_uart_ctrl(ss->cuart, CUART_CTL_RST, false); |
| 136 | } else { /* warm reset */ |
| 137 | card_uart_ctrl(ss->cuart, CUART_CTL_RST, true); |
| 138 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_ACT_IND, NULL); |
| 139 | #ifdef OCTSIMFWBUILD |
| 140 | delay_us(10000); |
| 141 | #else |
| 142 | usleep(10000); |
| 143 | #endif |
| 144 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_REL_IND, NULL); |
| 145 | card_uart_ctrl(ss->cuart, CUART_CTL_RST, false); |
| 146 | } |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 147 | msgb_free(msg); |
| 148 | /* continues in iso_fsm_clot_user_cb once ATR is received */ |
| 149 | } |
| 150 | static void iso_fsm_clot_user_cb(struct osmo_fsm_inst *fi, int event, int cause, void *data) |
| 151 | { |
| 152 | struct iso_fsm_slot *ss = iso7816_fsm_get_user_priv(fi); |
| 153 | struct ccid_slot *cs = ss->cs; |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 154 | |
| 155 | switch (event) { |
| 156 | case ISO7816_E_ATR_DONE_IND: |
| 157 | case ISO7816_E_ATR_ERR_IND: |
| 158 | case ISO7816_E_TPDU_DONE_IND: |
| 159 | case ISO7816_E_TPDU_FAILED_IND: |
| 160 | case ISO7816_E_PPS_DONE_IND: |
| 161 | case ISO7816_E_PPS_FAILED_IND: |
Eric Wild | 91f75ce | 2020-03-30 04:10:31 +0200 | [diff] [blame] | 162 | case ISO7816_E_WTIME_EXP: |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 163 | cs->event_data = data; |
Eric Wild | e84a571 | 2019-11-28 17:30:30 +0100 | [diff] [blame] | 164 | #ifdef OCTSIMFWBUILD |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 165 | asm volatile("dmb st": : :"memory"); |
Eric Wild | e84a571 | 2019-11-28 17:30:30 +0100 | [diff] [blame] | 166 | #endif |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 167 | cs->event = event; |
| 168 | break; |
| 169 | default: |
| 170 | LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, cause=%d, data=%p) unhandled\n", |
| 171 | __func__, event, cause, data); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static int iso_handle_fsm_events(struct ccid_slot *cs, bool enable){ |
| 177 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 178 | struct msgb *tpdu, *resp; |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 179 | volatile uint32_t event = cs->event; |
| 180 | volatile void * volatile data = cs->event_data; |
| 181 | |
| 182 | if(!event) |
| 183 | return 0; |
Eric Wild | 91f75ce | 2020-03-30 04:10:31 +0200 | [diff] [blame] | 184 | // if(event && !data) |
| 185 | // return 0; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 186 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 187 | switch (event) { |
Eric Wild | 91f75ce | 2020-03-30 04:10:31 +0200 | [diff] [blame] | 188 | case ISO7816_E_WTIME_EXP: |
| 189 | tpdu = data; |
| 190 | LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=0)\n", __func__, event); |
| 191 | |
| 192 | |
| 193 | /* perform deactivation */ |
| 194 | card_uart_ctrl(ss->cuart, CUART_CTL_RST, true); |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 195 | card_uart_ctrl(ss->cuart, CUART_CTL_POWER_5V0, false); |
Eric Wild | 91f75ce | 2020-03-30 04:10:31 +0200 | [diff] [blame] | 196 | cs->icc_powered = false; |
| 197 | |
| 198 | |
| 199 | resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_FAILED, CCID_ERR_ICC_MUTE, 0, 0); |
| 200 | ccid_slot_send_unbusy(cs, resp); |
| 201 | cs->event = 0; |
| 202 | break; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 203 | case ISO7816_E_ATR_DONE_IND: |
| 204 | tpdu = data; |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 205 | LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event, |
Harald Welte | 22dd1ff | 2019-10-10 15:40:53 +0200 | [diff] [blame] | 206 | msgb_hexdump(tpdu)); |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 207 | resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0, |
| 208 | msgb_data(tpdu), msgb_length(tpdu)); |
| 209 | ccid_slot_send_unbusy(cs, resp); |
Harald Welte | bbb5009 | 2019-10-10 14:55:25 +0200 | [diff] [blame] | 210 | /* Don't free "TPDU" here, as the ATR should survive */ |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 211 | cs->event = 0; |
| 212 | break; |
| 213 | case ISO7816_E_ATR_ERR_IND: |
| 214 | tpdu = data; |
| 215 | LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event, |
| 216 | msgb_hexdump(tpdu)); |
| 217 | resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_FAILED, CCID_ERR_ICC_MUTE, |
| 218 | msgb_data(tpdu), msgb_length(tpdu)); |
| 219 | ccid_slot_send_unbusy(cs, resp); |
| 220 | /* Don't free "TPDU" here, as the ATR should survive */ |
| 221 | cs->event = 0; |
| 222 | break; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 223 | break; |
| 224 | case ISO7816_E_TPDU_DONE_IND: |
| 225 | tpdu = data; |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 226 | LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event, |
Harald Welte | 22dd1ff | 2019-10-10 15:40:53 +0200 | [diff] [blame] | 227 | msgb_hexdump(tpdu)); |
Harald Welte | 43281fe | 2020-07-30 20:29:02 +0200 | [diff] [blame] | 228 | resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0, msgb_l4(tpdu), msgb_l4len(tpdu)); |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 229 | ccid_slot_send_unbusy(cs, resp); |
| 230 | msgb_free(tpdu); |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 231 | cs->event = 0; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 232 | break; |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 233 | case ISO7816_E_TPDU_FAILED_IND: |
| 234 | tpdu = data; |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 235 | LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event, |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 236 | msgb_hexdump(tpdu)); |
| 237 | /* FIXME: other error causes than card removal?*/ |
| 238 | resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_FAILED, CCID_ERR_ICC_MUTE, msgb_l2(tpdu), 0); |
| 239 | ccid_slot_send_unbusy(cs, resp); |
| 240 | msgb_free(tpdu); |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 241 | cs->event = 0; |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 242 | break; |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 243 | case ISO7816_E_PPS_DONE_IND: |
| 244 | tpdu = data; |
| 245 | /* pps was successful, so we know these values are fine */ |
| 246 | uint16_t F = iso7816_3_fi_table[cs->proposed_pars.fi]; |
| 247 | uint8_t D = iso7816_3_di_table[cs->proposed_pars.di]; |
| 248 | uint32_t fmax = iso7816_3_fmax_table[cs->proposed_pars.fi]; |
| 249 | |
Eric Wild | a057457 | 2019-11-21 15:28:59 +0100 | [diff] [blame] | 250 | /* 7816-3 5.2.3 |
| 251 | * No information shall be exchanged when switching the |
| 252 | * frequency value. Two different times are recommended |
| 253 | * for switching the frequency value, either |
| 254 | * - after ATR while card is idle |
| 255 | * - after PPS while card is idle |
| 256 | */ |
Eric Wild | 587d4fb | 2019-11-27 18:59:43 +0100 | [diff] [blame] | 257 | card_uart_ctrl(ss->cuart, CUART_CTL_SET_CLOCK_FREQ, fmax); |
| 258 | card_uart_ctrl(ss->cuart, CUART_CTL_SET_FD, F/D); |
Eric Wild | 3ef02c0 | 2020-09-11 00:58:59 +0200 | [diff] [blame] | 259 | card_uart_ctrl(ss->cuart, CUART_CTL_WTIME, cs->proposed_pars.t0.waiting_integer * 960); |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 260 | |
| 261 | cs->pars = cs->proposed_pars; |
| 262 | resp = ccid_gen_parameters_t0(cs, ss->seq, CCID_CMD_STATUS_OK, 0); |
| 263 | |
| 264 | ccid_slot_send_unbusy(cs, resp); |
| 265 | |
| 266 | /* this frees the pps req from the host, pps resp buffer stays with the pps fsm */ |
| 267 | msgb_free(tpdu); |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 268 | cs->event = 0; |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 269 | break; |
| 270 | case ISO7816_E_PPS_FAILED_IND: |
| 271 | tpdu = data; |
| 272 | /* failed fi/di */ |
| 273 | resp = ccid_gen_parameters_t0(cs, ss->seq, CCID_CMD_STATUS_FAILED, 10); |
| 274 | ccid_slot_send_unbusy(cs, resp); |
| 275 | /* this frees the pps req from the host, pps resp buffer stays with the pps fsm */ |
| 276 | msgb_free(tpdu); |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 277 | cs->event = 0; |
| 278 | break; |
| 279 | case 0: |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 280 | break; |
Harald Welte | 22dd1ff | 2019-10-10 15:40:53 +0200 | [diff] [blame] | 281 | default: |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 282 | LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, data=%p) unhandled\n", |
| 283 | __func__, event, data); |
Harald Welte | 22dd1ff | 2019-10-10 15:40:53 +0200 | [diff] [blame] | 284 | break; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 288 | static int iso_fsm_slot_xfr_block_async(struct ccid_slot *cs, struct msgb *msg, |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 289 | const struct ccid_pc_to_rdr_xfr_block *xfb) |
| 290 | { |
| 291 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 292 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 293 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 294 | ss->seq = xfb->hdr.bSeq; |
Harald Welte | 6def1cf | 2019-10-10 15:40:02 +0200 | [diff] [blame] | 295 | |
| 296 | /* must be '0' for TPDU level exchanges or for short APDU */ |
Eric Wild | 672a960 | 2020-10-17 01:02:39 +0200 | [diff] [blame] | 297 | if (xfb->wLevelParameter != 0x0000) |
| 298 | return -8; |
| 299 | |
| 300 | if (msgb_length(msg) != xfb->hdr.dwLength) |
| 301 | return -1; |
Harald Welte | 6def1cf | 2019-10-10 15:40:02 +0200 | [diff] [blame] | 302 | |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 303 | msgb_pull(msg, 10); |
Harald Welte | 6def1cf | 2019-10-10 15:40:02 +0200 | [diff] [blame] | 304 | |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 305 | LOGPCS(cs, LOGL_DEBUG, "scheduling TPDU transfer: %s\n", msgb_hexdump(msg)); |
| 306 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_TPDU_CMD, msg); |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 307 | /* continues in iso_fsm_clot_user_cb once response/error/timeout is received */ |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 308 | return 0; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
| 312 | static void iso_fsm_slot_set_power(struct ccid_slot *cs, bool enable) |
| 313 | { |
| 314 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
| 315 | |
| 316 | if (enable) { |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 317 | card_uart_ctrl(ss->cuart, CUART_CTL_POWER_5V0, true); |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 318 | cs->icc_powered = true; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 319 | } else { |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 320 | card_uart_ctrl(ss->cuart, CUART_CTL_POWER_5V0, false); |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 321 | cs->icc_powered = false; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | static void iso_fsm_slot_set_clock(struct ccid_slot *cs, enum ccid_clock_command cmd) |
| 326 | { |
| 327 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
| 328 | |
| 329 | switch (cmd) { |
| 330 | case CCID_CLOCK_CMD_STOP: |
| 331 | card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, false); |
| 332 | break; |
| 333 | case CCID_CLOCK_CMD_RESTART: |
| 334 | card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true); |
| 335 | break; |
| 336 | default: |
| 337 | OSMO_ASSERT(0); |
| 338 | } |
| 339 | } |
| 340 | |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 341 | static int iso_fsm_slot_set_params(struct ccid_slot *cs, uint8_t seq, enum ccid_protocol_num proto, |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 342 | const struct ccid_pars_decoded *pars_dec) |
| 343 | { |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 344 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
| 345 | struct msgb *tpdu; |
| 346 | |
| 347 | /* see 6.1.7 for error offsets */ |
| 348 | if(proto != CCID_PROTOCOL_NUM_T0) |
| 349 | return -7; |
| 350 | |
| 351 | if(pars_dec->t0.guard_time_etu != 0) |
| 352 | return -12; |
| 353 | |
| 354 | if(pars_dec->clock_stop != CCID_CLOCK_STOP_NOTALLOWED) |
| 355 | return -14; |
| 356 | |
| 357 | ss->seq = seq; |
| 358 | |
Eric Wild | 45e930d | 2019-11-21 14:38:16 +0100 | [diff] [blame] | 359 | /* FIXME: |
| 360 | When using D=64, the interface device shall ensure a delay |
| 361 | of at least 16 etu between the leading edge of the last |
| 362 | received character and the leading edge of the character transmitted |
| 363 | for initiating a command. |
| 364 | -> we can't really do 4 stop bits?! |
| 365 | */ |
| 366 | |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 367 | /* Hardware does not support SPU, so no PPS2, and PPS3 is reserved anyway */ |
| 368 | tpdu = msgb_alloc(6, "PPSRQ"); |
| 369 | OSMO_ASSERT(tpdu); |
| 370 | msgb_put_u8(tpdu, 0xff); |
| 371 | msgb_put_u8(tpdu, (1 << 4)); /* only PPS1, T=0 */ |
| 372 | msgb_put_u8(tpdu, (pars_dec->fi << 4 | pars_dec->di)); |
| 373 | msgb_put_u8(tpdu, 0xff ^ (1 << 4) ^ (pars_dec->fi << 4 | pars_dec->di)); |
| 374 | |
| 375 | |
| 376 | LOGPCS(cs, LOGL_DEBUG, "scheduling PPS transfer: %s\n", msgb_hexdump(tpdu)); |
| 377 | osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_PPS_CMD, tpdu); |
| 378 | /* continues in iso_fsm_clot_user_cb once response/error/timeout is received */ |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
Eric Wild | 56a5055 | 2020-01-28 16:07:23 +0100 | [diff] [blame] | 382 | static int iso_fsm_slot_set_rate_and_clock(struct ccid_slot *cs, uint32_t* freq_hz, uint32_t* rate_bps) |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 383 | { |
Eric Wild | 56a5055 | 2020-01-28 16:07:23 +0100 | [diff] [blame] | 384 | /* we return the currently used values, since we support automatic features */ |
| 385 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
| 386 | |
| 387 | *rate_bps = card_uart_ctrl(ss->cuart, CUART_CTL_GET_BAUDRATE, false); |
| 388 | *freq_hz = card_uart_ctrl(ss->cuart, CUART_CTL_GET_CLOCK_FREQ, false)/1000; |
| 389 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 393 | extern void *g_tall_ctx; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 394 | static int iso_fsm_slot_init(struct ccid_slot *cs) |
| 395 | { |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 396 | void *ctx = g_tall_ctx; /* FIXME */ |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 397 | struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs); |
| 398 | struct card_uart *cuart = talloc_zero(ctx, struct card_uart); |
Eric Wild | e84a571 | 2019-11-28 17:30:30 +0100 | [diff] [blame] | 399 | char id_buf[3+3+1]; |
| 400 | char devname[2+1]; |
| 401 | char *devnamep = 0; |
| 402 | char *drivername = "asf4"; |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 403 | int rc; |
| 404 | |
| 405 | LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__); |
| 406 | |
Eric Wild | e84a571 | 2019-11-28 17:30:30 +0100 | [diff] [blame] | 407 | snprintf(id_buf, sizeof(id_buf), "SIM%d", cs->slot_nr); |
| 408 | #ifdef OCTSIMFWBUILD |
| 409 | snprintf(devname, sizeof(devname), "%d", cs->slot_nr); |
| 410 | devnamep = devname; |
| 411 | #else |
| 412 | if (cs->slot_nr == 0) { |
| 413 | cs->icc_present = true; |
| 414 | devnamep = "/dev/ttyUSB5"; |
| 415 | } |
| 416 | drivername = "tty"; |
| 417 | #endif |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 418 | |
| 419 | if (!cuart) |
| 420 | return -ENOMEM; |
| 421 | |
Eric Wild | e84a571 | 2019-11-28 17:30:30 +0100 | [diff] [blame] | 422 | if (devnamep) { |
| 423 | rc = card_uart_open(cuart, drivername, devnamep); |
Harald Welte | 515d5b2 | 2019-10-10 13:46:13 +0200 | [diff] [blame] | 424 | if (rc < 0) { |
| 425 | LOGPCS(cs, LOGL_ERROR, "Cannot open UART %s: %d\n", devname, rc); |
| 426 | talloc_free(cuart); |
| 427 | return rc; |
| 428 | } |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 429 | } |
Eric Wild | e84a571 | 2019-11-28 17:30:30 +0100 | [diff] [blame] | 430 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 431 | ss->fi = iso7816_fsm_alloc(ctx, LOGL_DEBUG, id_buf, cuart, iso_fsm_clot_user_cb, ss); |
| 432 | if (!ss->fi) { |
Harald Welte | 515d5b2 | 2019-10-10 13:46:13 +0200 | [diff] [blame] | 433 | LOGPCS(cs, LOGL_ERROR, "Cannot allocate ISO FSM\n"); |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 434 | talloc_free(cuart); |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | cs->default_pars = &iso_fsm_def_pars; |
| 439 | ss->cuart = cuart; |
| 440 | ss->cs = cs; |
| 441 | |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | const struct ccid_slot_ops iso_fsm_slot_ops = { |
| 447 | .init = iso_fsm_slot_init, |
| 448 | .pre_proc_cb = iso_fsm_slot_pre_proc_cb, |
| 449 | .icc_power_on_async = iso_fsm_slot_icc_power_on_async, |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 450 | .icc_set_insertion_status = iso_fsm_slot_icc_set_insertion_status, |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 451 | .xfr_block_async = iso_fsm_slot_xfr_block_async, |
| 452 | .set_power = iso_fsm_slot_set_power, |
| 453 | .set_clock = iso_fsm_slot_set_clock, |
| 454 | .set_params = iso_fsm_slot_set_params, |
| 455 | .set_rate_and_clock = iso_fsm_slot_set_rate_and_clock, |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 456 | .handle_fsm_events = iso_handle_fsm_events, |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 457 | }; |