blob: 06c14f1c90aa332843dc4e110580ee13e7f0273d [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>
6
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/timer.h>
9#include <osmocom/core/logging.h>
10#include <osmocom/core/fsm.h>
11
12#include "ccid_device.h"
13#include "cuart.h"
14#include "iso7816_fsm.h"
15
16struct iso_fsm_slot {
17 /* CCID slot above us */
18 struct ccid_slot *cs;
19 /* main ISO7816-3 FSM instance beneath us */
20 struct osmo_fsm_inst *fi;
21 /* UART beneath the ISO7816-3 FSM */
22 struct card_uart *cuart;
23 /* bSeq of the operation currently in progress */
24 uint8_t seq;
25};
26
27struct iso_fsm_slot_instance {
28 struct iso_fsm_slot slot[NR_SLOTS];
29};
30
31static struct iso_fsm_slot_instance g_si;
32
33struct iso_fsm_slot *ccid_slot2iso_fsm_slot(struct ccid_slot *cs)
34{
35 OSMO_ASSERT(cs->slot_nr < ARRAY_SIZE(g_si.slot));
36 return &g_si.slot[cs->slot_nr];
37}
38
39static const uint8_t sysmousim_sjs1_atr[] = {
40 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
41 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
42 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
43
44static const struct ccid_pars_decoded iso_fsm_def_pars = {
45 .fi = 372,
46 .di = 1,
47 .clock_stop = CCID_CLOCK_STOP_NOTALLOWED,
48 .inverse_convention = false,
49 .t0 = {
50 .guard_time_etu = 0,
51 .waiting_integer = 0,
52 },
53 /* FIXME: T=1 */
54};
55
56static void iso_fsm_slot_pre_proc_cb(struct ccid_slot *cs, struct msgb *msg)
57{
58 /* do nothing; real hardware would update the slot related state here */
59}
60
61static void iso_fsm_slot_icc_power_on_async(struct ccid_slot *cs, struct msgb *msg,
62 const struct ccid_pc_to_rdr_icc_power_on *ipo)
63{
64 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
65
66 ss->seq = ipo->hdr.bSeq;
67 LOGPCS(cs, LOGL_DEBUG, "scheduling power-up\n");
68
69 /* FIXME: do this via a FSM? */
70 card_uart_ctrl(ss->cuart, CUART_CTL_RST, true);
71 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, true);
72 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_POWER_UP_IND, NULL);
73 cs->icc_powered = true;
74 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true);
75 usleep(10000);
76 card_uart_ctrl(ss->cuart, CUART_CTL_RST, false);
77 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_REL_IND, NULL);
78
79 msgb_free(msg);
80 /* continues in iso_fsm_clot_user_cb once ATR is received */
81}
82static void iso_fsm_clot_user_cb(struct osmo_fsm_inst *fi, int event, int cause, void *data)
83{
84 struct iso_fsm_slot *ss = iso7816_fsm_get_user_priv(fi);
85 struct ccid_slot *cs = ss->cs;
86 struct msgb *tpdu, *resp;
87
88 LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, cause=%d, data=%p)\n", __func__, event, cause, data);
89
90 switch (event) {
91 case ISO7816_E_ATR_DONE_IND:
92 tpdu = data;
93 /* FIXME: copy response data over */
94 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0,
95 msgb_data(tpdu), msgb_length(tpdu));
96 ccid_slot_send_unbusy(cs, resp);
97 msgb_free(tpdu);
98 break;
99 case ISO7816_E_TPDU_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, msgb_l2(tpdu), msgb_l2len(tpdu));
103 ccid_slot_send_unbusy(cs, resp);
104 msgb_free(tpdu);
105 break;
106 }
107}
108
109static void iso_fsm_slot_xfr_block_async(struct ccid_slot *cs, struct msgb *msg,
110 const struct ccid_pc_to_rdr_xfr_block *xfb)
111{
112 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
113
114 LOGPCS(cs, LOGL_DEBUG, "scheduling TPDU transfer\n");
115 ss->seq = xfb->hdr.bSeq;
116 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_TPDU_CMD, msg);
117 /* continues in iso_fsm_clot_user_cb once response/error/timeout is received */
118}
119
120
121static void iso_fsm_slot_set_power(struct ccid_slot *cs, bool enable)
122{
123 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
124
125 if (enable) {
126 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, true);
127 } else {
128 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, false);
129 }
130}
131
132static void iso_fsm_slot_set_clock(struct ccid_slot *cs, enum ccid_clock_command cmd)
133{
134 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
135
136 switch (cmd) {
137 case CCID_CLOCK_CMD_STOP:
138 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, false);
139 break;
140 case CCID_CLOCK_CMD_RESTART:
141 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true);
142 break;
143 default:
144 OSMO_ASSERT(0);
145 }
146}
147
148static int iso_fsm_slot_set_params(struct ccid_slot *cs, enum ccid_protocol_num proto,
149 const struct ccid_pars_decoded *pars_dec)
150{
151 /* we always acknowledge all parameters */
152 return 0;
153}
154
155static int iso_fsm_slot_set_rate_and_clock(struct ccid_slot *cs, uint32_t freq_hz, uint32_t rate_bps)
156{
157 /* we always acknowledge all rates/clocks */
158 return 0;
159}
160
161
162static int iso_fsm_slot_init(struct ccid_slot *cs)
163{
164 void *ctx = NULL; /* FIXME */
165 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
166 struct card_uart *cuart = talloc_zero(ctx, struct card_uart);
167 char id_buf[16];
168 char *devname = "/dev/null";
169 int rc;
170
171 LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
172
173 if (cs->slot_nr == 0) {
174 cs->icc_present = true;
175 devname = "/dev/ttyUSB5";
176 }
177
178 if (!cuart)
179 return -ENOMEM;
180
181 snprintf(id_buf, sizeof(id_buf), "SIM%d", cs->slot_nr);
182 rc = card_uart_open(cuart, "tty", devname);
183 if (rc < 0) {
184 talloc_free(cuart);
185 return rc;
186 }
187 ss->fi = iso7816_fsm_alloc(ctx, LOGL_DEBUG, id_buf, cuart, iso_fsm_clot_user_cb, ss);
188 if (!ss->fi) {
189 talloc_free(cuart);
190 return -1;
191 }
192
193 cs->default_pars = &iso_fsm_def_pars;
194 ss->cuart = cuart;
195 ss->cs = cs;
196
197
198 return 0;
199}
200
201const struct ccid_slot_ops iso_fsm_slot_ops = {
202 .init = iso_fsm_slot_init,
203 .pre_proc_cb = iso_fsm_slot_pre_proc_cb,
204 .icc_power_on_async = iso_fsm_slot_icc_power_on_async,
205 .xfr_block_async = iso_fsm_slot_xfr_block_async,
206 .set_power = iso_fsm_slot_set_power,
207 .set_clock = iso_fsm_slot_set_clock,
208 .set_params = iso_fsm_slot_set_params,
209 .set_rate_and_clock = iso_fsm_slot_set_rate_and_clock,
210};