blob: 11edd3eb89fb1ebc63ac96e5674395e7f8c98a34 [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;
Harald Welte8772f582019-05-14 16:34:47 +0200478
Harald Weltecab5d152019-05-16 13:31:16 +0200479 /* copy default parameters from somewhere */
Harald Welte8579ce52019-05-15 12:22:24 +0200480 /* FIXME: T=1 */
Eric Wildad1edce2019-11-27 16:51:08 +0100481 cs->ci->slot_ops->set_params(cs, seq, CCID_PROTOCOL_NUM_T0, cs->default_pars);
Harald Weltecab5d152019-05-16 13:31:16 +0200482 cs->pars = *cs->default_pars;
483
Harald Welte8579ce52019-05-15 12:22:24 +0200484 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200485 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100486}
487
488/* Section 6.1.7 */
489static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
490{
491 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200492 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200493 const struct ccid_header *ch = (const struct ccid_header *) u;
494 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200495 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200496 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200497 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200498
Harald Welte8579ce52019-05-15 12:22:24 +0200499 switch (spar->bProtocolNum) {
500 case CCID_PROTOCOL_NUM_T0:
501 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200502 break;
503 case CCID_PROTOCOL_NUM_T1:
504 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200505 break;
506 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200507 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200508 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200509 goto out;
510 }
511
512 if (rc < 0) {
513 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
514 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
515 goto out;
516 }
517
Eric Wildad1edce2019-11-27 16:51:08 +0100518 cs->proposed_pars = pars_dec;
519
Harald Weltecab5d152019-05-16 13:31:16 +0200520 /* validate parameters; abort if they are not supported */
Eric Wildad1edce2019-11-27 16:51:08 +0100521 rc = cs->ci->slot_ops->set_params(cs, seq, spar->bProtocolNum, &pars_dec);
Harald Weltecab5d152019-05-16 13:31:16 +0200522 if (rc < 0) {
523 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
Eric Wildad1edce2019-11-27 16:51:08 +0100524 goto out;
Harald Welte8579ce52019-05-15 12:22:24 +0200525 }
Eric Wildad1edce2019-11-27 16:51:08 +0100526 /* busy, tdpu like callback */
527 return 1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200528out:
Harald Welte8772f582019-05-14 16:34:47 +0200529 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100530}
531
532/* Section 6.1.8 */
533static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
534{
535 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200536 const struct ccid_header *ch = (const struct ccid_header *) u;
537 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200538 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200539
540 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
541 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100542}
543
544/* Section 6.1.9 */
545static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
546{
547 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200548 const struct ccid_header *ch = (const struct ccid_header *) u;
549 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200550 struct msgb *resp;
551
Harald Weltecab5d152019-05-16 13:31:16 +0200552 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200553 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
554 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100555}
556
557/* Section 6.1.10 */
558static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
559{
560 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200561 const struct ccid_header *ch = (const struct ccid_header *) u;
562 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200563 struct msgb *resp;
564
Harald Weltecab5d152019-05-16 13:31:16 +0200565 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200566 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
567 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
568 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100569}
570
571/* Section 6.1.11 */
572static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
573{
574 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200575 const struct ccid_header *ch = (const struct ccid_header *) u;
576 uint8_t seq = u->secure.hdr.bSeq;
577 struct msgb *resp;
578
579 /* FIXME */
580 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
581 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100582}
583
584/* Section 6.1.12 */
585static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
586{
587 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200588 const struct ccid_header *ch = (const struct ccid_header *) u;
589 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200590 struct msgb *resp;
591
Harald Welte8772f582019-05-14 16:34:47 +0200592 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
593 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100594}
595
596/* Section 6.1.13 */
597static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
598{
599 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200600 const struct ccid_header *ch = (const struct ccid_header *) u;
601 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200602 struct msgb *resp;
603
Harald Welte8772f582019-05-14 16:34:47 +0200604 /* Check if the currently in-progress message is Abortable */
605 switch (0/* FIXME */) {
606 case PC_to_RDR_IccPowerOn:
607 case PC_to_RDR_XfrBlock:
608 case PC_to_RDR_Escape:
609 case PC_to_RDR_Secure:
610 case PC_to_RDR_Mechanical:
611 //case PC_to_RDR_Abort: /* seriously? WTF! */
612 break;
613 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200614 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200615 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
616 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
617 return ccid_slot_send_unbusy(cs, resp);
618 }
619
620 /* FIXME */
621 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
622 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100623}
624
625/* Section 6.1.14 */
626static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
627{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200628 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200629 const struct ccid_header *ch = (const struct ccid_header *) u;
630 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200631 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
632 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200633 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200634 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200635
Harald Weltecab5d152019-05-16 13:31:16 +0200636 /* FIXME: which rate to return in failure case? */
637 rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps);
638 if (rc < 0)
639 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
640 else
641 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200642 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100643}
644
Harald Welte8772f582019-05-14 16:34:47 +0200645/*! Handle data arriving from the host on the OUT endpoint.
646 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200647 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
648 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
649 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200650 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100651int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
652{
653 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
654 const struct ccid_header *ch = (const struct ccid_header *) u;
655 unsigned int len = msgb_length(msg);
656 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200657 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200658 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100659
660 if (len < sizeof(*ch)) {
661 /* FIXME */
Harald Welte1b3fb632019-11-12 06:45:48 +0100662 msgb_free(msg);
Harald Welte63653742019-01-03 16:54:16 +0100663 return -1;
664 }
665
Harald Welte8772f582019-05-14 16:34:47 +0200666 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200667 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100668 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200669 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200670 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
Harald Welte1b3fb632019-11-12 06:45:48 +0100671 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200672 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100673 }
674
Harald Welte8772f582019-05-14 16:34:47 +0200675 /* Check if slot is already busy; Reject any additional commands meanwhile */
676 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200677 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200678 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
679 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
680 CCID_ERR_CMD_SLOT_BUSY);
Harald Welte1b3fb632019-11-12 06:45:48 +0100681 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200682 return ccid_send(ci, resp);
683 }
684
Harald Weltee73a1df2019-05-15 22:27:02 +0200685 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
686 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
687
Harald Welte8772f582019-05-14 16:34:47 +0200688 /* we're now processing a command for the slot; mark slot as busy */
689 cs->cmd_busy = true;
690
691 /* TODO: enqueue into the per-slot specific input queue */
692
Harald Weltecab5d152019-05-16 13:31:16 +0200693 /* call pre-processing call-back function; allows reader to update state */
694 if (ci->slot_ops->pre_proc_cb)
695 ci->slot_ops->pre_proc_cb(cs, msg);
696
Harald Welte63653742019-01-03 16:54:16 +0100697 switch (ch->bMessageType) {
698 case PC_to_RDR_GetSlotStatus:
Eric Wild33403af2019-10-01 15:01:58 +0200699 if (len < sizeof(u->get_slot_status))
Harald Welte63653742019-01-03 16:54:16 +0100700 goto short_msg;
701 rc = ccid_handle_get_slot_status(cs, msg);
702 break;
703 case PC_to_RDR_IccPowerOn:
704 if (len != sizeof(u->icc_power_on))
705 goto short_msg;
706 rc = ccid_handle_icc_power_on(cs, msg);
707 break;
708 case PC_to_RDR_IccPowerOff:
709 if (len != sizeof(u->icc_power_off))
710 goto short_msg;
711 rc = ccid_handle_icc_power_off(cs, msg);
712 break;
713 case PC_to_RDR_XfrBlock:
714 if (len < sizeof(u->xfr_block))
715 goto short_msg;
716 rc = ccid_handle_xfr_block(cs, msg);
717 break;
718 case PC_to_RDR_GetParameters:
719 if (len != sizeof(u->get_parameters))
720 goto short_msg;
721 rc = ccid_handle_get_parameters(cs, msg);
722 break;
723 case PC_to_RDR_ResetParameters:
724 if (len != sizeof(u->reset_parameters))
725 goto short_msg;
726 rc = ccid_handle_reset_parameters(cs, msg);
727 break;
728 case PC_to_RDR_SetParameters:
Eric Wild33403af2019-10-01 15:01:58 +0200729 // smallest union member
730 if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10))
Harald Welte63653742019-01-03 16:54:16 +0100731 goto short_msg;
732 rc = ccid_handle_set_parameters(cs, msg);
733 break;
734 case PC_to_RDR_Escape:
735 if (len < sizeof(u->escape))
736 goto short_msg;
737 rc = ccid_handle_escape(cs, msg);
738 break;
739 case PC_to_RDR_IccClock:
740 if (len != sizeof(u->icc_clock))
741 goto short_msg;
742 rc = ccid_handle_icc_clock(cs, msg);
743 break;
744 case PC_to_RDR_T0APDU:
745 if (len != /*FIXME*/ sizeof(u->t0apdu))
746 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200747 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100748 break;
749 case PC_to_RDR_Secure:
750 if (len < sizeof(u->secure))
751 goto short_msg;
752 rc = ccid_handle_secure(cs, msg);
753 break;
754 case PC_to_RDR_Mechanical:
755 if (len != sizeof(u->mechanical))
756 goto short_msg;
757 rc = ccid_handle_mechanical(cs, msg);
758 break;
759 case PC_to_RDR_Abort:
760 if (len != sizeof(u->abort))
761 goto short_msg;
762 rc = ccid_handle_abort(cs, msg);
763 break;
764 case PC_to_RDR_SetDataRateAndClockFrequency:
765 if (len != sizeof(u->set_rate_and_clock))
766 goto short_msg;
767 rc = ccid_handle_set_rate_and_clock(cs, msg);
768 break;
769 default:
Harald Welte8772f582019-05-14 16:34:47 +0200770 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200771 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200772 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
773 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200774 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200775 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100776 }
Harald Welte922ff932019-05-16 11:21:51 +0200777 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
778 * of the msgb */
779 if (rc != 1)
780 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200781 return 0;
782
Harald Welte63653742019-01-03 16:54:16 +0100783short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200784 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200785 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200786 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100787}
Harald Weltebcbc1972019-05-15 21:57:32 +0200788
Harald Welte6982fab2019-05-16 23:01:35 +0200789/* Section 5.3.1 ABORT */
790static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req)
791{
792 uint16_t w_value = osmo_load16le(&req->wValue);
793 uint8_t slot_nr = w_value & 0xff;
794 uint8_t seq = w_value >> 8;
795 struct ccid_slot *cs;
796
797 if (slot_nr >= ARRAY_SIZE(ci->slot))
798 return CCID_CTRL_RET_INVALID;
799
800 cs = &ci->slot[slot_nr];
801
802 LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n");
803 /* Upon receiving the Control pipe ABORT request the CCID should check
804 * the state of the requested slot. */
805
806 /* If the last Bulk-OUT message received by the CCID was a
807 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
808 * request, then the CCID will respond to the Bulk-OUT message with
809 * the RDR_to_PC_SlotStatus response. */
810
811 /* FIXME */
812
813 /* If the previous Bulk-OUT message received by the CCID was not a
814 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
815 * request, then the CCID will fail all Bulk-Out commands to that slot
816 * until the PC_to_RDR_Abort command with the same bSlot and bSeq is
817 * received. Bulk-OUT commands will be failed by sending a response
818 * with bmCommandStatus=Failed and bError=CMD_ABORTED. */
819
820 /* FIXME */
821 return CCID_CTRL_RET_OK;
822}
823
824/* Section 5.3.2 GET_CLOCK_FREQUENCIES */
825static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
826 const uint8_t **data_in)
827{
828 uint16_t len = osmo_load16le(&req->wLength);
829
830 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
831 return CCID_CTRL_RET_INVALID;
832
833 *data_in = (const uint8_t *) ci->clock_freqs;
834 return CCID_CTRL_RET_OK;
835}
836
837/* Section 5.3.3 GET_DATA_RATES */
838static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
839 const uint8_t **data_in)
840{
841 uint16_t len = osmo_load16le(&req->wLength);
842
843 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
844 return CCID_CTRL_RET_INVALID;
845
846 *data_in = (const uint8_t *) ci->data_rates;
847 return CCID_CTRL_RET_OK;
848}
849
850/*! Handle [class specific] CTRL request. We assume the caller has already verified that the
851 * request was made to the correct interface as well as it is a class-specific request.
852 * \param[in] ci CCID Instance for which CTRL request was received
853 * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header
854 * \param[out] data_in data to be returned to the host in the IN transaction (if any)
855 * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN
856 */
857int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in)
858{
859 const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req;
860 int rc;
861
862 LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n",
863 req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest),
864 req->wValue, req->wIndex, req->wLength);
865
866 switch (req->bRequest) {
867 case CLASS_SPEC_CCID_ABORT:
868 rc = ccid_handle_ctrl_abort(ci, req);
869 break;
870 case CLASS_SPEC_CCID_GET_CLOCK_FREQ:
871 rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in);
872 break;
873 case CLASS_SPEC_CCID_GET_DATA_RATES:
874 rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in);
875 break;
876 default:
877 return CCID_CTRL_RET_UNKNOWN;
878 }
879 return rc;
880}
881
Harald Weltecab5d152019-05-16 13:31:16 +0200882void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
Harald Welte6982fab2019-05-16 23:01:35 +0200883 const struct ccid_slot_ops *slot_ops,
884 const struct usb_ccid_class_descriptor *class_desc,
885 const uint32_t *data_rates, const uint32_t *clock_freqs,
886 const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200887{
888 int i;
889
Harald Weltecab5d152019-05-16 13:31:16 +0200890 ci->ops = ops;
891 ci->slot_ops = slot_ops;
Harald Welte6982fab2019-05-16 23:01:35 +0200892 ci->class_desc = class_desc;
893 ci->clock_freqs = clock_freqs;
894 ci->data_rates = data_rates;
Harald Weltebcbc1972019-05-15 21:57:32 +0200895 ci->name = name;
896 ci->priv = priv;
Harald Welted3385072019-05-16 17:38:23 +0200897
898 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
899 struct ccid_slot *cs = &ci->slot[i];
900 cs->slot_nr = i;
901 cs->ci = ci;
902
903 slot_ops->init(cs);
904 }
905
Harald Weltebcbc1972019-05-15 21:57:32 +0200906}