blob: d9e59b0153f8740dc968e45fc9354118438b9c07 [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 Welte63653742019-01-03 16:54:16 +0100174static int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg)
175{
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 */
184static int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg)
185{
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 Welte8772f582019-05-14 16:34:47 +0200205static struct 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)
208{
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 Welte8772f582019-05-14 16:34:47 +0200225static struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
226 enum ccid_error_code err)
227{
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;
406 uint8_t seq = u->icc_power_on.hdr.bSeq;
Harald Welte63653742019-01-03 16:54:16 +0100407 struct msgb *resp;
408
409 /* TODO: send actual ATR; handle error cases */
410 /* TODO: handle this asynchronously */
Harald Welte8772f582019-05-14 16:34:47 +0200411 resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0);
Harald Welte63653742019-01-03 16:54:16 +0100412
Harald Welte8772f582019-05-14 16:34:47 +0200413 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100414}
415
416/* Section 6.1.2 */
417static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg)
418{
419 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200420 const struct ccid_header *ch = (const struct ccid_header *) u;
421 uint8_t seq = u->icc_power_off.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200422 struct msgb *resp;
423
Harald Weltecab5d152019-05-16 13:31:16 +0200424 cs->ci->slot_ops->set_power(cs, false);
Harald Welte8772f582019-05-14 16:34:47 +0200425 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
426 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100427}
428
429/* Section 6.1.4 */
430static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg)
431{
432 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200433 const struct ccid_header *ch = (const struct ccid_header *) u;
434 uint8_t seq = u->xfr_block.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200435 struct msgb *resp;
436
Harald Weltecab5d152019-05-16 13:31:16 +0200437 /* FIXME: handle this asynchronously */
Harald Welte8772f582019-05-14 16:34:47 +0200438 resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0);
439 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100440}
441
442/* Section 6.1.5 */
443static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
444{
445 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200446 const struct ccid_header *ch = (const struct ccid_header *) u;
447 uint8_t seq = u->get_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200448 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200449
Harald Welte8579ce52019-05-15 12:22:24 +0200450 /* FIXME: T=1 */
451 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200452 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100453}
454
455/* Section 6.1.6 */
456static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg)
457{
458 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200459 const struct ccid_header *ch = (const struct ccid_header *) u;
460 uint8_t seq = u->reset_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200461 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200462
Harald Weltecab5d152019-05-16 13:31:16 +0200463 /* copy default parameters from somewhere */
Harald Welte8579ce52019-05-15 12:22:24 +0200464 /* FIXME: T=1 */
Harald Weltecab5d152019-05-16 13:31:16 +0200465 cs->ci->slot_ops->set_params(cs, CCID_PROTOCOL_NUM_T0, cs->default_pars);
466 cs->pars = *cs->default_pars;
467
Harald Welte8579ce52019-05-15 12:22:24 +0200468 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200469 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100470}
471
472/* Section 6.1.7 */
473static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
474{
475 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200476 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200477 const struct ccid_header *ch = (const struct ccid_header *) u;
478 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200479 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200480 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200481 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200482
Harald Welte8579ce52019-05-15 12:22:24 +0200483 switch (spar->bProtocolNum) {
484 case CCID_PROTOCOL_NUM_T0:
485 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200486 break;
487 case CCID_PROTOCOL_NUM_T1:
488 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200489 break;
490 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200491 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200492 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200493 goto out;
494 }
495
496 if (rc < 0) {
497 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
498 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
499 goto out;
500 }
501
502 /* validate parameters; abort if they are not supported */
503 rc = cs->ci->slot_ops->set_params(cs, spar->bProtocolNum, &pars_dec);
504 if (rc < 0) {
505 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
506 } else {
507 cs->pars = pars_dec;
508 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8579ce52019-05-15 12:22:24 +0200509 }
Harald Weltee73a1df2019-05-15 22:27:02 +0200510out:
Harald Welte8772f582019-05-14 16:34:47 +0200511 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100512}
513
514/* Section 6.1.8 */
515static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
516{
517 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200518 const struct ccid_header *ch = (const struct ccid_header *) u;
519 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200520 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200521
522 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
523 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100524}
525
526/* Section 6.1.9 */
527static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
528{
529 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200530 const struct ccid_header *ch = (const struct ccid_header *) u;
531 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200532 struct msgb *resp;
533
Harald Weltecab5d152019-05-16 13:31:16 +0200534 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200535 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
536 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100537}
538
539/* Section 6.1.10 */
540static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
541{
542 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200543 const struct ccid_header *ch = (const struct ccid_header *) u;
544 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200545 struct msgb *resp;
546
Harald Weltecab5d152019-05-16 13:31:16 +0200547 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200548 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
549 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
550 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100551}
552
553/* Section 6.1.11 */
554static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
555{
556 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200557 const struct ccid_header *ch = (const struct ccid_header *) u;
558 uint8_t seq = u->secure.hdr.bSeq;
559 struct msgb *resp;
560
561 /* FIXME */
562 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
563 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100564}
565
566/* Section 6.1.12 */
567static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
568{
569 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200570 const struct ccid_header *ch = (const struct ccid_header *) u;
571 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200572 struct msgb *resp;
573
Harald Welte8772f582019-05-14 16:34:47 +0200574 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
575 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100576}
577
578/* Section 6.1.13 */
579static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
580{
581 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200582 const struct ccid_header *ch = (const struct ccid_header *) u;
583 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200584 struct msgb *resp;
585
Harald Welte8772f582019-05-14 16:34:47 +0200586 /* Check if the currently in-progress message is Abortable */
587 switch (0/* FIXME */) {
588 case PC_to_RDR_IccPowerOn:
589 case PC_to_RDR_XfrBlock:
590 case PC_to_RDR_Escape:
591 case PC_to_RDR_Secure:
592 case PC_to_RDR_Mechanical:
593 //case PC_to_RDR_Abort: /* seriously? WTF! */
594 break;
595 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200596 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200597 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
598 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
599 return ccid_slot_send_unbusy(cs, resp);
600 }
601
602 /* FIXME */
603 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
604 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100605}
606
607/* Section 6.1.14 */
608static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
609{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200610 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200611 const struct ccid_header *ch = (const struct ccid_header *) u;
612 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200613 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
614 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200615 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200616 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200617
Harald Weltecab5d152019-05-16 13:31:16 +0200618 /* FIXME: which rate to return in failure case? */
619 rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps);
620 if (rc < 0)
621 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
622 else
623 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200624 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100625}
626
Harald Welte8772f582019-05-14 16:34:47 +0200627/*! Handle data arriving from the host on the OUT endpoint.
628 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200629 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
630 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
631 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200632 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100633int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
634{
635 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
636 const struct ccid_header *ch = (const struct ccid_header *) u;
637 unsigned int len = msgb_length(msg);
638 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200639 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200640 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100641
642 if (len < sizeof(*ch)) {
643 /* FIXME */
644 return -1;
645 }
646
Harald Welte8772f582019-05-14 16:34:47 +0200647 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200648 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100649 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200650 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200651 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
652 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100653 }
654
Harald Welte8772f582019-05-14 16:34:47 +0200655 /* Check if slot is already busy; Reject any additional commands meanwhile */
656 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200657 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200658 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
659 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
660 CCID_ERR_CMD_SLOT_BUSY);
661 return ccid_send(ci, resp);
662 }
663
Harald Weltee73a1df2019-05-15 22:27:02 +0200664 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
665 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
666
Harald Welte8772f582019-05-14 16:34:47 +0200667 /* we're now processing a command for the slot; mark slot as busy */
668 cs->cmd_busy = true;
669
670 /* TODO: enqueue into the per-slot specific input queue */
671
Harald Weltecab5d152019-05-16 13:31:16 +0200672 /* call pre-processing call-back function; allows reader to update state */
673 if (ci->slot_ops->pre_proc_cb)
674 ci->slot_ops->pre_proc_cb(cs, msg);
675
Harald Welte63653742019-01-03 16:54:16 +0100676 switch (ch->bMessageType) {
677 case PC_to_RDR_GetSlotStatus:
678 if (len != sizeof(u->get_slot_status))
679 goto short_msg;
680 rc = ccid_handle_get_slot_status(cs, msg);
681 break;
682 case PC_to_RDR_IccPowerOn:
683 if (len != sizeof(u->icc_power_on))
684 goto short_msg;
685 rc = ccid_handle_icc_power_on(cs, msg);
686 break;
687 case PC_to_RDR_IccPowerOff:
688 if (len != sizeof(u->icc_power_off))
689 goto short_msg;
690 rc = ccid_handle_icc_power_off(cs, msg);
691 break;
692 case PC_to_RDR_XfrBlock:
693 if (len < sizeof(u->xfr_block))
694 goto short_msg;
695 rc = ccid_handle_xfr_block(cs, msg);
696 break;
697 case PC_to_RDR_GetParameters:
698 if (len != sizeof(u->get_parameters))
699 goto short_msg;
700 rc = ccid_handle_get_parameters(cs, msg);
701 break;
702 case PC_to_RDR_ResetParameters:
703 if (len != sizeof(u->reset_parameters))
704 goto short_msg;
705 rc = ccid_handle_reset_parameters(cs, msg);
706 break;
707 case PC_to_RDR_SetParameters:
708 if (len != sizeof(u->set_parameters))
709 goto short_msg;
710 rc = ccid_handle_set_parameters(cs, msg);
711 break;
712 case PC_to_RDR_Escape:
713 if (len < sizeof(u->escape))
714 goto short_msg;
715 rc = ccid_handle_escape(cs, msg);
716 break;
717 case PC_to_RDR_IccClock:
718 if (len != sizeof(u->icc_clock))
719 goto short_msg;
720 rc = ccid_handle_icc_clock(cs, msg);
721 break;
722 case PC_to_RDR_T0APDU:
723 if (len != /*FIXME*/ sizeof(u->t0apdu))
724 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200725 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100726 break;
727 case PC_to_RDR_Secure:
728 if (len < sizeof(u->secure))
729 goto short_msg;
730 rc = ccid_handle_secure(cs, msg);
731 break;
732 case PC_to_RDR_Mechanical:
733 if (len != sizeof(u->mechanical))
734 goto short_msg;
735 rc = ccid_handle_mechanical(cs, msg);
736 break;
737 case PC_to_RDR_Abort:
738 if (len != sizeof(u->abort))
739 goto short_msg;
740 rc = ccid_handle_abort(cs, msg);
741 break;
742 case PC_to_RDR_SetDataRateAndClockFrequency:
743 if (len != sizeof(u->set_rate_and_clock))
744 goto short_msg;
745 rc = ccid_handle_set_rate_and_clock(cs, msg);
746 break;
747 default:
Harald Welte8772f582019-05-14 16:34:47 +0200748 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200749 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200750 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
751 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200752 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200753 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100754 }
Harald Welte922ff932019-05-16 11:21:51 +0200755 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
756 * of the msgb */
757 if (rc != 1)
758 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200759 return 0;
760
Harald Welte63653742019-01-03 16:54:16 +0100761short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200762 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200763 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200764 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100765}
Harald Weltebcbc1972019-05-15 21:57:32 +0200766
Harald Weltecab5d152019-05-16 13:31:16 +0200767void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
768 const struct ccid_slot_ops *slot_ops, const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200769{
770 int i;
771
772 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
773 struct ccid_slot *cs = &ci->slot[i];
774 cs->slot_nr = i;
775 cs->ci = ci;
776 }
Harald Weltecab5d152019-05-16 13:31:16 +0200777 ci->ops = ops;
778 ci->slot_ops = slot_ops;
Harald Weltebcbc1972019-05-15 21:57:32 +0200779 ci->name = name;
780 ci->priv = priv;
781}