blob: b7dd54c9d18abd1a86c634cbfe8133a686c9f082 [file] [log] [blame]
Harald Welte727d6752019-09-30 21:46:44 +02001/* Code providing a ccid_slot_ops implementation based on iso7716_fsm,
2 * (which in turn sits on top of card_uart) */
3
4#include <unistd.h>
5#include <errno.h>
Harald Welte74f7fb52019-10-10 15:40:02 +02006#include <string.h>
Harald Welte727d6752019-09-30 21:46:44 +02007
8#include <osmocom/core/msgb.h>
9#include <osmocom/core/timer.h>
10#include <osmocom/core/logging.h>
11#include <osmocom/core/fsm.h>
12
13#include "ccid_device.h"
14#include "cuart.h"
15#include "iso7816_fsm.h"
16
17struct iso_fsm_slot {
18 /* CCID slot above us */
19 struct ccid_slot *cs;
20 /* main ISO7816-3 FSM instance beneath us */
21 struct osmo_fsm_inst *fi;
22 /* UART beneath the ISO7816-3 FSM */
23 struct card_uart *cuart;
24 /* bSeq of the operation currently in progress */
25 uint8_t seq;
26};
27
28struct iso_fsm_slot_instance {
29 struct iso_fsm_slot slot[NR_SLOTS];
30};
31
32static struct iso_fsm_slot_instance g_si;
33
Harald Welte4aab33f2019-09-28 23:19:31 +020034static struct iso_fsm_slot *ccid_slot2iso_fsm_slot(struct ccid_slot *cs)
Harald Welte727d6752019-09-30 21:46:44 +020035{
36 OSMO_ASSERT(cs->slot_nr < ARRAY_SIZE(g_si.slot));
37 return &g_si.slot[cs->slot_nr];
38}
39
Harald Welte4aab33f2019-09-28 23:19:31 +020040struct card_uart *cuart4slot_nr(uint8_t slot_nr)
41{
42 OSMO_ASSERT(slot_nr < ARRAY_SIZE(g_si.slot));
43 return g_si.slot[slot_nr].cuart;
44}
45
Harald Welte727d6752019-09-30 21:46:44 +020046static const uint8_t sysmousim_sjs1_atr[] = {
47 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
48 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
49 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
50
51static const struct ccid_pars_decoded iso_fsm_def_pars = {
52 .fi = 372,
53 .di = 1,
54 .clock_stop = CCID_CLOCK_STOP_NOTALLOWED,
55 .inverse_convention = false,
56 .t0 = {
57 .guard_time_etu = 0,
58 .waiting_integer = 0,
59 },
60 /* FIXME: T=1 */
61};
62
63static void iso_fsm_slot_pre_proc_cb(struct ccid_slot *cs, struct msgb *msg)
64{
65 /* do nothing; real hardware would update the slot related state here */
66}
67
68static void iso_fsm_slot_icc_power_on_async(struct ccid_slot *cs, struct msgb *msg,
69 const struct ccid_pc_to_rdr_icc_power_on *ipo)
70{
71 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
72
73 ss->seq = ipo->hdr.bSeq;
74 LOGPCS(cs, LOGL_DEBUG, "scheduling power-up\n");
75
76 /* FIXME: do this via a FSM? */
77 card_uart_ctrl(ss->cuart, CUART_CTL_RST, true);
Harald Welte742163a2019-10-10 13:30:24 +020078 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_ACT_IND, NULL);
Harald Welte727d6752019-09-30 21:46:44 +020079 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, true);
80 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_POWER_UP_IND, NULL);
81 cs->icc_powered = true;
82 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true);
83 usleep(10000);
84 card_uart_ctrl(ss->cuart, CUART_CTL_RST, false);
85 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_REL_IND, NULL);
86
87 msgb_free(msg);
88 /* continues in iso_fsm_clot_user_cb once ATR is received */
89}
90static void iso_fsm_clot_user_cb(struct osmo_fsm_inst *fi, int event, int cause, void *data)
91{
92 struct iso_fsm_slot *ss = iso7816_fsm_get_user_priv(fi);
93 struct ccid_slot *cs = ss->cs;
94 struct msgb *tpdu, *resp;
95
96 LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, cause=%d, data=%p)\n", __func__, event, cause, data);
97
98 switch (event) {
99 case ISO7816_E_ATR_DONE_IND:
100 tpdu = data;
101 /* FIXME: copy response data over */
102 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0,
103 msgb_data(tpdu), msgb_length(tpdu));
104 ccid_slot_send_unbusy(cs, resp);
Harald Welte82779d32019-10-10 14:55:25 +0200105 /* Don't free "TPDU" here, as the ATR should survive */
Harald Welte727d6752019-09-30 21:46:44 +0200106 break;
107 case ISO7816_E_TPDU_DONE_IND:
108 tpdu = data;
109 /* FIXME: copy response data over */
110 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0, msgb_l2(tpdu), msgb_l2len(tpdu));
111 ccid_slot_send_unbusy(cs, resp);
112 msgb_free(tpdu);
113 break;
114 }
115}
116
117static void iso_fsm_slot_xfr_block_async(struct ccid_slot *cs, struct msgb *msg,
118 const struct ccid_pc_to_rdr_xfr_block *xfb)
119{
120 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
Harald Welte74f7fb52019-10-10 15:40:02 +0200121 struct msgb *tpdu;
Harald Welte727d6752019-09-30 21:46:44 +0200122
Harald Welte727d6752019-09-30 21:46:44 +0200123 ss->seq = xfb->hdr.bSeq;
Harald Welte74f7fb52019-10-10 15:40:02 +0200124
125 /* must be '0' for TPDU level exchanges or for short APDU */
126 OSMO_ASSERT(xfb->wLevelParameter == 0x0000);
127 OSMO_ASSERT(msgb_length(msg) > xfb->hdr.dwLength);
128
129 /* 'msg' contains the raw CCID message as received from USB. We could create
130 * a new message buffer for the ISO7816 side here or we could 'strip the CCID
131 * header off the start of the message. Let's KISS and do a copy here */
132 tpdu = msgb_alloc(512, "TPDU");
133 OSMO_ASSERT(tpdu);
134 memcpy(msgb_data(tpdu), xfb->abData, xfb->hdr.dwLength);
135 msgb_put(tpdu, xfb->hdr.dwLength);
136 msgb_free(msg);
137
138 LOGPCS(cs, LOGL_DEBUG, "scheduling TPDU transfer: %s\n", msgb_hexdump(tpdu));
139 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_TPDU_CMD, tpdu);
Harald Welte727d6752019-09-30 21:46:44 +0200140 /* continues in iso_fsm_clot_user_cb once response/error/timeout is received */
141}
142
143
144static void iso_fsm_slot_set_power(struct ccid_slot *cs, bool enable)
145{
146 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
147
148 if (enable) {
149 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, true);
150 } else {
151 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, false);
152 }
153}
154
155static void iso_fsm_slot_set_clock(struct ccid_slot *cs, enum ccid_clock_command cmd)
156{
157 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
158
159 switch (cmd) {
160 case CCID_CLOCK_CMD_STOP:
161 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, false);
162 break;
163 case CCID_CLOCK_CMD_RESTART:
164 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true);
165 break;
166 default:
167 OSMO_ASSERT(0);
168 }
169}
170
171static int iso_fsm_slot_set_params(struct ccid_slot *cs, enum ccid_protocol_num proto,
172 const struct ccid_pars_decoded *pars_dec)
173{
174 /* we always acknowledge all parameters */
175 return 0;
176}
177
178static int iso_fsm_slot_set_rate_and_clock(struct ccid_slot *cs, uint32_t freq_hz, uint32_t rate_bps)
179{
180 /* we always acknowledge all rates/clocks */
181 return 0;
182}
183
184
185static int iso_fsm_slot_init(struct ccid_slot *cs)
186{
187 void *ctx = NULL; /* FIXME */
188 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
189 struct card_uart *cuart = talloc_zero(ctx, struct card_uart);
190 char id_buf[16];
Harald Welte5835f382019-10-10 13:46:13 +0200191 char *devname = NULL;
Harald Welte727d6752019-09-30 21:46:44 +0200192 int rc;
193
194 LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
195
Harald Welte5835f382019-10-10 13:46:13 +0200196 /* HACK: make this in some way configurable so it works both in the firmware
197 * and on the host (functionfs) */
Harald Welte727d6752019-09-30 21:46:44 +0200198 if (cs->slot_nr == 0) {
199 cs->icc_present = true;
200 devname = "/dev/ttyUSB5";
201 }
202
203 if (!cuart)
204 return -ENOMEM;
205
206 snprintf(id_buf, sizeof(id_buf), "SIM%d", cs->slot_nr);
Harald Welte5835f382019-10-10 13:46:13 +0200207 if (devname) {
208 rc = card_uart_open(cuart, "tty", devname);
209 if (rc < 0) {
210 LOGPCS(cs, LOGL_ERROR, "Cannot open UART %s: %d\n", devname, rc);
211 talloc_free(cuart);
212 return rc;
213 }
Harald Welte727d6752019-09-30 21:46:44 +0200214 }
215 ss->fi = iso7816_fsm_alloc(ctx, LOGL_DEBUG, id_buf, cuart, iso_fsm_clot_user_cb, ss);
216 if (!ss->fi) {
Harald Welte5835f382019-10-10 13:46:13 +0200217 LOGPCS(cs, LOGL_ERROR, "Cannot allocate ISO FSM\n");
Harald Welte727d6752019-09-30 21:46:44 +0200218 talloc_free(cuart);
219 return -1;
220 }
221
222 cs->default_pars = &iso_fsm_def_pars;
223 ss->cuart = cuart;
224 ss->cs = cs;
225
226
227 return 0;
228}
229
230const struct ccid_slot_ops iso_fsm_slot_ops = {
231 .init = iso_fsm_slot_init,
232 .pre_proc_cb = iso_fsm_slot_pre_proc_cb,
233 .icc_power_on_async = iso_fsm_slot_icc_power_on_async,
234 .xfr_block_async = iso_fsm_slot_xfr_block_async,
235 .set_power = iso_fsm_slot_set_power,
236 .set_clock = iso_fsm_slot_set_clock,
237 .set_params = iso_fsm_slot_set_params,
238 .set_rate_and_clock = iso_fsm_slot_set_rate_and_clock,
239};