blob: 70553b81af88b33314ae324fc51e77c8c3ba498a [file] [log] [blame]
Harald Welte63653742019-01-03 16:54:16 +01001#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <assert.h>
Harald Welte92e7c0b2019-05-14 09:32:10 +02005#include <string.h>
Harald Welte63653742019-01-03 16:54:16 +01006
7#include <osmocom/core/msgb.h>
8#include <osmocom/core/utils.h>
Harald Weltee73a1df2019-05-15 22:27:02 +02009#include <osmocom/core/logging.h>
Harald Welte63653742019-01-03 16:54:16 +010010
11#include "ccid_proto.h"
Harald Welte8d186ad2019-05-15 19:40:29 +020012#include "ccid_device.h"
Harald Welte63653742019-01-03 16:54:16 +010013
Harald Welte8579ce52019-05-15 12:22:24 +020014/* decode on-the-wire T0 parameters into their parsed form */
15static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in)
16{
17 /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */
18 if (in->bmTCCKST0 & 0xFD)
19 return -11;
20 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
21 if (in->bClockStop & 0xFC)
22 return -14;
23
24 out->fi = in->bmFindexDindex >> 4;
25 out->di = in->bmFindexDindex & 0xF;
26 if (in->bmTCCKST0 & 2)
27 out->inverse_convention = true;
28 else
29 out->inverse_convention = false;
30 if (in->bGuardTimeT0 == 0xff)
31 out->t0.guard_time_etu = 0;
32 else
33 out->t0.guard_time_etu = in->bGuardTimeT0;
34 out->t0.waiting_integer = in->bWaitingIntegerT0;
35 out->clock_stop = in->bClockStop & 0x03;
36
37 return 0;
38}
39
40/* encode T0 parameters from parsed form into on-the-wire encoding */
41static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in)
42{
43 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
44 if (in->inverse_convention)
45 out->bmTCCKST0 = 0x02;
46 else
47 out->bmTCCKST0 = 0x00;
48 out->bGuardTimeT0 = in->t0.guard_time_etu;
49 out->bWaitingIntegerT0 = in->t0.waiting_integer;
50 out->bClockStop = in->clock_stop & 0x03;
51}
52
53/* decode on-the-wire T1 parameters into their parsed form */
54static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in)
55{
56 /* input validation: only some values permitted for bmTCCKST0 */
57 if (in->bmTCCKST1 & 0xE8)
58 return -11;
59 /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */
60 if (in->bWaitingIntegersT1 > 0x9F)
61 return -13;
62 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
63 if (in->bClockStop & 0xFC)
64 return -14;
65 /* input validation: only 0x00 to 0xFE permitted for bIFSC */
66 if (in->bIFSC > 0xFE)
67 return -15;
68
69 out->fi = in->bmFindexDindex >> 4;
70 out->di = in->bmFindexDindex & 0xF;
71 if (in->bmTCCKST1 & 1)
72 out->t1.csum_type = CCID_CSUM_TYPE_CRC;
73 else
74 out->t1.csum_type = CCID_CSUM_TYPE_LRC;
75 if (in->bmTCCKST1 & 2)
76 out->inverse_convention = true;
77 else
78 out->inverse_convention = false;
79 out->t1.guard_time_t1 = in->bGuardTimeT1;
80 out->t1.bwi = in->bWaitingIntegersT1 >> 4;
81 out->t1.cwi = in->bWaitingIntegersT1 & 0xF;
82 out->clock_stop = in->bClockStop & 0x03;
83 out->t1.ifsc = in->bIFSC;
84 out->t1.nad = in->bNadValue;
85
86 return 0;
87}
88
89/* encode T1 parameters from parsed form into on-the-wire encoding */
90static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in)
91{
92 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
93 out->bmTCCKST1 = 0x10;
94 if (in->t1.csum_type == CCID_CSUM_TYPE_CRC)
95 out->bmTCCKST1 |= 0x01;
96 if (in->inverse_convention)
97 out->bmTCCKST1 |= 0x02;
98 out->bGuardTimeT1 = in->t1.guard_time_t1;
99 out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F);
100 out->bClockStop = in->clock_stop & 0x03;
101 out->bIFSC = in->t1.ifsc;
102 out->bNadValue = in->t1.nad;
103}
104
Harald Welte63653742019-01-03 16:54:16 +0100105#define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x)
106#define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x)
107
108static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr)
109{
110 if (slot_nr >= sizeof(ci->slot))
111 return NULL;
112 else
Harald Welte92e7c0b2019-05-14 09:32:10 +0200113 return &ci->slot[slot_nr];
Harald Welte63653742019-01-03 16:54:16 +0100114}
115
116static uint8_t get_icc_status(const struct ccid_slot *cs)
117{
118 if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset)
119 return CCID_ICC_STATUS_PRES_ACT;
120 else if (!cs->icc_present)
121 return CCID_ICC_STATUS_NO_ICC;
122 else
123 return CCID_ICC_STATUS_PRES_INACT;
124}
125
126#define SET_HDR(x, msg_type, slot, seq) do { \
127 (x)->hdr.bMessageType = msg_type; \
128 (x)->hdr.dwLength = 0; \
129 (x)->hdr.bSlot = slot; \
130 (x)->hdr.bSeq = seq; \
131 } while (0)
132
133#define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \
134 SET_HDR(&(x)->hdr, msg_type, slot, seq); \
135 (x)->hdr.bStatus = status; \
136 (x)->hdr.bError = error; \
137 } while (0)
138
Harald Welte8772f582019-05-14 16:34:47 +0200139#if 0
140static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u)
141{
142 const struct ccid_header *ch = (const struct ccid_header *) u;
143 return ch->bSeq;
144}
145#endif
146
Harald Welte63653742019-01-03 16:54:16 +0100147/***********************************************************************
148 * Message generation / sending
149 ***********************************************************************/
150
151static struct msgb *ccid_msgb_alloc(void)
152{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200153 struct msgb *msg = msgb_alloc(512, "ccid");
Harald Welte63653742019-01-03 16:54:16 +0100154 OSMO_ASSERT(msg);
155 return msg;
156}
157
Harald Welte8772f582019-05-14 16:34:47 +0200158/* Send given CCID message */
Harald Welte63653742019-01-03 16:54:16 +0100159static int ccid_send(struct ccid_instance *ci, struct msgb *msg)
160{
Harald Weltee73a1df2019-05-15 22:27:02 +0200161 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
162 struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot);
163 if (cs) {
164 LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
165 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
166 } else {
167 LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
168 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
169 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200170 return ci->ops->send_in(ci, msg);
Harald Welte63653742019-01-03 16:54:16 +0100171}
172
Harald Welte8772f582019-05-14 16:34:47 +0200173/* Send given CCID message for given slot; patch bSlot into message */
Harald Welte005b09d2019-05-16 17:24:29 +0200174int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg)
Harald Welte63653742019-01-03 16:54:16 +0100175{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200176 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
Harald Welte63653742019-01-03 16:54:16 +0100177
178 /* patch bSlotNr into message */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200179 ch->bSlot = cs->slot_nr;
Harald Welte63653742019-01-03 16:54:16 +0100180 return ccid_send(cs->ci, msg);
181}
182
Harald Welte8772f582019-05-14 16:34:47 +0200183/* Send given CCID message and mark slot as un-busy */
Harald Welte005b09d2019-05-16 17:24:29 +0200184int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg)
Harald Welte8772f582019-05-14 16:34:47 +0200185{
186 cs->cmd_busy = false;
187 return ccid_slot_send(cs, msg);
188}
Harald Welte63653742019-01-03 16:54:16 +0100189
190/* Section 6.2.1 */
Harald Welte8772f582019-05-14 16:34:47 +0200191static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
192 uint8_t cmd_sts, enum ccid_error_code err,
193 const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100194{
195 struct msgb *msg = ccid_msgb_alloc();
Harald Welte8772f582019-05-14 16:34:47 +0200196 struct ccid_rdr_to_pc_data_block *db =
Harald Welte92e7c0b2019-05-14 09:32:10 +0200197 (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200198 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100199
Harald Welte8772f582019-05-14 16:34:47 +0200200 SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200201 osmo_store32le(data_len, &db->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100202 memcpy(db->abData, data, data_len);
203 return msg;
204}
Harald Welte005b09d2019-05-16 17:24:29 +0200205struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
206 enum ccid_error_code err, const uint8_t *data,
207 uint32_t data_len)
Harald Welte8772f582019-05-14 16:34:47 +0200208{
209 return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
210}
Harald Welte63653742019-01-03 16:54:16 +0100211
212/* Section 6.2.2 */
Harald Welte8772f582019-05-14 16:34:47 +0200213static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status,
214 uint8_t seq, uint8_t cmd_sts,
215 enum ccid_error_code err)
Harald Welte63653742019-01-03 16:54:16 +0100216{
217 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200218 struct ccid_rdr_to_pc_slot_status *ss =
219 (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss));
Harald Welte8772f582019-05-14 16:34:47 +0200220 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100221
Harald Welte8772f582019-05-14 16:34:47 +0200222 SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err);
Harald Welte63653742019-01-03 16:54:16 +0100223 return msg;
224}
Harald Welte005b09d2019-05-16 17:24:29 +0200225struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
226 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200227{
228 return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err);
229}
Harald Welte63653742019-01-03 16:54:16 +0100230
231/* Section 6.2.3 */
Harald Welte8772f582019-05-14 16:34:47 +0200232static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status,
233 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200234 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200235{
236 struct msgb *msg = ccid_msgb_alloc();
237 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200238 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0));
Harald Welte8772f582019-05-14 16:34:47 +0200239 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
240
241 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200242 if (dec_par) {
243 osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength);
244 encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200245 }
246 return msg;
247}
248static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200249 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200250{
Harald Welte8579ce52019-05-15 12:22:24 +0200251 return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars);
Harald Welte8772f582019-05-14 16:34:47 +0200252}
253
254static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status,
255 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200256 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200257{
258 struct msgb *msg = ccid_msgb_alloc();
259 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200260 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1));
Harald Welte8772f582019-05-14 16:34:47 +0200261 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
262
263 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200264 if (dec_par) {
265 osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength);
266 encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200267 }
268 return msg;
269}
270static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200271 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200272{
Harald Welte8579ce52019-05-15 12:22:24 +0200273 return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars);
Harald Welte8772f582019-05-14 16:34:47 +0200274}
275
Harald Welte63653742019-01-03 16:54:16 +0100276
277/* Section 6.2.4 */
Harald Welte8772f582019-05-14 16:34:47 +0200278static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts,
279 enum ccid_error_code err, const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100280{
281 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200282 struct ccid_rdr_to_pc_escape *esc =
283 (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200284 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100285
Harald Welte8772f582019-05-14 16:34:47 +0200286 SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200287 osmo_store32le(data_len, &esc->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100288 memcpy(esc->abData, data, data_len);
289 return msg;
290}
Harald Welte8772f582019-05-14 16:34:47 +0200291static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
292 enum ccid_error_code err, const uint8_t *data,
293 uint32_t data_len)
294{
295 return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
296}
Harald Welte63653742019-01-03 16:54:16 +0100297
298/* Section 6.2.5 */
Harald Welte8772f582019-05-14 16:34:47 +0200299static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
300 uint8_t cmd_sts, enum ccid_error_code err,
301 uint32_t clock_khz, uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100302{
303 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200304 struct ccid_rdr_to_pc_data_rate_and_clock *drc =
305 (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc));
Harald Welte8772f582019-05-14 16:34:47 +0200306 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100307
Harald Welte8772f582019-05-14 16:34:47 +0200308 SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200309 osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */
310 osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */
311 osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */
Harald Welte63653742019-01-03 16:54:16 +0100312 return msg;
313}
Harald Welte8772f582019-05-14 16:34:47 +0200314static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
315 enum ccid_error_code err, uint32_t clock_khz,
316 uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100317{
Harald Welte8772f582019-05-14 16:34:47 +0200318 return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err,
319 clock_khz, rate_bps);
Harald Welte63653742019-01-03 16:54:16 +0100320}
Harald Welte8772f582019-05-14 16:34:47 +0200321
322/*! generate an error response for given input message_type/slot_nr/seq
323 * \param[in] msg_type CCID Message Type against which response is to be created
324 * \param[in] slot_nr CCID Slot Number
325 * \param[in] icc_status ICC Status of the slot
326 * \param[in] seq CCID Sequence number
327 * \param[in] err_code CCID Error Code to send
328 * \returns dynamically-allocated message buffer containing error response */
329static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status,
330 uint8_t seq, enum ccid_error_code err_code)
331{
332 struct msgb *resp = NULL;
333
334 switch (msg_type) {
335 case PC_to_RDR_IccPowerOn:
336 case PC_to_RDR_XfrBlock:
337 case PC_to_RDR_Secure:
338 /* Return RDR_to_PC_DataBlock */
339 resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
340 err_code, NULL, 0);
341 break;
342
343 case PC_to_RDR_IccPowerOff:
344 case PC_to_RDR_GetSlotStatus:
345 case PC_to_RDR_IccClock:
346 case PC_to_RDR_T0APDU:
347 case PC_to_RDR_Mechanical:
348 case PC_to_RDR_Abort:
349 /* Return RDR_to_PC_SlotStatus */
350 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
351 err_code);
352 break;
353
354 case PC_to_RDR_GetParameters:
355 case PC_to_RDR_ResetParameters:
356 case PC_to_RDR_SetParameters:
357 /* Return RDR_to_PC_Parameters */
358 resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
359 err_code, NULL); /* FIXME: parameters? */
360 break;
361
362 case PC_to_RDR_Escape:
363 /* Return RDR_to_PC_Escape */
364 resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
365 err_code, NULL, 0);
366 break;
367
368 case PC_to_RDR_SetDataRateAndClockFrequency:
369 /* Return RDR_to_PC_SlotStatus */
370 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
371 err_code);
372 break;
373
374 default:
375 /* generate general error */
376 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
377 CCID_ERR_CMD_NOT_SUPPORTED);
378 break;
379 }
380 return resp;
381}
Harald Welte63653742019-01-03 16:54:16 +0100382
383/***********************************************************************
384 * Message reception / parsing
385 ***********************************************************************/
386
387/* Section 6.1.3 */
388static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg)
389{
390 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200391 const struct ccid_header *ch = (const struct ccid_header *) u;
392 uint8_t seq = u->get_slot_status.hdr.bSeq;
Harald Welte63653742019-01-03 16:54:16 +0100393 struct msgb *resp;
394
Harald Welte8772f582019-05-14 16:34:47 +0200395 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte63653742019-01-03 16:54:16 +0100396
Harald Welte8772f582019-05-14 16:34:47 +0200397 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100398}
399
400
401/* Section 6.1.1 */
402static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg)
403{
404 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200405 const struct ccid_header *ch = (const struct ccid_header *) u;
Harald Welte63653742019-01-03 16:54:16 +0100406
Harald Welte505d4412019-05-16 17:26:09 +0200407 /* handle this asynchronously */
408 cs->ci->slot_ops->icc_power_on_async(cs, msg, &u->icc_power_on);
409 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100410}
411
412/* Section 6.1.2 */
413static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg)
414{
415 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200416 const struct ccid_header *ch = (const struct ccid_header *) u;
417 uint8_t seq = u->icc_power_off.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200418 struct msgb *resp;
419
Harald Weltecab5d152019-05-16 13:31:16 +0200420 cs->ci->slot_ops->set_power(cs, false);
Harald Welte8772f582019-05-14 16:34:47 +0200421 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
422 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100423}
424
425/* Section 6.1.4 */
426static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg)
427{
428 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200429 const struct ccid_header *ch = (const struct ccid_header *) u;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200430
Harald Welte505d4412019-05-16 17:26:09 +0200431 /* handle this asynchronously */
432 cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block);
433 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100434}
435
436/* Section 6.1.5 */
437static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
438{
439 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200440 const struct ccid_header *ch = (const struct ccid_header *) u;
441 uint8_t seq = u->get_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200442 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200443
Harald Welte8579ce52019-05-15 12:22:24 +0200444 /* FIXME: T=1 */
445 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200446 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100447}
448
449/* Section 6.1.6 */
450static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg)
451{
452 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200453 const struct ccid_header *ch = (const struct ccid_header *) u;
454 uint8_t seq = u->reset_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200455 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200456
Harald Weltecab5d152019-05-16 13:31:16 +0200457 /* copy default parameters from somewhere */
Harald Welte8579ce52019-05-15 12:22:24 +0200458 /* FIXME: T=1 */
Harald Weltecab5d152019-05-16 13:31:16 +0200459 cs->ci->slot_ops->set_params(cs, CCID_PROTOCOL_NUM_T0, cs->default_pars);
460 cs->pars = *cs->default_pars;
461
Harald Welte8579ce52019-05-15 12:22:24 +0200462 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200463 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100464}
465
466/* Section 6.1.7 */
467static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
468{
469 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200470 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200471 const struct ccid_header *ch = (const struct ccid_header *) u;
472 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200473 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200474 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200475 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200476
Harald Welte8579ce52019-05-15 12:22:24 +0200477 switch (spar->bProtocolNum) {
478 case CCID_PROTOCOL_NUM_T0:
479 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200480 break;
481 case CCID_PROTOCOL_NUM_T1:
482 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200483 break;
484 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200485 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200486 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200487 goto out;
488 }
489
490 if (rc < 0) {
491 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
492 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
493 goto out;
494 }
495
496 /* validate parameters; abort if they are not supported */
497 rc = cs->ci->slot_ops->set_params(cs, spar->bProtocolNum, &pars_dec);
498 if (rc < 0) {
499 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
500 } else {
501 cs->pars = pars_dec;
502 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8579ce52019-05-15 12:22:24 +0200503 }
Harald Weltee73a1df2019-05-15 22:27:02 +0200504out:
Harald Welte8772f582019-05-14 16:34:47 +0200505 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100506}
507
508/* Section 6.1.8 */
509static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
510{
511 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200512 const struct ccid_header *ch = (const struct ccid_header *) u;
513 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200514 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200515
516 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
517 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100518}
519
520/* Section 6.1.9 */
521static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
522{
523 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200524 const struct ccid_header *ch = (const struct ccid_header *) u;
525 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200526 struct msgb *resp;
527
Harald Weltecab5d152019-05-16 13:31:16 +0200528 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200529 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
530 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100531}
532
533/* Section 6.1.10 */
534static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
535{
536 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200537 const struct ccid_header *ch = (const struct ccid_header *) u;
538 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200539 struct msgb *resp;
540
Harald Weltecab5d152019-05-16 13:31:16 +0200541 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200542 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
543 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
544 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100545}
546
547/* Section 6.1.11 */
548static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
549{
550 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200551 const struct ccid_header *ch = (const struct ccid_header *) u;
552 uint8_t seq = u->secure.hdr.bSeq;
553 struct msgb *resp;
554
555 /* FIXME */
556 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
557 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100558}
559
560/* Section 6.1.12 */
561static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
562{
563 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200564 const struct ccid_header *ch = (const struct ccid_header *) u;
565 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200566 struct msgb *resp;
567
Harald Welte8772f582019-05-14 16:34:47 +0200568 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
569 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100570}
571
572/* Section 6.1.13 */
573static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
574{
575 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200576 const struct ccid_header *ch = (const struct ccid_header *) u;
577 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200578 struct msgb *resp;
579
Harald Welte8772f582019-05-14 16:34:47 +0200580 /* Check if the currently in-progress message is Abortable */
581 switch (0/* FIXME */) {
582 case PC_to_RDR_IccPowerOn:
583 case PC_to_RDR_XfrBlock:
584 case PC_to_RDR_Escape:
585 case PC_to_RDR_Secure:
586 case PC_to_RDR_Mechanical:
587 //case PC_to_RDR_Abort: /* seriously? WTF! */
588 break;
589 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200590 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200591 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
592 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
593 return ccid_slot_send_unbusy(cs, resp);
594 }
595
596 /* FIXME */
597 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
598 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100599}
600
601/* Section 6.1.14 */
602static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
603{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200604 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200605 const struct ccid_header *ch = (const struct ccid_header *) u;
606 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200607 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
608 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200609 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200610 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200611
Harald Weltecab5d152019-05-16 13:31:16 +0200612 /* FIXME: which rate to return in failure case? */
613 rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps);
614 if (rc < 0)
615 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
616 else
617 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200618 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100619}
620
Harald Welte8772f582019-05-14 16:34:47 +0200621/*! Handle data arriving from the host on the OUT endpoint.
622 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200623 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
624 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
625 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200626 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100627int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
628{
629 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
630 const struct ccid_header *ch = (const struct ccid_header *) u;
631 unsigned int len = msgb_length(msg);
632 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200633 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200634 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100635
636 if (len < sizeof(*ch)) {
637 /* FIXME */
638 return -1;
639 }
640
Harald Welte8772f582019-05-14 16:34:47 +0200641 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200642 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100643 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200644 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200645 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
646 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100647 }
648
Harald Welte8772f582019-05-14 16:34:47 +0200649 /* Check if slot is already busy; Reject any additional commands meanwhile */
650 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200651 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200652 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
653 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
654 CCID_ERR_CMD_SLOT_BUSY);
655 return ccid_send(ci, resp);
656 }
657
Harald Weltee73a1df2019-05-15 22:27:02 +0200658 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
659 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
660
Harald Welte8772f582019-05-14 16:34:47 +0200661 /* we're now processing a command for the slot; mark slot as busy */
662 cs->cmd_busy = true;
663
664 /* TODO: enqueue into the per-slot specific input queue */
665
Harald Weltecab5d152019-05-16 13:31:16 +0200666 /* call pre-processing call-back function; allows reader to update state */
667 if (ci->slot_ops->pre_proc_cb)
668 ci->slot_ops->pre_proc_cb(cs, msg);
669
Harald Welte63653742019-01-03 16:54:16 +0100670 switch (ch->bMessageType) {
671 case PC_to_RDR_GetSlotStatus:
672 if (len != sizeof(u->get_slot_status))
673 goto short_msg;
674 rc = ccid_handle_get_slot_status(cs, msg);
675 break;
676 case PC_to_RDR_IccPowerOn:
677 if (len != sizeof(u->icc_power_on))
678 goto short_msg;
679 rc = ccid_handle_icc_power_on(cs, msg);
680 break;
681 case PC_to_RDR_IccPowerOff:
682 if (len != sizeof(u->icc_power_off))
683 goto short_msg;
684 rc = ccid_handle_icc_power_off(cs, msg);
685 break;
686 case PC_to_RDR_XfrBlock:
687 if (len < sizeof(u->xfr_block))
688 goto short_msg;
689 rc = ccid_handle_xfr_block(cs, msg);
690 break;
691 case PC_to_RDR_GetParameters:
692 if (len != sizeof(u->get_parameters))
693 goto short_msg;
694 rc = ccid_handle_get_parameters(cs, msg);
695 break;
696 case PC_to_RDR_ResetParameters:
697 if (len != sizeof(u->reset_parameters))
698 goto short_msg;
699 rc = ccid_handle_reset_parameters(cs, msg);
700 break;
701 case PC_to_RDR_SetParameters:
702 if (len != sizeof(u->set_parameters))
703 goto short_msg;
704 rc = ccid_handle_set_parameters(cs, msg);
705 break;
706 case PC_to_RDR_Escape:
707 if (len < sizeof(u->escape))
708 goto short_msg;
709 rc = ccid_handle_escape(cs, msg);
710 break;
711 case PC_to_RDR_IccClock:
712 if (len != sizeof(u->icc_clock))
713 goto short_msg;
714 rc = ccid_handle_icc_clock(cs, msg);
715 break;
716 case PC_to_RDR_T0APDU:
717 if (len != /*FIXME*/ sizeof(u->t0apdu))
718 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200719 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100720 break;
721 case PC_to_RDR_Secure:
722 if (len < sizeof(u->secure))
723 goto short_msg;
724 rc = ccid_handle_secure(cs, msg);
725 break;
726 case PC_to_RDR_Mechanical:
727 if (len != sizeof(u->mechanical))
728 goto short_msg;
729 rc = ccid_handle_mechanical(cs, msg);
730 break;
731 case PC_to_RDR_Abort:
732 if (len != sizeof(u->abort))
733 goto short_msg;
734 rc = ccid_handle_abort(cs, msg);
735 break;
736 case PC_to_RDR_SetDataRateAndClockFrequency:
737 if (len != sizeof(u->set_rate_and_clock))
738 goto short_msg;
739 rc = ccid_handle_set_rate_and_clock(cs, msg);
740 break;
741 default:
Harald Welte8772f582019-05-14 16:34:47 +0200742 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200743 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200744 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
745 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200746 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200747 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100748 }
Harald Welte922ff932019-05-16 11:21:51 +0200749 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
750 * of the msgb */
751 if (rc != 1)
752 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200753 return 0;
754
Harald Welte63653742019-01-03 16:54:16 +0100755short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200756 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200757 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200758 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100759}
Harald Weltebcbc1972019-05-15 21:57:32 +0200760
Harald Weltecab5d152019-05-16 13:31:16 +0200761void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
762 const struct ccid_slot_ops *slot_ops, const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200763{
764 int i;
765
Harald Weltecab5d152019-05-16 13:31:16 +0200766 ci->ops = ops;
767 ci->slot_ops = slot_ops;
Harald Weltebcbc1972019-05-15 21:57:32 +0200768 ci->name = name;
769 ci->priv = priv;
Harald Welted3385072019-05-16 17:38:23 +0200770
771 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
772 struct ccid_slot *cs = &ci->slot[i];
773 cs->slot_nr = i;
774 cs->ci = ci;
775
776 slot_ops->init(cs);
777 }
778
Harald Weltebcbc1972019-05-15 21:57:32 +0200779}