blob: b14f194fb6585a0bb39c391619f0c25e47cb30c8 [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 Wild91552312019-11-18 13:57:11 +0100483 /* validate parameters; abort if they are not supported */
484 rc = cs->ci->slot_ops->set_params(cs, seq, CCID_PROTOCOL_NUM_T0, cs->default_pars);
485 if (rc < 0) {
486 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
487 goto out;
488 }
489
490 msgb_free(msg);
491 /* busy, tdpu like callback */
492 return 1;
493out:
494 msgb_free(msg);
495 ccid_slot_send_unbusy(cs, resp);
496 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100497}
498
499/* Section 6.1.7 */
500static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
501{
502 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200503 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200504 const struct ccid_header *ch = (const struct ccid_header *) u;
505 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200506 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200507 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200508 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200509
Harald Welte8579ce52019-05-15 12:22:24 +0200510 switch (spar->bProtocolNum) {
511 case CCID_PROTOCOL_NUM_T0:
512 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200513 break;
514 case CCID_PROTOCOL_NUM_T1:
515 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200516 break;
517 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200518 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200519 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200520 goto out;
521 }
522
523 if (rc < 0) {
524 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
525 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
526 goto out;
527 }
528
Eric Wildad1edce2019-11-27 16:51:08 +0100529 cs->proposed_pars = pars_dec;
530
Harald Weltecab5d152019-05-16 13:31:16 +0200531 /* validate parameters; abort if they are not supported */
Eric Wildad1edce2019-11-27 16:51:08 +0100532 rc = cs->ci->slot_ops->set_params(cs, seq, spar->bProtocolNum, &pars_dec);
Harald Weltecab5d152019-05-16 13:31:16 +0200533 if (rc < 0) {
534 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
Eric Wildad1edce2019-11-27 16:51:08 +0100535 goto out;
Harald Welte8579ce52019-05-15 12:22:24 +0200536 }
Eric Wild91552312019-11-18 13:57:11 +0100537
538 msgb_free(msg);
Eric Wildad1edce2019-11-27 16:51:08 +0100539 /* busy, tdpu like callback */
540 return 1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200541out:
Eric Wild91552312019-11-18 13:57:11 +0100542 msgb_free(msg);
543 ccid_slot_send_unbusy(cs, resp);
544 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100545}
546
547/* Section 6.1.8 */
548static int ccid_handle_escape(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->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200553 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200554
555 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
556 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100557}
558
559/* Section 6.1.9 */
560static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
561{
562 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200563 const struct ccid_header *ch = (const struct ccid_header *) u;
564 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200565 struct msgb *resp;
566
Harald Weltecab5d152019-05-16 13:31:16 +0200567 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200568 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
569 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100570}
571
572/* Section 6.1.10 */
573static int ccid_handle_t0apdu(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->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200578 struct msgb *resp;
579
Harald Weltecab5d152019-05-16 13:31:16 +0200580 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200581 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
582 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
583 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100584}
585
586/* Section 6.1.11 */
587static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
588{
589 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200590 const struct ccid_header *ch = (const struct ccid_header *) u;
591 uint8_t seq = u->secure.hdr.bSeq;
592 struct msgb *resp;
593
594 /* FIXME */
595 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
596 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100597}
598
599/* Section 6.1.12 */
600static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
601{
602 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200603 const struct ccid_header *ch = (const struct ccid_header *) u;
604 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200605 struct msgb *resp;
606
Harald Welte8772f582019-05-14 16:34:47 +0200607 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
608 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100609}
610
611/* Section 6.1.13 */
612static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
613{
614 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200615 const struct ccid_header *ch = (const struct ccid_header *) u;
616 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200617 struct msgb *resp;
618
Harald Welte8772f582019-05-14 16:34:47 +0200619 /* Check if the currently in-progress message is Abortable */
620 switch (0/* FIXME */) {
621 case PC_to_RDR_IccPowerOn:
622 case PC_to_RDR_XfrBlock:
623 case PC_to_RDR_Escape:
624 case PC_to_RDR_Secure:
625 case PC_to_RDR_Mechanical:
626 //case PC_to_RDR_Abort: /* seriously? WTF! */
627 break;
628 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200629 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200630 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
631 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
632 return ccid_slot_send_unbusy(cs, resp);
633 }
634
635 /* FIXME */
636 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
637 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100638}
639
640/* Section 6.1.14 */
641static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
642{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200643 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200644 const struct ccid_header *ch = (const struct ccid_header *) u;
645 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200646 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
647 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200648 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200649 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200650
Harald Weltecab5d152019-05-16 13:31:16 +0200651 /* FIXME: which rate to return in failure case? */
652 rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps);
653 if (rc < 0)
654 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
655 else
656 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200657 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100658}
659
Harald Welte8772f582019-05-14 16:34:47 +0200660/*! Handle data arriving from the host on the OUT endpoint.
661 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200662 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
663 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
664 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200665 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100666int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
667{
668 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
669 const struct ccid_header *ch = (const struct ccid_header *) u;
670 unsigned int len = msgb_length(msg);
671 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200672 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200673 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100674
675 if (len < sizeof(*ch)) {
676 /* FIXME */
Harald Welte1b3fb632019-11-12 06:45:48 +0100677 msgb_free(msg);
Harald Welte63653742019-01-03 16:54:16 +0100678 return -1;
679 }
680
Harald Welte8772f582019-05-14 16:34:47 +0200681 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200682 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100683 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200684 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200685 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
Harald Welte1b3fb632019-11-12 06:45:48 +0100686 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200687 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100688 }
689
Harald Welte8772f582019-05-14 16:34:47 +0200690 /* Check if slot is already busy; Reject any additional commands meanwhile */
691 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200692 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200693 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
694 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
695 CCID_ERR_CMD_SLOT_BUSY);
Harald Welte1b3fb632019-11-12 06:45:48 +0100696 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200697 return ccid_send(ci, resp);
698 }
699
Eric Wild91552312019-11-18 13:57:11 +0100700 if(!cs->icc_present) {
701 LOGPCS(cs, LOGL_ERROR, "No icc present, but another cmd received\n");
702 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
703 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
704 CCID_ERR_ICC_MUTE);
705 msgb_free(msg);
706 return ccid_send(ci, resp);
707 }
708
Harald Weltee73a1df2019-05-15 22:27:02 +0200709 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
710 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
711
Harald Welte8772f582019-05-14 16:34:47 +0200712 /* we're now processing a command for the slot; mark slot as busy */
713 cs->cmd_busy = true;
714
715 /* TODO: enqueue into the per-slot specific input queue */
716
Harald Weltecab5d152019-05-16 13:31:16 +0200717 /* call pre-processing call-back function; allows reader to update state */
718 if (ci->slot_ops->pre_proc_cb)
719 ci->slot_ops->pre_proc_cb(cs, msg);
720
Harald Welte63653742019-01-03 16:54:16 +0100721 switch (ch->bMessageType) {
722 case PC_to_RDR_GetSlotStatus:
Eric Wild33403af2019-10-01 15:01:58 +0200723 if (len < sizeof(u->get_slot_status))
Harald Welte63653742019-01-03 16:54:16 +0100724 goto short_msg;
725 rc = ccid_handle_get_slot_status(cs, msg);
726 break;
727 case PC_to_RDR_IccPowerOn:
728 if (len != sizeof(u->icc_power_on))
729 goto short_msg;
730 rc = ccid_handle_icc_power_on(cs, msg);
731 break;
732 case PC_to_RDR_IccPowerOff:
733 if (len != sizeof(u->icc_power_off))
734 goto short_msg;
735 rc = ccid_handle_icc_power_off(cs, msg);
736 break;
737 case PC_to_RDR_XfrBlock:
738 if (len < sizeof(u->xfr_block))
739 goto short_msg;
740 rc = ccid_handle_xfr_block(cs, msg);
741 break;
742 case PC_to_RDR_GetParameters:
743 if (len != sizeof(u->get_parameters))
744 goto short_msg;
745 rc = ccid_handle_get_parameters(cs, msg);
746 break;
747 case PC_to_RDR_ResetParameters:
748 if (len != sizeof(u->reset_parameters))
749 goto short_msg;
750 rc = ccid_handle_reset_parameters(cs, msg);
751 break;
752 case PC_to_RDR_SetParameters:
Eric Wild33403af2019-10-01 15:01:58 +0200753 // smallest union member
754 if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10))
Harald Welte63653742019-01-03 16:54:16 +0100755 goto short_msg;
756 rc = ccid_handle_set_parameters(cs, msg);
757 break;
758 case PC_to_RDR_Escape:
759 if (len < sizeof(u->escape))
760 goto short_msg;
761 rc = ccid_handle_escape(cs, msg);
762 break;
763 case PC_to_RDR_IccClock:
764 if (len != sizeof(u->icc_clock))
765 goto short_msg;
766 rc = ccid_handle_icc_clock(cs, msg);
767 break;
768 case PC_to_RDR_T0APDU:
769 if (len != /*FIXME*/ sizeof(u->t0apdu))
770 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200771 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100772 break;
773 case PC_to_RDR_Secure:
774 if (len < sizeof(u->secure))
775 goto short_msg;
776 rc = ccid_handle_secure(cs, msg);
777 break;
778 case PC_to_RDR_Mechanical:
779 if (len != sizeof(u->mechanical))
780 goto short_msg;
781 rc = ccid_handle_mechanical(cs, msg);
782 break;
783 case PC_to_RDR_Abort:
784 if (len != sizeof(u->abort))
785 goto short_msg;
786 rc = ccid_handle_abort(cs, msg);
787 break;
788 case PC_to_RDR_SetDataRateAndClockFrequency:
789 if (len != sizeof(u->set_rate_and_clock))
790 goto short_msg;
791 rc = ccid_handle_set_rate_and_clock(cs, msg);
792 break;
793 default:
Harald Welte8772f582019-05-14 16:34:47 +0200794 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200795 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200796 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
797 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200798 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200799 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100800 }
Harald Welte922ff932019-05-16 11:21:51 +0200801 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
802 * of the msgb */
803 if (rc != 1)
804 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200805 return 0;
806
Harald Welte63653742019-01-03 16:54:16 +0100807short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200808 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200809 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200810 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100811}
Harald Weltebcbc1972019-05-15 21:57:32 +0200812
Harald Welte6982fab2019-05-16 23:01:35 +0200813/* Section 5.3.1 ABORT */
814static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req)
815{
816 uint16_t w_value = osmo_load16le(&req->wValue);
817 uint8_t slot_nr = w_value & 0xff;
818 uint8_t seq = w_value >> 8;
819 struct ccid_slot *cs;
820
821 if (slot_nr >= ARRAY_SIZE(ci->slot))
822 return CCID_CTRL_RET_INVALID;
823
824 cs = &ci->slot[slot_nr];
825
826 LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n");
827 /* Upon receiving the Control pipe ABORT request the CCID should check
828 * the state of the requested slot. */
829
830 /* If the last Bulk-OUT message received by the CCID was a
831 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
832 * request, then the CCID will respond to the Bulk-OUT message with
833 * the RDR_to_PC_SlotStatus response. */
834
835 /* FIXME */
836
837 /* If the previous Bulk-OUT message received by the CCID was not a
838 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
839 * request, then the CCID will fail all Bulk-Out commands to that slot
840 * until the PC_to_RDR_Abort command with the same bSlot and bSeq is
841 * received. Bulk-OUT commands will be failed by sending a response
842 * with bmCommandStatus=Failed and bError=CMD_ABORTED. */
843
844 /* FIXME */
845 return CCID_CTRL_RET_OK;
846}
847
848/* Section 5.3.2 GET_CLOCK_FREQUENCIES */
849static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
850 const uint8_t **data_in)
851{
852 uint16_t len = osmo_load16le(&req->wLength);
853
854 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
855 return CCID_CTRL_RET_INVALID;
856
857 *data_in = (const uint8_t *) ci->clock_freqs;
858 return CCID_CTRL_RET_OK;
859}
860
861/* Section 5.3.3 GET_DATA_RATES */
862static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
863 const uint8_t **data_in)
864{
865 uint16_t len = osmo_load16le(&req->wLength);
866
867 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
868 return CCID_CTRL_RET_INVALID;
869
870 *data_in = (const uint8_t *) ci->data_rates;
871 return CCID_CTRL_RET_OK;
872}
873
874/*! Handle [class specific] CTRL request. We assume the caller has already verified that the
875 * request was made to the correct interface as well as it is a class-specific request.
876 * \param[in] ci CCID Instance for which CTRL request was received
877 * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header
878 * \param[out] data_in data to be returned to the host in the IN transaction (if any)
879 * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN
880 */
881int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in)
882{
883 const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req;
884 int rc;
885
886 LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n",
887 req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest),
888 req->wValue, req->wIndex, req->wLength);
889
890 switch (req->bRequest) {
891 case CLASS_SPEC_CCID_ABORT:
892 rc = ccid_handle_ctrl_abort(ci, req);
893 break;
894 case CLASS_SPEC_CCID_GET_CLOCK_FREQ:
895 rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in);
896 break;
897 case CLASS_SPEC_CCID_GET_DATA_RATES:
898 rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in);
899 break;
900 default:
901 return CCID_CTRL_RET_UNKNOWN;
902 }
903 return rc;
904}
905
Harald Weltecab5d152019-05-16 13:31:16 +0200906void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
Harald Welte6982fab2019-05-16 23:01:35 +0200907 const struct ccid_slot_ops *slot_ops,
908 const struct usb_ccid_class_descriptor *class_desc,
909 const uint32_t *data_rates, const uint32_t *clock_freqs,
910 const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200911{
912 int i;
913
Harald Weltecab5d152019-05-16 13:31:16 +0200914 ci->ops = ops;
915 ci->slot_ops = slot_ops;
Harald Welte6982fab2019-05-16 23:01:35 +0200916 ci->class_desc = class_desc;
917 ci->clock_freqs = clock_freqs;
918 ci->data_rates = data_rates;
Harald Weltebcbc1972019-05-15 21:57:32 +0200919 ci->name = name;
920 ci->priv = priv;
Harald Welted3385072019-05-16 17:38:23 +0200921
922 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
923 struct ccid_slot *cs = &ci->slot[i];
924 cs->slot_nr = i;
925 cs->ci = ci;
926
927 slot_ops->init(cs);
928 }
929
Harald Weltebcbc1972019-05-15 21:57:32 +0200930}