blob: 9e72e11ad46f4fe853ca24e6ca8a7ae0fab9d5f6 [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 Welte6982fab2019-05-16 23:01:35 +020014/* local, stand-alone definition of a USB control request */
15struct _usb_ctrl_req {
16 uint8_t bRequestType;
17 uint8_t bRequest;
18 uint16_t wValue;
19 uint16_t wIndex;
20 uint16_t wLength;
21} __attribute__ ((packed));;
22
Harald Welte8579ce52019-05-15 12:22:24 +020023/* decode on-the-wire T0 parameters into their parsed form */
24static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in)
25{
Eric Wildad1edce2019-11-27 16:51:08 +010026 /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0
27 * if (in->bmTCCKST0 & 0xFD)
28 * return -11;
29 * 7816-3 6.1.7 says: "Note: the CCID ignores this bit", placeholder for GETparameters */
30
Harald Welte8579ce52019-05-15 12:22:24 +020031 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
32 if (in->bClockStop & 0xFC)
33 return -14;
34
35 out->fi = in->bmFindexDindex >> 4;
36 out->di = in->bmFindexDindex & 0xF;
37 if (in->bmTCCKST0 & 2)
38 out->inverse_convention = true;
39 else
40 out->inverse_convention = false;
41 if (in->bGuardTimeT0 == 0xff)
42 out->t0.guard_time_etu = 0;
43 else
44 out->t0.guard_time_etu = in->bGuardTimeT0;
45 out->t0.waiting_integer = in->bWaitingIntegerT0;
46 out->clock_stop = in->bClockStop & 0x03;
47
48 return 0;
49}
50
51/* encode T0 parameters from parsed form into on-the-wire encoding */
52static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in)
53{
54 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
55 if (in->inverse_convention)
56 out->bmTCCKST0 = 0x02;
57 else
58 out->bmTCCKST0 = 0x00;
59 out->bGuardTimeT0 = in->t0.guard_time_etu;
60 out->bWaitingIntegerT0 = in->t0.waiting_integer;
61 out->bClockStop = in->clock_stop & 0x03;
62}
63
64/* decode on-the-wire T1 parameters into their parsed form */
65static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in)
66{
67 /* input validation: only some values permitted for bmTCCKST0 */
68 if (in->bmTCCKST1 & 0xE8)
69 return -11;
70 /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */
71 if (in->bWaitingIntegersT1 > 0x9F)
72 return -13;
73 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
74 if (in->bClockStop & 0xFC)
75 return -14;
76 /* input validation: only 0x00 to 0xFE permitted for bIFSC */
77 if (in->bIFSC > 0xFE)
78 return -15;
79
80 out->fi = in->bmFindexDindex >> 4;
81 out->di = in->bmFindexDindex & 0xF;
82 if (in->bmTCCKST1 & 1)
83 out->t1.csum_type = CCID_CSUM_TYPE_CRC;
84 else
85 out->t1.csum_type = CCID_CSUM_TYPE_LRC;
86 if (in->bmTCCKST1 & 2)
87 out->inverse_convention = true;
88 else
89 out->inverse_convention = false;
90 out->t1.guard_time_t1 = in->bGuardTimeT1;
91 out->t1.bwi = in->bWaitingIntegersT1 >> 4;
92 out->t1.cwi = in->bWaitingIntegersT1 & 0xF;
93 out->clock_stop = in->bClockStop & 0x03;
94 out->t1.ifsc = in->bIFSC;
95 out->t1.nad = in->bNadValue;
96
97 return 0;
98}
99
100/* encode T1 parameters from parsed form into on-the-wire encoding */
101static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in)
102{
103 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
104 out->bmTCCKST1 = 0x10;
105 if (in->t1.csum_type == CCID_CSUM_TYPE_CRC)
106 out->bmTCCKST1 |= 0x01;
107 if (in->inverse_convention)
108 out->bmTCCKST1 |= 0x02;
109 out->bGuardTimeT1 = in->t1.guard_time_t1;
110 out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F);
111 out->bClockStop = in->clock_stop & 0x03;
112 out->bIFSC = in->t1.ifsc;
113 out->bNadValue = in->t1.nad;
114}
115
Harald Welte63653742019-01-03 16:54:16 +0100116#define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x)
117#define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x)
118
119static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr)
120{
121 if (slot_nr >= sizeof(ci->slot))
122 return NULL;
123 else
Harald Welte92e7c0b2019-05-14 09:32:10 +0200124 return &ci->slot[slot_nr];
Harald Welte63653742019-01-03 16:54:16 +0100125}
126
127static uint8_t get_icc_status(const struct ccid_slot *cs)
128{
129 if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset)
130 return CCID_ICC_STATUS_PRES_ACT;
131 else if (!cs->icc_present)
132 return CCID_ICC_STATUS_NO_ICC;
133 else
134 return CCID_ICC_STATUS_PRES_INACT;
135}
136
137#define SET_HDR(x, msg_type, slot, seq) do { \
138 (x)->hdr.bMessageType = msg_type; \
139 (x)->hdr.dwLength = 0; \
140 (x)->hdr.bSlot = slot; \
141 (x)->hdr.bSeq = seq; \
142 } while (0)
143
144#define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \
145 SET_HDR(&(x)->hdr, msg_type, slot, seq); \
146 (x)->hdr.bStatus = status; \
147 (x)->hdr.bError = error; \
148 } while (0)
149
Harald Welte8772f582019-05-14 16:34:47 +0200150#if 0
151static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u)
152{
153 const struct ccid_header *ch = (const struct ccid_header *) u;
154 return ch->bSeq;
155}
156#endif
157
Harald Welte63653742019-01-03 16:54:16 +0100158/***********************************************************************
159 * Message generation / sending
160 ***********************************************************************/
161
162static struct msgb *ccid_msgb_alloc(void)
163{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200164 struct msgb *msg = msgb_alloc(512, "ccid");
Harald Welte63653742019-01-03 16:54:16 +0100165 OSMO_ASSERT(msg);
166 return msg;
167}
168
Harald Welte8772f582019-05-14 16:34:47 +0200169/* Send given CCID message */
Harald Welte63653742019-01-03 16:54:16 +0100170static int ccid_send(struct ccid_instance *ci, struct msgb *msg)
171{
Harald Weltee73a1df2019-05-15 22:27:02 +0200172 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
173 struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot);
174 if (cs) {
175 LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
176 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
177 } else {
178 LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
179 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
180 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200181 return ci->ops->send_in(ci, msg);
Harald Welte63653742019-01-03 16:54:16 +0100182}
183
Harald Welte8772f582019-05-14 16:34:47 +0200184/* Send given CCID message for given slot; patch bSlot into message */
Harald Welte005b09d2019-05-16 17:24:29 +0200185int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg)
Harald Welte63653742019-01-03 16:54:16 +0100186{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200187 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
Harald Welte63653742019-01-03 16:54:16 +0100188
189 /* patch bSlotNr into message */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200190 ch->bSlot = cs->slot_nr;
Harald Welte63653742019-01-03 16:54:16 +0100191 return ccid_send(cs->ci, msg);
192}
193
Harald Welte8772f582019-05-14 16:34:47 +0200194/* Send given CCID message and mark slot as un-busy */
Harald Welte005b09d2019-05-16 17:24:29 +0200195int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg)
Harald Welte8772f582019-05-14 16:34:47 +0200196{
197 cs->cmd_busy = false;
198 return ccid_slot_send(cs, msg);
199}
Harald Welte63653742019-01-03 16:54:16 +0100200
201/* Section 6.2.1 */
Harald Welte8772f582019-05-14 16:34:47 +0200202static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
203 uint8_t cmd_sts, enum ccid_error_code err,
204 const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100205{
206 struct msgb *msg = ccid_msgb_alloc();
Harald Welte8772f582019-05-14 16:34:47 +0200207 struct ccid_rdr_to_pc_data_block *db =
Harald Welte92e7c0b2019-05-14 09:32:10 +0200208 (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200209 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100210
Harald Welte8772f582019-05-14 16:34:47 +0200211 SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200212 osmo_store32le(data_len, &db->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100213 memcpy(db->abData, data, data_len);
214 return msg;
215}
Harald Welte005b09d2019-05-16 17:24:29 +0200216struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
217 enum ccid_error_code err, const uint8_t *data,
218 uint32_t data_len)
Harald Welte8772f582019-05-14 16:34:47 +0200219{
220 return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
221}
Harald Welte63653742019-01-03 16:54:16 +0100222
223/* Section 6.2.2 */
Harald Welte8772f582019-05-14 16:34:47 +0200224static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status,
225 uint8_t seq, uint8_t cmd_sts,
226 enum ccid_error_code err)
Harald Welte63653742019-01-03 16:54:16 +0100227{
228 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200229 struct ccid_rdr_to_pc_slot_status *ss =
230 (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss));
Harald Welte8772f582019-05-14 16:34:47 +0200231 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100232
Harald Welte8772f582019-05-14 16:34:47 +0200233 SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err);
Harald Welte63653742019-01-03 16:54:16 +0100234 return msg;
235}
Harald Welte005b09d2019-05-16 17:24:29 +0200236struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
237 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200238{
239 return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err);
240}
Harald Welte63653742019-01-03 16:54:16 +0100241
242/* Section 6.2.3 */
Harald Welte8772f582019-05-14 16:34:47 +0200243static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status,
244 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200245 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200246{
247 struct msgb *msg = ccid_msgb_alloc();
248 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200249 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0));
Harald Welte8772f582019-05-14 16:34:47 +0200250 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
251
252 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200253 if (dec_par) {
254 osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength);
255 encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200256 }
257 return msg;
258}
Eric Wildad1edce2019-11-27 16:51:08 +0100259struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200260 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200261{
Harald Welte8579ce52019-05-15 12:22:24 +0200262 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 +0200263}
264
265static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status,
266 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200267 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200268{
269 struct msgb *msg = ccid_msgb_alloc();
270 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200271 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1));
Harald Welte8772f582019-05-14 16:34:47 +0200272 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
273
274 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200275 if (dec_par) {
276 osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength);
277 encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200278 }
279 return msg;
280}
Eric Wildad1edce2019-11-27 16:51:08 +0100281struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200282 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200283{
Harald Welte8579ce52019-05-15 12:22:24 +0200284 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 +0200285}
286
Harald Welte63653742019-01-03 16:54:16 +0100287
288/* Section 6.2.4 */
Harald Welte8772f582019-05-14 16:34:47 +0200289static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts,
290 enum ccid_error_code err, const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100291{
292 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200293 struct ccid_rdr_to_pc_escape *esc =
294 (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200295 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100296
Harald Welte8772f582019-05-14 16:34:47 +0200297 SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200298 osmo_store32le(data_len, &esc->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100299 memcpy(esc->abData, data, data_len);
300 return msg;
301}
Harald Welte8772f582019-05-14 16:34:47 +0200302static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
303 enum ccid_error_code err, const uint8_t *data,
304 uint32_t data_len)
305{
306 return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
307}
Harald Welte63653742019-01-03 16:54:16 +0100308
309/* Section 6.2.5 */
Harald Welte8772f582019-05-14 16:34:47 +0200310static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
311 uint8_t cmd_sts, enum ccid_error_code err,
312 uint32_t clock_khz, uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100313{
314 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200315 struct ccid_rdr_to_pc_data_rate_and_clock *drc =
316 (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc));
Harald Welte8772f582019-05-14 16:34:47 +0200317 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100318
Harald Welte8772f582019-05-14 16:34:47 +0200319 SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200320 osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */
321 osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */
322 osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */
Harald Welte63653742019-01-03 16:54:16 +0100323 return msg;
324}
Harald Welte8772f582019-05-14 16:34:47 +0200325static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
326 enum ccid_error_code err, uint32_t clock_khz,
327 uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100328{
Harald Welte8772f582019-05-14 16:34:47 +0200329 return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err,
330 clock_khz, rate_bps);
Harald Welte63653742019-01-03 16:54:16 +0100331}
Harald Welte8772f582019-05-14 16:34:47 +0200332
333/*! generate an error response for given input message_type/slot_nr/seq
334 * \param[in] msg_type CCID Message Type against which response is to be created
335 * \param[in] slot_nr CCID Slot Number
336 * \param[in] icc_status ICC Status of the slot
337 * \param[in] seq CCID Sequence number
338 * \param[in] err_code CCID Error Code to send
339 * \returns dynamically-allocated message buffer containing error response */
340static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status,
341 uint8_t seq, enum ccid_error_code err_code)
342{
343 struct msgb *resp = NULL;
344
345 switch (msg_type) {
346 case PC_to_RDR_IccPowerOn:
347 case PC_to_RDR_XfrBlock:
348 case PC_to_RDR_Secure:
349 /* Return RDR_to_PC_DataBlock */
350 resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
351 err_code, NULL, 0);
352 break;
353
354 case PC_to_RDR_IccPowerOff:
355 case PC_to_RDR_GetSlotStatus:
356 case PC_to_RDR_IccClock:
357 case PC_to_RDR_T0APDU:
358 case PC_to_RDR_Mechanical:
359 case PC_to_RDR_Abort:
360 /* Return RDR_to_PC_SlotStatus */
361 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
362 err_code);
363 break;
364
365 case PC_to_RDR_GetParameters:
366 case PC_to_RDR_ResetParameters:
367 case PC_to_RDR_SetParameters:
368 /* Return RDR_to_PC_Parameters */
369 resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
370 err_code, NULL); /* FIXME: parameters? */
371 break;
372
373 case PC_to_RDR_Escape:
374 /* Return RDR_to_PC_Escape */
375 resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
376 err_code, NULL, 0);
377 break;
378
379 case PC_to_RDR_SetDataRateAndClockFrequency:
380 /* Return RDR_to_PC_SlotStatus */
381 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
382 err_code);
383 break;
384
385 default:
386 /* generate general error */
387 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
388 CCID_ERR_CMD_NOT_SUPPORTED);
389 break;
390 }
391 return resp;
392}
Harald Welte63653742019-01-03 16:54:16 +0100393
394/***********************************************************************
395 * Message reception / parsing
396 ***********************************************************************/
397
398/* Section 6.1.3 */
399static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg)
400{
401 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200402 const struct ccid_header *ch = (const struct ccid_header *) u;
403 uint8_t seq = u->get_slot_status.hdr.bSeq;
Harald Welte63653742019-01-03 16:54:16 +0100404 struct msgb *resp;
405
Harald Welte8772f582019-05-14 16:34:47 +0200406 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte63653742019-01-03 16:54:16 +0100407
Harald Welte8772f582019-05-14 16:34:47 +0200408 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100409}
410
411
412/* Section 6.1.1 */
413static int ccid_handle_icc_power_on(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;
Harald Welte63653742019-01-03 16:54:16 +0100417
Harald Welte505d4412019-05-16 17:26:09 +0200418 /* handle this asynchronously */
419 cs->ci->slot_ops->icc_power_on_async(cs, msg, &u->icc_power_on);
420 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100421}
422
423/* Section 6.1.2 */
424static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg)
425{
426 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200427 const struct ccid_header *ch = (const struct ccid_header *) u;
428 uint8_t seq = u->icc_power_off.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200429 struct msgb *resp;
430
Harald Weltecab5d152019-05-16 13:31:16 +0200431 cs->ci->slot_ops->set_power(cs, false);
Harald Welte8772f582019-05-14 16:34:47 +0200432 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
433 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100434}
435
436/* Section 6.1.4 */
437static int ccid_handle_xfr_block(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;
Eric Wild9e622dc2019-11-27 14:43:16 +0100441 struct msgb *resp;
442 int rc;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200443
Harald Welte505d4412019-05-16 17:26:09 +0200444 /* handle this asynchronously */
Eric Wild9e622dc2019-11-27 14:43:16 +0100445 rc = cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block);
446 if (rc < 0) {
447 msgb_trim(msg, sizeof(struct ccid_rdr_to_pc_data_block));
448 resp = ccid_gen_data_block(cs, u->xfr_block.hdr.bSeq, CCID_CMD_STATUS_FAILED, -rc, 0, 0);
449 goto out;
450 }
451 /* busy */
452 return 1;
453out:
454 ccid_slot_send_unbusy(cs, resp);
Harald Welte505d4412019-05-16 17:26:09 +0200455 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100456}
457
458/* Section 6.1.5 */
459static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
460{
461 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200462 const struct ccid_header *ch = (const struct ccid_header *) u;
463 uint8_t seq = u->get_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200464 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200465
Harald Welte8579ce52019-05-15 12:22:24 +0200466 /* FIXME: T=1 */
467 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200468 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100469}
470
471/* Section 6.1.6 */
472static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg)
473{
474 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200475 const struct ccid_header *ch = (const struct ccid_header *) u;
476 uint8_t seq = u->reset_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200477 struct msgb *resp;
Eric Wild91552312019-11-18 13:57:11 +0100478 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200479
Harald Weltecab5d152019-05-16 13:31:16 +0200480 /* copy default parameters from somewhere */
Harald Welte8579ce52019-05-15 12:22:24 +0200481 /* FIXME: T=1 */
Harald Weltecab5d152019-05-16 13:31:16 +0200482
Eric Wild50c88162020-01-28 16:15:43 +0100483 cs->proposed_pars = *cs->default_pars;
484
Eric Wild91552312019-11-18 13:57:11 +0100485 /* validate parameters; abort if they are not supported */
486 rc = cs->ci->slot_ops->set_params(cs, seq, CCID_PROTOCOL_NUM_T0, cs->default_pars);
487 if (rc < 0) {
488 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
489 goto out;
490 }
491
492 msgb_free(msg);
493 /* busy, tdpu like callback */
494 return 1;
495out:
496 msgb_free(msg);
497 ccid_slot_send_unbusy(cs, resp);
498 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100499}
500
501/* Section 6.1.7 */
502static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
503{
504 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200505 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200506 const struct ccid_header *ch = (const struct ccid_header *) u;
507 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200508 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200509 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200510 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200511
Harald Welte8579ce52019-05-15 12:22:24 +0200512 switch (spar->bProtocolNum) {
513 case CCID_PROTOCOL_NUM_T0:
514 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200515 break;
516 case CCID_PROTOCOL_NUM_T1:
517 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200518 break;
519 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200520 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200521 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200522 goto out;
523 }
524
525 if (rc < 0) {
526 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
527 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
528 goto out;
529 }
530
Eric Wildad1edce2019-11-27 16:51:08 +0100531 cs->proposed_pars = pars_dec;
532
Harald Weltecab5d152019-05-16 13:31:16 +0200533 /* validate parameters; abort if they are not supported */
Eric Wildad1edce2019-11-27 16:51:08 +0100534 rc = cs->ci->slot_ops->set_params(cs, seq, spar->bProtocolNum, &pars_dec);
Harald Weltecab5d152019-05-16 13:31:16 +0200535 if (rc < 0) {
536 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
Eric Wildad1edce2019-11-27 16:51:08 +0100537 goto out;
Harald Welte8579ce52019-05-15 12:22:24 +0200538 }
Eric Wild91552312019-11-18 13:57:11 +0100539
540 msgb_free(msg);
Eric Wildad1edce2019-11-27 16:51:08 +0100541 /* busy, tdpu like callback */
542 return 1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200543out:
Eric Wild91552312019-11-18 13:57:11 +0100544 msgb_free(msg);
545 ccid_slot_send_unbusy(cs, resp);
546 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100547}
548
549/* Section 6.1.8 */
550static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
551{
552 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200553 const struct ccid_header *ch = (const struct ccid_header *) u;
554 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200555 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200556
557 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
558 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100559}
560
561/* Section 6.1.9 */
562static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
563{
564 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200565 const struct ccid_header *ch = (const struct ccid_header *) u;
566 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200567 struct msgb *resp;
568
Harald Weltecab5d152019-05-16 13:31:16 +0200569 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200570 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
571 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100572}
573
574/* Section 6.1.10 */
575static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
576{
577 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200578 const struct ccid_header *ch = (const struct ccid_header *) u;
579 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200580 struct msgb *resp;
581
Harald Weltecab5d152019-05-16 13:31:16 +0200582 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200583 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
584 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
585 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100586}
587
588/* Section 6.1.11 */
589static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
590{
591 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200592 const struct ccid_header *ch = (const struct ccid_header *) u;
593 uint8_t seq = u->secure.hdr.bSeq;
594 struct msgb *resp;
595
596 /* FIXME */
597 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
598 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100599}
600
601/* Section 6.1.12 */
602static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
603{
604 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->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200607 struct msgb *resp;
608
Harald Welte8772f582019-05-14 16:34:47 +0200609 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
610 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100611}
612
613/* Section 6.1.13 */
614static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
615{
616 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200617 const struct ccid_header *ch = (const struct ccid_header *) u;
618 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200619 struct msgb *resp;
620
Harald Welte8772f582019-05-14 16:34:47 +0200621 /* Check if the currently in-progress message is Abortable */
622 switch (0/* FIXME */) {
623 case PC_to_RDR_IccPowerOn:
624 case PC_to_RDR_XfrBlock:
625 case PC_to_RDR_Escape:
626 case PC_to_RDR_Secure:
627 case PC_to_RDR_Mechanical:
628 //case PC_to_RDR_Abort: /* seriously? WTF! */
629 break;
630 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200631 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200632 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
633 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
634 return ccid_slot_send_unbusy(cs, resp);
635 }
636
637 /* FIXME */
638 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
639 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100640}
641
642/* Section 6.1.14 */
643static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
644{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200645 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200646 const struct ccid_header *ch = (const struct ccid_header *) u;
647 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200648 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
649 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200650 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200651 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200652
Harald Weltecab5d152019-05-16 13:31:16 +0200653 /* FIXME: which rate to return in failure case? */
Eric Wild56a50552020-01-28 16:07:23 +0100654 rc = cs->ci->slot_ops->set_rate_and_clock(cs, &freq_hz, &rate_bps);
Harald Weltecab5d152019-05-16 13:31:16 +0200655 if (rc < 0)
656 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
657 else
658 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200659 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100660}
661
Harald Welte8772f582019-05-14 16:34:47 +0200662/*! Handle data arriving from the host on the OUT endpoint.
663 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200664 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
665 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
666 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200667 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100668int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
669{
670 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
671 const struct ccid_header *ch = (const struct ccid_header *) u;
672 unsigned int len = msgb_length(msg);
673 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200674 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200675 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100676
677 if (len < sizeof(*ch)) {
678 /* FIXME */
Harald Welte1b3fb632019-11-12 06:45:48 +0100679 msgb_free(msg);
Harald Welte63653742019-01-03 16:54:16 +0100680 return -1;
681 }
682
Harald Welte8772f582019-05-14 16:34:47 +0200683 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200684 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100685 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200686 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200687 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
Harald Welte1b3fb632019-11-12 06:45:48 +0100688 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200689 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100690 }
691
Harald Welte8772f582019-05-14 16:34:47 +0200692 /* Check if slot is already busy; Reject any additional commands meanwhile */
693 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200694 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200695 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
696 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
697 CCID_ERR_CMD_SLOT_BUSY);
Harald Welte1b3fb632019-11-12 06:45:48 +0100698 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200699 return ccid_send(ci, resp);
700 }
701
Eric Wild91552312019-11-18 13:57:11 +0100702 if(!cs->icc_present) {
703 LOGPCS(cs, LOGL_ERROR, "No icc present, but another cmd received\n");
704 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
705 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
706 CCID_ERR_ICC_MUTE);
707 msgb_free(msg);
708 return ccid_send(ci, resp);
709 }
710
Harald Weltee73a1df2019-05-15 22:27:02 +0200711 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
712 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
713
Harald Welte8772f582019-05-14 16:34:47 +0200714 /* we're now processing a command for the slot; mark slot as busy */
715 cs->cmd_busy = true;
716
717 /* TODO: enqueue into the per-slot specific input queue */
718
Harald Weltecab5d152019-05-16 13:31:16 +0200719 /* call pre-processing call-back function; allows reader to update state */
720 if (ci->slot_ops->pre_proc_cb)
721 ci->slot_ops->pre_proc_cb(cs, msg);
722
Harald Welte63653742019-01-03 16:54:16 +0100723 switch (ch->bMessageType) {
724 case PC_to_RDR_GetSlotStatus:
Eric Wild33403af2019-10-01 15:01:58 +0200725 if (len < sizeof(u->get_slot_status))
Harald Welte63653742019-01-03 16:54:16 +0100726 goto short_msg;
727 rc = ccid_handle_get_slot_status(cs, msg);
728 break;
729 case PC_to_RDR_IccPowerOn:
730 if (len != sizeof(u->icc_power_on))
731 goto short_msg;
732 rc = ccid_handle_icc_power_on(cs, msg);
733 break;
734 case PC_to_RDR_IccPowerOff:
735 if (len != sizeof(u->icc_power_off))
736 goto short_msg;
737 rc = ccid_handle_icc_power_off(cs, msg);
738 break;
739 case PC_to_RDR_XfrBlock:
740 if (len < sizeof(u->xfr_block))
741 goto short_msg;
742 rc = ccid_handle_xfr_block(cs, msg);
743 break;
744 case PC_to_RDR_GetParameters:
745 if (len != sizeof(u->get_parameters))
746 goto short_msg;
747 rc = ccid_handle_get_parameters(cs, msg);
748 break;
749 case PC_to_RDR_ResetParameters:
750 if (len != sizeof(u->reset_parameters))
751 goto short_msg;
752 rc = ccid_handle_reset_parameters(cs, msg);
753 break;
754 case PC_to_RDR_SetParameters:
Eric Wild33403af2019-10-01 15:01:58 +0200755 // smallest union member
756 if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10))
Harald Welte63653742019-01-03 16:54:16 +0100757 goto short_msg;
758 rc = ccid_handle_set_parameters(cs, msg);
759 break;
760 case PC_to_RDR_Escape:
761 if (len < sizeof(u->escape))
762 goto short_msg;
763 rc = ccid_handle_escape(cs, msg);
764 break;
765 case PC_to_RDR_IccClock:
766 if (len != sizeof(u->icc_clock))
767 goto short_msg;
768 rc = ccid_handle_icc_clock(cs, msg);
769 break;
770 case PC_to_RDR_T0APDU:
771 if (len != /*FIXME*/ sizeof(u->t0apdu))
772 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200773 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100774 break;
775 case PC_to_RDR_Secure:
776 if (len < sizeof(u->secure))
777 goto short_msg;
778 rc = ccid_handle_secure(cs, msg);
779 break;
780 case PC_to_RDR_Mechanical:
781 if (len != sizeof(u->mechanical))
782 goto short_msg;
783 rc = ccid_handle_mechanical(cs, msg);
784 break;
785 case PC_to_RDR_Abort:
786 if (len != sizeof(u->abort))
787 goto short_msg;
788 rc = ccid_handle_abort(cs, msg);
789 break;
790 case PC_to_RDR_SetDataRateAndClockFrequency:
791 if (len != sizeof(u->set_rate_and_clock))
792 goto short_msg;
793 rc = ccid_handle_set_rate_and_clock(cs, msg);
794 break;
795 default:
Harald Welte8772f582019-05-14 16:34:47 +0200796 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200797 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200798 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
799 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200800 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200801 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100802 }
Harald Welte922ff932019-05-16 11:21:51 +0200803 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
804 * of the msgb */
805 if (rc != 1)
806 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200807 return 0;
808
Harald Welte63653742019-01-03 16:54:16 +0100809short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200810 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200811 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200812 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100813}
Harald Weltebcbc1972019-05-15 21:57:32 +0200814
Harald Welte6982fab2019-05-16 23:01:35 +0200815/* Section 5.3.1 ABORT */
816static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req)
817{
818 uint16_t w_value = osmo_load16le(&req->wValue);
819 uint8_t slot_nr = w_value & 0xff;
820 uint8_t seq = w_value >> 8;
821 struct ccid_slot *cs;
822
823 if (slot_nr >= ARRAY_SIZE(ci->slot))
824 return CCID_CTRL_RET_INVALID;
825
826 cs = &ci->slot[slot_nr];
827
828 LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n");
829 /* Upon receiving the Control pipe ABORT request the CCID should check
830 * the state of the requested slot. */
831
832 /* If the last Bulk-OUT message received by the CCID was a
833 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
834 * request, then the CCID will respond to the Bulk-OUT message with
835 * the RDR_to_PC_SlotStatus response. */
836
837 /* FIXME */
838
839 /* If the previous Bulk-OUT message received by the CCID was not a
840 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
841 * request, then the CCID will fail all Bulk-Out commands to that slot
842 * until the PC_to_RDR_Abort command with the same bSlot and bSeq is
843 * received. Bulk-OUT commands will be failed by sending a response
844 * with bmCommandStatus=Failed and bError=CMD_ABORTED. */
845
846 /* FIXME */
847 return CCID_CTRL_RET_OK;
848}
849
850/* Section 5.3.2 GET_CLOCK_FREQUENCIES */
851static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
852 const uint8_t **data_in)
853{
854 uint16_t len = osmo_load16le(&req->wLength);
855
856 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
857 return CCID_CTRL_RET_INVALID;
858
859 *data_in = (const uint8_t *) ci->clock_freqs;
860 return CCID_CTRL_RET_OK;
861}
862
863/* Section 5.3.3 GET_DATA_RATES */
864static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
865 const uint8_t **data_in)
866{
867 uint16_t len = osmo_load16le(&req->wLength);
868
869 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
870 return CCID_CTRL_RET_INVALID;
871
872 *data_in = (const uint8_t *) ci->data_rates;
873 return CCID_CTRL_RET_OK;
874}
875
876/*! Handle [class specific] CTRL request. We assume the caller has already verified that the
877 * request was made to the correct interface as well as it is a class-specific request.
878 * \param[in] ci CCID Instance for which CTRL request was received
879 * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header
880 * \param[out] data_in data to be returned to the host in the IN transaction (if any)
881 * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN
882 */
883int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in)
884{
885 const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req;
886 int rc;
887
888 LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n",
889 req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest),
890 req->wValue, req->wIndex, req->wLength);
891
892 switch (req->bRequest) {
893 case CLASS_SPEC_CCID_ABORT:
894 rc = ccid_handle_ctrl_abort(ci, req);
895 break;
896 case CLASS_SPEC_CCID_GET_CLOCK_FREQ:
897 rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in);
898 break;
899 case CLASS_SPEC_CCID_GET_DATA_RATES:
900 rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in);
901 break;
902 default:
903 return CCID_CTRL_RET_UNKNOWN;
904 }
905 return rc;
906}
907
Harald Weltecab5d152019-05-16 13:31:16 +0200908void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
Harald Welte6982fab2019-05-16 23:01:35 +0200909 const struct ccid_slot_ops *slot_ops,
910 const struct usb_ccid_class_descriptor *class_desc,
911 const uint32_t *data_rates, const uint32_t *clock_freqs,
912 const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200913{
914 int i;
915
Harald Weltecab5d152019-05-16 13:31:16 +0200916 ci->ops = ops;
917 ci->slot_ops = slot_ops;
Harald Welte6982fab2019-05-16 23:01:35 +0200918 ci->class_desc = class_desc;
919 ci->clock_freqs = clock_freqs;
920 ci->data_rates = data_rates;
Harald Weltebcbc1972019-05-15 21:57:32 +0200921 ci->name = name;
922 ci->priv = priv;
Harald Welted3385072019-05-16 17:38:23 +0200923
924 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
925 struct ccid_slot *cs = &ci->slot[i];
926 cs->slot_nr = i;
927 cs->ci = ci;
928
929 slot_ops->init(cs);
930 }
931
Harald Weltebcbc1972019-05-15 21:57:32 +0200932}