blob: c67c52e1fef35ad0cb3f031f0f6bdb0908426596 [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;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200441
Harald Welte505d4412019-05-16 17:26:09 +0200442 /* handle this asynchronously */
443 cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block);
444 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100445}
446
447/* Section 6.1.5 */
448static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
449{
450 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200451 const struct ccid_header *ch = (const struct ccid_header *) u;
452 uint8_t seq = u->get_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200453 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200454
Harald Welte8579ce52019-05-15 12:22:24 +0200455 /* FIXME: T=1 */
456 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200457 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100458}
459
460/* Section 6.1.6 */
461static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg)
462{
463 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200464 const struct ccid_header *ch = (const struct ccid_header *) u;
465 uint8_t seq = u->reset_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200466 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200467
Harald Weltecab5d152019-05-16 13:31:16 +0200468 /* copy default parameters from somewhere */
Harald Welte8579ce52019-05-15 12:22:24 +0200469 /* FIXME: T=1 */
Eric Wildad1edce2019-11-27 16:51:08 +0100470 cs->ci->slot_ops->set_params(cs, seq, CCID_PROTOCOL_NUM_T0, cs->default_pars);
Harald Weltecab5d152019-05-16 13:31:16 +0200471 cs->pars = *cs->default_pars;
472
Harald Welte8579ce52019-05-15 12:22:24 +0200473 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200474 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100475}
476
477/* Section 6.1.7 */
478static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
479{
480 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200481 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200482 const struct ccid_header *ch = (const struct ccid_header *) u;
483 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200484 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200485 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200486 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200487
Harald Welte8579ce52019-05-15 12:22:24 +0200488 switch (spar->bProtocolNum) {
489 case CCID_PROTOCOL_NUM_T0:
490 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200491 break;
492 case CCID_PROTOCOL_NUM_T1:
493 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200494 break;
495 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200496 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200497 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200498 goto out;
499 }
500
501 if (rc < 0) {
502 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
503 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
504 goto out;
505 }
506
Eric Wildad1edce2019-11-27 16:51:08 +0100507 cs->proposed_pars = pars_dec;
508
Harald Weltecab5d152019-05-16 13:31:16 +0200509 /* validate parameters; abort if they are not supported */
Eric Wildad1edce2019-11-27 16:51:08 +0100510 rc = cs->ci->slot_ops->set_params(cs, seq, spar->bProtocolNum, &pars_dec);
Harald Weltecab5d152019-05-16 13:31:16 +0200511 if (rc < 0) {
512 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
Eric Wildad1edce2019-11-27 16:51:08 +0100513 goto out;
Harald Welte8579ce52019-05-15 12:22:24 +0200514 }
Eric Wildad1edce2019-11-27 16:51:08 +0100515 /* busy, tdpu like callback */
516 return 1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200517out:
Harald Welte8772f582019-05-14 16:34:47 +0200518 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100519}
520
521/* Section 6.1.8 */
522static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
523{
524 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200525 const struct ccid_header *ch = (const struct ccid_header *) u;
526 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200527 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200528
529 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
530 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100531}
532
533/* Section 6.1.9 */
534static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
535{
536 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200537 const struct ccid_header *ch = (const struct ccid_header *) u;
538 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200539 struct msgb *resp;
540
Harald Weltecab5d152019-05-16 13:31:16 +0200541 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200542 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
543 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100544}
545
546/* Section 6.1.10 */
547static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
548{
549 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200550 const struct ccid_header *ch = (const struct ccid_header *) u;
551 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200552 struct msgb *resp;
553
Harald Weltecab5d152019-05-16 13:31:16 +0200554 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200555 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
556 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
557 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100558}
559
560/* Section 6.1.11 */
561static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
562{
563 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200564 const struct ccid_header *ch = (const struct ccid_header *) u;
565 uint8_t seq = u->secure.hdr.bSeq;
566 struct msgb *resp;
567
568 /* FIXME */
569 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
570 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100571}
572
573/* Section 6.1.12 */
574static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
575{
576 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200577 const struct ccid_header *ch = (const struct ccid_header *) u;
578 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200579 struct msgb *resp;
580
Harald Welte8772f582019-05-14 16:34:47 +0200581 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
582 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100583}
584
585/* Section 6.1.13 */
586static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
587{
588 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200589 const struct ccid_header *ch = (const struct ccid_header *) u;
590 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200591 struct msgb *resp;
592
Harald Welte8772f582019-05-14 16:34:47 +0200593 /* Check if the currently in-progress message is Abortable */
594 switch (0/* FIXME */) {
595 case PC_to_RDR_IccPowerOn:
596 case PC_to_RDR_XfrBlock:
597 case PC_to_RDR_Escape:
598 case PC_to_RDR_Secure:
599 case PC_to_RDR_Mechanical:
600 //case PC_to_RDR_Abort: /* seriously? WTF! */
601 break;
602 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200603 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200604 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
605 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
606 return ccid_slot_send_unbusy(cs, resp);
607 }
608
609 /* FIXME */
610 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
611 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100612}
613
614/* Section 6.1.14 */
615static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
616{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200617 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200618 const struct ccid_header *ch = (const struct ccid_header *) u;
619 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200620 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
621 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200622 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200623 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200624
Harald Weltecab5d152019-05-16 13:31:16 +0200625 /* FIXME: which rate to return in failure case? */
626 rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps);
627 if (rc < 0)
628 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
629 else
630 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200631 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100632}
633
Harald Welte8772f582019-05-14 16:34:47 +0200634/*! Handle data arriving from the host on the OUT endpoint.
635 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200636 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
637 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
638 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200639 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100640int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
641{
642 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
643 const struct ccid_header *ch = (const struct ccid_header *) u;
644 unsigned int len = msgb_length(msg);
645 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200646 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200647 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100648
649 if (len < sizeof(*ch)) {
650 /* FIXME */
Harald Welte1b3fb632019-11-12 06:45:48 +0100651 msgb_free(msg);
Harald Welte63653742019-01-03 16:54:16 +0100652 return -1;
653 }
654
Harald Welte8772f582019-05-14 16:34:47 +0200655 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200656 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100657 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200658 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200659 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
Harald Welte1b3fb632019-11-12 06:45:48 +0100660 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200661 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100662 }
663
Harald Welte8772f582019-05-14 16:34:47 +0200664 /* Check if slot is already busy; Reject any additional commands meanwhile */
665 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200666 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200667 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
668 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
669 CCID_ERR_CMD_SLOT_BUSY);
Harald Welte1b3fb632019-11-12 06:45:48 +0100670 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200671 return ccid_send(ci, resp);
672 }
673
Harald Weltee73a1df2019-05-15 22:27:02 +0200674 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
675 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
676
Harald Welte8772f582019-05-14 16:34:47 +0200677 /* we're now processing a command for the slot; mark slot as busy */
678 cs->cmd_busy = true;
679
680 /* TODO: enqueue into the per-slot specific input queue */
681
Harald Weltecab5d152019-05-16 13:31:16 +0200682 /* call pre-processing call-back function; allows reader to update state */
683 if (ci->slot_ops->pre_proc_cb)
684 ci->slot_ops->pre_proc_cb(cs, msg);
685
Harald Welte63653742019-01-03 16:54:16 +0100686 switch (ch->bMessageType) {
687 case PC_to_RDR_GetSlotStatus:
Eric Wild33403af2019-10-01 15:01:58 +0200688 if (len < sizeof(u->get_slot_status))
Harald Welte63653742019-01-03 16:54:16 +0100689 goto short_msg;
690 rc = ccid_handle_get_slot_status(cs, msg);
691 break;
692 case PC_to_RDR_IccPowerOn:
693 if (len != sizeof(u->icc_power_on))
694 goto short_msg;
695 rc = ccid_handle_icc_power_on(cs, msg);
696 break;
697 case PC_to_RDR_IccPowerOff:
698 if (len != sizeof(u->icc_power_off))
699 goto short_msg;
700 rc = ccid_handle_icc_power_off(cs, msg);
701 break;
702 case PC_to_RDR_XfrBlock:
703 if (len < sizeof(u->xfr_block))
704 goto short_msg;
705 rc = ccid_handle_xfr_block(cs, msg);
706 break;
707 case PC_to_RDR_GetParameters:
708 if (len != sizeof(u->get_parameters))
709 goto short_msg;
710 rc = ccid_handle_get_parameters(cs, msg);
711 break;
712 case PC_to_RDR_ResetParameters:
713 if (len != sizeof(u->reset_parameters))
714 goto short_msg;
715 rc = ccid_handle_reset_parameters(cs, msg);
716 break;
717 case PC_to_RDR_SetParameters:
Eric Wild33403af2019-10-01 15:01:58 +0200718 // smallest union member
719 if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10))
Harald Welte63653742019-01-03 16:54:16 +0100720 goto short_msg;
721 rc = ccid_handle_set_parameters(cs, msg);
722 break;
723 case PC_to_RDR_Escape:
724 if (len < sizeof(u->escape))
725 goto short_msg;
726 rc = ccid_handle_escape(cs, msg);
727 break;
728 case PC_to_RDR_IccClock:
729 if (len != sizeof(u->icc_clock))
730 goto short_msg;
731 rc = ccid_handle_icc_clock(cs, msg);
732 break;
733 case PC_to_RDR_T0APDU:
734 if (len != /*FIXME*/ sizeof(u->t0apdu))
735 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200736 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100737 break;
738 case PC_to_RDR_Secure:
739 if (len < sizeof(u->secure))
740 goto short_msg;
741 rc = ccid_handle_secure(cs, msg);
742 break;
743 case PC_to_RDR_Mechanical:
744 if (len != sizeof(u->mechanical))
745 goto short_msg;
746 rc = ccid_handle_mechanical(cs, msg);
747 break;
748 case PC_to_RDR_Abort:
749 if (len != sizeof(u->abort))
750 goto short_msg;
751 rc = ccid_handle_abort(cs, msg);
752 break;
753 case PC_to_RDR_SetDataRateAndClockFrequency:
754 if (len != sizeof(u->set_rate_and_clock))
755 goto short_msg;
756 rc = ccid_handle_set_rate_and_clock(cs, msg);
757 break;
758 default:
Harald Welte8772f582019-05-14 16:34:47 +0200759 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200760 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200761 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
762 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200763 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200764 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100765 }
Harald Welte922ff932019-05-16 11:21:51 +0200766 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
767 * of the msgb */
768 if (rc != 1)
769 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200770 return 0;
771
Harald Welte63653742019-01-03 16:54:16 +0100772short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200773 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200774 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200775 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100776}
Harald Weltebcbc1972019-05-15 21:57:32 +0200777
Harald Welte6982fab2019-05-16 23:01:35 +0200778/* Section 5.3.1 ABORT */
779static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req)
780{
781 uint16_t w_value = osmo_load16le(&req->wValue);
782 uint8_t slot_nr = w_value & 0xff;
783 uint8_t seq = w_value >> 8;
784 struct ccid_slot *cs;
785
786 if (slot_nr >= ARRAY_SIZE(ci->slot))
787 return CCID_CTRL_RET_INVALID;
788
789 cs = &ci->slot[slot_nr];
790
791 LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n");
792 /* Upon receiving the Control pipe ABORT request the CCID should check
793 * the state of the requested slot. */
794
795 /* If the last Bulk-OUT message received by the CCID was a
796 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
797 * request, then the CCID will respond to the Bulk-OUT message with
798 * the RDR_to_PC_SlotStatus response. */
799
800 /* FIXME */
801
802 /* If the previous Bulk-OUT message received by the CCID was not a
803 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
804 * request, then the CCID will fail all Bulk-Out commands to that slot
805 * until the PC_to_RDR_Abort command with the same bSlot and bSeq is
806 * received. Bulk-OUT commands will be failed by sending a response
807 * with bmCommandStatus=Failed and bError=CMD_ABORTED. */
808
809 /* FIXME */
810 return CCID_CTRL_RET_OK;
811}
812
813/* Section 5.3.2 GET_CLOCK_FREQUENCIES */
814static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
815 const uint8_t **data_in)
816{
817 uint16_t len = osmo_load16le(&req->wLength);
818
819 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
820 return CCID_CTRL_RET_INVALID;
821
822 *data_in = (const uint8_t *) ci->clock_freqs;
823 return CCID_CTRL_RET_OK;
824}
825
826/* Section 5.3.3 GET_DATA_RATES */
827static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
828 const uint8_t **data_in)
829{
830 uint16_t len = osmo_load16le(&req->wLength);
831
832 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
833 return CCID_CTRL_RET_INVALID;
834
835 *data_in = (const uint8_t *) ci->data_rates;
836 return CCID_CTRL_RET_OK;
837}
838
839/*! Handle [class specific] CTRL request. We assume the caller has already verified that the
840 * request was made to the correct interface as well as it is a class-specific request.
841 * \param[in] ci CCID Instance for which CTRL request was received
842 * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header
843 * \param[out] data_in data to be returned to the host in the IN transaction (if any)
844 * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN
845 */
846int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in)
847{
848 const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req;
849 int rc;
850
851 LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n",
852 req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest),
853 req->wValue, req->wIndex, req->wLength);
854
855 switch (req->bRequest) {
856 case CLASS_SPEC_CCID_ABORT:
857 rc = ccid_handle_ctrl_abort(ci, req);
858 break;
859 case CLASS_SPEC_CCID_GET_CLOCK_FREQ:
860 rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in);
861 break;
862 case CLASS_SPEC_CCID_GET_DATA_RATES:
863 rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in);
864 break;
865 default:
866 return CCID_CTRL_RET_UNKNOWN;
867 }
868 return rc;
869}
870
Harald Weltecab5d152019-05-16 13:31:16 +0200871void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
Harald Welte6982fab2019-05-16 23:01:35 +0200872 const struct ccid_slot_ops *slot_ops,
873 const struct usb_ccid_class_descriptor *class_desc,
874 const uint32_t *data_rates, const uint32_t *clock_freqs,
875 const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200876{
877 int i;
878
Harald Weltecab5d152019-05-16 13:31:16 +0200879 ci->ops = ops;
880 ci->slot_ops = slot_ops;
Harald Welte6982fab2019-05-16 23:01:35 +0200881 ci->class_desc = class_desc;
882 ci->clock_freqs = clock_freqs;
883 ci->data_rates = data_rates;
Harald Weltebcbc1972019-05-15 21:57:32 +0200884 ci->name = name;
885 ci->priv = priv;
Harald Welted3385072019-05-16 17:38:23 +0200886
887 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
888 struct ccid_slot *cs = &ci->slot[i];
889 cs->slot_nr = i;
890 cs->ci = ci;
891
892 slot_ops->init(cs);
893 }
894
Harald Weltebcbc1972019-05-15 21:57:32 +0200895}