blob: 049b1cf1c09fa3a386c0f522e9dd77c895357d81 [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{
26 /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */
27 if (in->bmTCCKST0 & 0xFD)
28 return -11;
29 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
30 if (in->bClockStop & 0xFC)
31 return -14;
32
33 out->fi = in->bmFindexDindex >> 4;
34 out->di = in->bmFindexDindex & 0xF;
35 if (in->bmTCCKST0 & 2)
36 out->inverse_convention = true;
37 else
38 out->inverse_convention = false;
39 if (in->bGuardTimeT0 == 0xff)
40 out->t0.guard_time_etu = 0;
41 else
42 out->t0.guard_time_etu = in->bGuardTimeT0;
43 out->t0.waiting_integer = in->bWaitingIntegerT0;
44 out->clock_stop = in->bClockStop & 0x03;
45
46 return 0;
47}
48
49/* encode T0 parameters from parsed form into on-the-wire encoding */
50static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in)
51{
52 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
53 if (in->inverse_convention)
54 out->bmTCCKST0 = 0x02;
55 else
56 out->bmTCCKST0 = 0x00;
57 out->bGuardTimeT0 = in->t0.guard_time_etu;
58 out->bWaitingIntegerT0 = in->t0.waiting_integer;
59 out->bClockStop = in->clock_stop & 0x03;
60}
61
62/* decode on-the-wire T1 parameters into their parsed form */
63static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in)
64{
65 /* input validation: only some values permitted for bmTCCKST0 */
66 if (in->bmTCCKST1 & 0xE8)
67 return -11;
68 /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */
69 if (in->bWaitingIntegersT1 > 0x9F)
70 return -13;
71 /* input validation: only 0x00 to 0x03 permitted for bClockSTop */
72 if (in->bClockStop & 0xFC)
73 return -14;
74 /* input validation: only 0x00 to 0xFE permitted for bIFSC */
75 if (in->bIFSC > 0xFE)
76 return -15;
77
78 out->fi = in->bmFindexDindex >> 4;
79 out->di = in->bmFindexDindex & 0xF;
80 if (in->bmTCCKST1 & 1)
81 out->t1.csum_type = CCID_CSUM_TYPE_CRC;
82 else
83 out->t1.csum_type = CCID_CSUM_TYPE_LRC;
84 if (in->bmTCCKST1 & 2)
85 out->inverse_convention = true;
86 else
87 out->inverse_convention = false;
88 out->t1.guard_time_t1 = in->bGuardTimeT1;
89 out->t1.bwi = in->bWaitingIntegersT1 >> 4;
90 out->t1.cwi = in->bWaitingIntegersT1 & 0xF;
91 out->clock_stop = in->bClockStop & 0x03;
92 out->t1.ifsc = in->bIFSC;
93 out->t1.nad = in->bNadValue;
94
95 return 0;
96}
97
98/* encode T1 parameters from parsed form into on-the-wire encoding */
99static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in)
100{
101 out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
102 out->bmTCCKST1 = 0x10;
103 if (in->t1.csum_type == CCID_CSUM_TYPE_CRC)
104 out->bmTCCKST1 |= 0x01;
105 if (in->inverse_convention)
106 out->bmTCCKST1 |= 0x02;
107 out->bGuardTimeT1 = in->t1.guard_time_t1;
108 out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F);
109 out->bClockStop = in->clock_stop & 0x03;
110 out->bIFSC = in->t1.ifsc;
111 out->bNadValue = in->t1.nad;
112}
113
Harald Welte63653742019-01-03 16:54:16 +0100114#define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x)
115#define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x)
116
117static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr)
118{
119 if (slot_nr >= sizeof(ci->slot))
120 return NULL;
121 else
Harald Welte92e7c0b2019-05-14 09:32:10 +0200122 return &ci->slot[slot_nr];
Harald Welte63653742019-01-03 16:54:16 +0100123}
124
125static uint8_t get_icc_status(const struct ccid_slot *cs)
126{
127 if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset)
128 return CCID_ICC_STATUS_PRES_ACT;
129 else if (!cs->icc_present)
130 return CCID_ICC_STATUS_NO_ICC;
131 else
132 return CCID_ICC_STATUS_PRES_INACT;
133}
134
135#define SET_HDR(x, msg_type, slot, seq) do { \
136 (x)->hdr.bMessageType = msg_type; \
137 (x)->hdr.dwLength = 0; \
138 (x)->hdr.bSlot = slot; \
139 (x)->hdr.bSeq = seq; \
140 } while (0)
141
142#define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \
143 SET_HDR(&(x)->hdr, msg_type, slot, seq); \
144 (x)->hdr.bStatus = status; \
145 (x)->hdr.bError = error; \
146 } while (0)
147
Harald Welte8772f582019-05-14 16:34:47 +0200148#if 0
149static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u)
150{
151 const struct ccid_header *ch = (const struct ccid_header *) u;
152 return ch->bSeq;
153}
154#endif
155
Harald Welte63653742019-01-03 16:54:16 +0100156/***********************************************************************
157 * Message generation / sending
158 ***********************************************************************/
159
160static struct msgb *ccid_msgb_alloc(void)
161{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200162 struct msgb *msg = msgb_alloc(512, "ccid");
Harald Welte63653742019-01-03 16:54:16 +0100163 OSMO_ASSERT(msg);
164 return msg;
165}
166
Harald Welte8772f582019-05-14 16:34:47 +0200167/* Send given CCID message */
Harald Welte63653742019-01-03 16:54:16 +0100168static int ccid_send(struct ccid_instance *ci, struct msgb *msg)
169{
Harald Weltee73a1df2019-05-15 22:27:02 +0200170 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
171 struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot);
172 if (cs) {
173 LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
174 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
175 } else {
176 LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
177 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
178 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200179 return ci->ops->send_in(ci, msg);
Harald Welte63653742019-01-03 16:54:16 +0100180}
181
Harald Welte8772f582019-05-14 16:34:47 +0200182/* Send given CCID message for given slot; patch bSlot into message */
Harald Welte005b09d2019-05-16 17:24:29 +0200183int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg)
Harald Welte63653742019-01-03 16:54:16 +0100184{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200185 struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
Harald Welte63653742019-01-03 16:54:16 +0100186
187 /* patch bSlotNr into message */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200188 ch->bSlot = cs->slot_nr;
Harald Welte63653742019-01-03 16:54:16 +0100189 return ccid_send(cs->ci, msg);
190}
191
Harald Welte8772f582019-05-14 16:34:47 +0200192/* Send given CCID message and mark slot as un-busy */
Harald Welte005b09d2019-05-16 17:24:29 +0200193int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg)
Harald Welte8772f582019-05-14 16:34:47 +0200194{
195 cs->cmd_busy = false;
196 return ccid_slot_send(cs, msg);
197}
Harald Welte63653742019-01-03 16:54:16 +0100198
199/* Section 6.2.1 */
Harald Welte8772f582019-05-14 16:34:47 +0200200static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
201 uint8_t cmd_sts, enum ccid_error_code err,
202 const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100203{
204 struct msgb *msg = ccid_msgb_alloc();
Harald Welte8772f582019-05-14 16:34:47 +0200205 struct ccid_rdr_to_pc_data_block *db =
Harald Welte92e7c0b2019-05-14 09:32:10 +0200206 (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200207 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100208
Harald Welte8772f582019-05-14 16:34:47 +0200209 SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200210 osmo_store32le(data_len, &db->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100211 memcpy(db->abData, data, data_len);
212 return msg;
213}
Harald Welte005b09d2019-05-16 17:24:29 +0200214struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
215 enum ccid_error_code err, const uint8_t *data,
216 uint32_t data_len)
Harald Welte8772f582019-05-14 16:34:47 +0200217{
218 return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
219}
Harald Welte63653742019-01-03 16:54:16 +0100220
221/* Section 6.2.2 */
Harald Welte8772f582019-05-14 16:34:47 +0200222static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status,
223 uint8_t seq, uint8_t cmd_sts,
224 enum ccid_error_code err)
Harald Welte63653742019-01-03 16:54:16 +0100225{
226 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200227 struct ccid_rdr_to_pc_slot_status *ss =
228 (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss));
Harald Welte8772f582019-05-14 16:34:47 +0200229 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100230
Harald Welte8772f582019-05-14 16:34:47 +0200231 SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err);
Harald Welte63653742019-01-03 16:54:16 +0100232 return msg;
233}
Harald Welte005b09d2019-05-16 17:24:29 +0200234struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
235 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200236{
237 return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err);
238}
Harald Welte63653742019-01-03 16:54:16 +0100239
240/* Section 6.2.3 */
Harald Welte8772f582019-05-14 16:34:47 +0200241static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status,
242 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200243 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200244{
245 struct msgb *msg = ccid_msgb_alloc();
246 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200247 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0));
Harald Welte8772f582019-05-14 16:34:47 +0200248 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
249
250 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200251 if (dec_par) {
252 osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength);
253 encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200254 }
255 return msg;
256}
257static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200258 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200259{
Harald Welte8579ce52019-05-15 12:22:24 +0200260 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 +0200261}
262
263static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status,
264 uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
Harald Welte8579ce52019-05-15 12:22:24 +0200265 const struct ccid_pars_decoded *dec_par)
Harald Welte8772f582019-05-14 16:34:47 +0200266{
267 struct msgb *msg = ccid_msgb_alloc();
268 struct ccid_rdr_to_pc_parameters *par =
Harald Welte8579ce52019-05-15 12:22:24 +0200269 (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1));
Harald Welte8772f582019-05-14 16:34:47 +0200270 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
271
272 SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
Harald Welte8579ce52019-05-15 12:22:24 +0200273 if (dec_par) {
274 osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength);
275 encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par);
Harald Welte8772f582019-05-14 16:34:47 +0200276 }
277 return msg;
278}
279static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
Harald Welte8579ce52019-05-15 12:22:24 +0200280 enum ccid_error_code err)
Harald Welte8772f582019-05-14 16:34:47 +0200281{
Harald Welte8579ce52019-05-15 12:22:24 +0200282 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 +0200283}
284
Harald Welte63653742019-01-03 16:54:16 +0100285
286/* Section 6.2.4 */
Harald Welte8772f582019-05-14 16:34:47 +0200287static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts,
288 enum ccid_error_code err, const uint8_t *data, uint32_t data_len)
Harald Welte63653742019-01-03 16:54:16 +0100289{
290 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200291 struct ccid_rdr_to_pc_escape *esc =
292 (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len);
Harald Welte8772f582019-05-14 16:34:47 +0200293 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100294
Harald Welte8772f582019-05-14 16:34:47 +0200295 SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200296 osmo_store32le(data_len, &esc->hdr.hdr.dwLength);
Harald Welte63653742019-01-03 16:54:16 +0100297 memcpy(esc->abData, data, data_len);
298 return msg;
299}
Harald Welte8772f582019-05-14 16:34:47 +0200300static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
301 enum ccid_error_code err, const uint8_t *data,
302 uint32_t data_len)
303{
304 return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
305}
Harald Welte63653742019-01-03 16:54:16 +0100306
307/* Section 6.2.5 */
Harald Welte8772f582019-05-14 16:34:47 +0200308static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
309 uint8_t cmd_sts, enum ccid_error_code err,
310 uint32_t clock_khz, uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100311{
312 struct msgb *msg = ccid_msgb_alloc();
Harald Welte92e7c0b2019-05-14 09:32:10 +0200313 struct ccid_rdr_to_pc_data_rate_and_clock *drc =
314 (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc));
Harald Welte8772f582019-05-14 16:34:47 +0200315 uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;
Harald Welte63653742019-01-03 16:54:16 +0100316
Harald Welte8772f582019-05-14 16:34:47 +0200317 SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200318 osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */
319 osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */
320 osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */
Harald Welte63653742019-01-03 16:54:16 +0100321 return msg;
322}
Harald Welte8772f582019-05-14 16:34:47 +0200323static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
324 enum ccid_error_code err, uint32_t clock_khz,
325 uint32_t rate_bps)
Harald Welte63653742019-01-03 16:54:16 +0100326{
Harald Welte8772f582019-05-14 16:34:47 +0200327 return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err,
328 clock_khz, rate_bps);
Harald Welte63653742019-01-03 16:54:16 +0100329}
Harald Welte8772f582019-05-14 16:34:47 +0200330
331/*! generate an error response for given input message_type/slot_nr/seq
332 * \param[in] msg_type CCID Message Type against which response is to be created
333 * \param[in] slot_nr CCID Slot Number
334 * \param[in] icc_status ICC Status of the slot
335 * \param[in] seq CCID Sequence number
336 * \param[in] err_code CCID Error Code to send
337 * \returns dynamically-allocated message buffer containing error response */
338static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status,
339 uint8_t seq, enum ccid_error_code err_code)
340{
341 struct msgb *resp = NULL;
342
343 switch (msg_type) {
344 case PC_to_RDR_IccPowerOn:
345 case PC_to_RDR_XfrBlock:
346 case PC_to_RDR_Secure:
347 /* Return RDR_to_PC_DataBlock */
348 resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
349 err_code, NULL, 0);
350 break;
351
352 case PC_to_RDR_IccPowerOff:
353 case PC_to_RDR_GetSlotStatus:
354 case PC_to_RDR_IccClock:
355 case PC_to_RDR_T0APDU:
356 case PC_to_RDR_Mechanical:
357 case PC_to_RDR_Abort:
358 /* Return RDR_to_PC_SlotStatus */
359 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
360 err_code);
361 break;
362
363 case PC_to_RDR_GetParameters:
364 case PC_to_RDR_ResetParameters:
365 case PC_to_RDR_SetParameters:
366 /* Return RDR_to_PC_Parameters */
367 resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
368 err_code, NULL); /* FIXME: parameters? */
369 break;
370
371 case PC_to_RDR_Escape:
372 /* Return RDR_to_PC_Escape */
373 resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
374 err_code, NULL, 0);
375 break;
376
377 case PC_to_RDR_SetDataRateAndClockFrequency:
378 /* Return RDR_to_PC_SlotStatus */
379 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
380 err_code);
381 break;
382
383 default:
384 /* generate general error */
385 resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
386 CCID_ERR_CMD_NOT_SUPPORTED);
387 break;
388 }
389 return resp;
390}
Harald Welte63653742019-01-03 16:54:16 +0100391
392/***********************************************************************
393 * Message reception / parsing
394 ***********************************************************************/
395
396/* Section 6.1.3 */
397static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg)
398{
399 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200400 const struct ccid_header *ch = (const struct ccid_header *) u;
401 uint8_t seq = u->get_slot_status.hdr.bSeq;
Harald Welte63653742019-01-03 16:54:16 +0100402 struct msgb *resp;
403
Harald Welte8772f582019-05-14 16:34:47 +0200404 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte63653742019-01-03 16:54:16 +0100405
Harald Welte8772f582019-05-14 16:34:47 +0200406 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100407}
408
409
410/* Section 6.1.1 */
411static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg)
412{
413 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200414 const struct ccid_header *ch = (const struct ccid_header *) u;
Harald Welte63653742019-01-03 16:54:16 +0100415
Harald Welte505d4412019-05-16 17:26:09 +0200416 /* handle this asynchronously */
417 cs->ci->slot_ops->icc_power_on_async(cs, msg, &u->icc_power_on);
418 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100419}
420
421/* Section 6.1.2 */
422static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg)
423{
424 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200425 const struct ccid_header *ch = (const struct ccid_header *) u;
426 uint8_t seq = u->icc_power_off.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200427 struct msgb *resp;
428
Harald Weltecab5d152019-05-16 13:31:16 +0200429 cs->ci->slot_ops->set_power(cs, false);
Harald Welte8772f582019-05-14 16:34:47 +0200430 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
431 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100432}
433
434/* Section 6.1.4 */
435static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg)
436{
437 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200438 const struct ccid_header *ch = (const struct ccid_header *) u;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200439
Harald Welte505d4412019-05-16 17:26:09 +0200440 /* handle this asynchronously */
441 cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block);
442 return 1;
Harald Welte63653742019-01-03 16:54:16 +0100443}
444
445/* Section 6.1.5 */
446static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
447{
448 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200449 const struct ccid_header *ch = (const struct ccid_header *) u;
450 uint8_t seq = u->get_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200451 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200452
Harald Welte8579ce52019-05-15 12:22:24 +0200453 /* FIXME: T=1 */
454 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200455 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100456}
457
458/* Section 6.1.6 */
459static int ccid_handle_reset_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->reset_parameters.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200464 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200465
Harald Weltecab5d152019-05-16 13:31:16 +0200466 /* copy default parameters from somewhere */
Harald Welte8579ce52019-05-15 12:22:24 +0200467 /* FIXME: T=1 */
Harald Weltecab5d152019-05-16 13:31:16 +0200468 cs->ci->slot_ops->set_params(cs, CCID_PROTOCOL_NUM_T0, cs->default_pars);
469 cs->pars = *cs->default_pars;
470
Harald Welte8579ce52019-05-15 12:22:24 +0200471 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8772f582019-05-14 16:34:47 +0200472 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100473}
474
475/* Section 6.1.7 */
476static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
477{
478 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8579ce52019-05-15 12:22:24 +0200479 const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
Harald Welte8772f582019-05-14 16:34:47 +0200480 const struct ccid_header *ch = (const struct ccid_header *) u;
481 uint8_t seq = u->set_parameters.hdr.bSeq;
Harald Welte8579ce52019-05-15 12:22:24 +0200482 struct ccid_pars_decoded pars_dec;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200483 struct msgb *resp;
Harald Welte8579ce52019-05-15 12:22:24 +0200484 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200485
Harald Welte8579ce52019-05-15 12:22:24 +0200486 switch (spar->bProtocolNum) {
487 case CCID_PROTOCOL_NUM_T0:
488 rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
Harald Welte8579ce52019-05-15 12:22:24 +0200489 break;
490 case CCID_PROTOCOL_NUM_T1:
491 rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
Harald Welte8579ce52019-05-15 12:22:24 +0200492 break;
493 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200494 LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
Harald Welte8579ce52019-05-15 12:22:24 +0200495 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
Harald Weltecab5d152019-05-16 13:31:16 +0200496 goto out;
497 }
498
499 if (rc < 0) {
500 LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc);
501 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
502 goto out;
503 }
504
505 /* validate parameters; abort if they are not supported */
506 rc = cs->ci->slot_ops->set_params(cs, spar->bProtocolNum, &pars_dec);
507 if (rc < 0) {
508 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
509 } else {
510 cs->pars = pars_dec;
511 resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
Harald Welte8579ce52019-05-15 12:22:24 +0200512 }
Harald Weltee73a1df2019-05-15 22:27:02 +0200513out:
Harald Welte8772f582019-05-14 16:34:47 +0200514 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100515}
516
517/* Section 6.1.8 */
518static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
519{
520 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200521 const struct ccid_header *ch = (const struct ccid_header *) u;
522 uint8_t seq = u->escape.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200523 struct msgb *resp;
Harald Welte8772f582019-05-14 16:34:47 +0200524
525 resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
526 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100527}
528
529/* Section 6.1.9 */
530static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
531{
532 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200533 const struct ccid_header *ch = (const struct ccid_header *) u;
534 uint8_t seq = u->icc_clock.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200535 struct msgb *resp;
536
Harald Weltecab5d152019-05-16 13:31:16 +0200537 cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand);
Harald Welte8772f582019-05-14 16:34:47 +0200538 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
539 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100540}
541
542/* Section 6.1.10 */
543static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
544{
545 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200546 const struct ccid_header *ch = (const struct ccid_header *) u;
547 uint8_t seq = u->t0apdu.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200548 struct msgb *resp;
549
Harald Weltecab5d152019-05-16 13:31:16 +0200550 /* FIXME: Required for APDU level exchange */
Harald Welte8772f582019-05-14 16:34:47 +0200551 //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
552 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
553 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100554}
555
556/* Section 6.1.11 */
557static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
558{
559 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200560 const struct ccid_header *ch = (const struct ccid_header *) u;
561 uint8_t seq = u->secure.hdr.bSeq;
562 struct msgb *resp;
563
564 /* FIXME */
565 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
566 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100567}
568
569/* Section 6.1.12 */
570static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
571{
572 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200573 const struct ccid_header *ch = (const struct ccid_header *) u;
574 uint8_t seq = u->mechanical.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200575 struct msgb *resp;
576
Harald Welte8772f582019-05-14 16:34:47 +0200577 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
578 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100579}
580
581/* Section 6.1.13 */
582static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
583{
584 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200585 const struct ccid_header *ch = (const struct ccid_header *) u;
586 uint8_t seq = u->abort.hdr.bSeq;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200587 struct msgb *resp;
588
Harald Welte8772f582019-05-14 16:34:47 +0200589 /* Check if the currently in-progress message is Abortable */
590 switch (0/* FIXME */) {
591 case PC_to_RDR_IccPowerOn:
592 case PC_to_RDR_XfrBlock:
593 case PC_to_RDR_Escape:
594 case PC_to_RDR_Secure:
595 case PC_to_RDR_Mechanical:
596 //case PC_to_RDR_Abort: /* seriously? WTF! */
597 break;
598 default:
Harald Weltee73a1df2019-05-15 22:27:02 +0200599 LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
Harald Welte8772f582019-05-14 16:34:47 +0200600 /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
601 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
602 return ccid_slot_send_unbusy(cs, resp);
603 }
604
605 /* FIXME */
606 resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
607 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100608}
609
610/* Section 6.1.14 */
611static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
612{
Harald Welte92e7c0b2019-05-14 09:32:10 +0200613 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200614 const struct ccid_header *ch = (const struct ccid_header *) u;
615 uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
Harald Weltecab5d152019-05-16 13:31:16 +0200616 uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency);
617 uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200618 struct msgb *resp;
Harald Weltecab5d152019-05-16 13:31:16 +0200619 int rc;
Harald Welte8772f582019-05-14 16:34:47 +0200620
Harald Weltecab5d152019-05-16 13:31:16 +0200621 /* FIXME: which rate to return in failure case? */
622 rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps);
623 if (rc < 0)
624 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000);
625 else
626 resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz);
Harald Welte8772f582019-05-14 16:34:47 +0200627 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100628}
629
Harald Welte8772f582019-05-14 16:34:47 +0200630/*! Handle data arriving from the host on the OUT endpoint.
631 * \param[in] cs CCID Instance on which to operate
Harald Weltee6ac4162019-05-16 11:18:00 +0200632 * \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
633 * Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
634 * it eventually, after we're done with it (could be asynchronously).
Harald Welte8772f582019-05-14 16:34:47 +0200635 * \returns 0 on success; negative on error */
Harald Welte63653742019-01-03 16:54:16 +0100636int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
637{
638 const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
639 const struct ccid_header *ch = (const struct ccid_header *) u;
640 unsigned int len = msgb_length(msg);
641 struct ccid_slot *cs;
Harald Welte8772f582019-05-14 16:34:47 +0200642 struct msgb *resp;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200643 int rc;
Harald Welte63653742019-01-03 16:54:16 +0100644
645 if (len < sizeof(*ch)) {
646 /* FIXME */
647 return -1;
648 }
649
Harald Welte8772f582019-05-14 16:34:47 +0200650 /* Check for invalid slot number */
Harald Welte92e7c0b2019-05-14 09:32:10 +0200651 cs = get_ccid_slot(ci, ch->bSlot);
Harald Welte63653742019-01-03 16:54:16 +0100652 if (!cs) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200653 LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
Harald Welte8772f582019-05-14 16:34:47 +0200654 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
655 return ccid_send(ci, resp);
Harald Welte63653742019-01-03 16:54:16 +0100656 }
657
Harald Welte8772f582019-05-14 16:34:47 +0200658 /* Check if slot is already busy; Reject any additional commands meanwhile */
659 if (cs->cmd_busy) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200660 LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
Harald Welte8772f582019-05-14 16:34:47 +0200661 /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
662 resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
663 CCID_ERR_CMD_SLOT_BUSY);
664 return ccid_send(ci, resp);
665 }
666
Harald Weltee73a1df2019-05-15 22:27:02 +0200667 LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
668 get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
669
Harald Welte8772f582019-05-14 16:34:47 +0200670 /* we're now processing a command for the slot; mark slot as busy */
671 cs->cmd_busy = true;
672
673 /* TODO: enqueue into the per-slot specific input queue */
674
Harald Weltecab5d152019-05-16 13:31:16 +0200675 /* call pre-processing call-back function; allows reader to update state */
676 if (ci->slot_ops->pre_proc_cb)
677 ci->slot_ops->pre_proc_cb(cs, msg);
678
Harald Welte63653742019-01-03 16:54:16 +0100679 switch (ch->bMessageType) {
680 case PC_to_RDR_GetSlotStatus:
Eric Wild33403af2019-10-01 15:01:58 +0200681 if (len < sizeof(u->get_slot_status))
Harald Welte63653742019-01-03 16:54:16 +0100682 goto short_msg;
683 rc = ccid_handle_get_slot_status(cs, msg);
684 break;
685 case PC_to_RDR_IccPowerOn:
686 if (len != sizeof(u->icc_power_on))
687 goto short_msg;
688 rc = ccid_handle_icc_power_on(cs, msg);
689 break;
690 case PC_to_RDR_IccPowerOff:
691 if (len != sizeof(u->icc_power_off))
692 goto short_msg;
693 rc = ccid_handle_icc_power_off(cs, msg);
694 break;
695 case PC_to_RDR_XfrBlock:
696 if (len < sizeof(u->xfr_block))
697 goto short_msg;
698 rc = ccid_handle_xfr_block(cs, msg);
699 break;
700 case PC_to_RDR_GetParameters:
701 if (len != sizeof(u->get_parameters))
702 goto short_msg;
703 rc = ccid_handle_get_parameters(cs, msg);
704 break;
705 case PC_to_RDR_ResetParameters:
706 if (len != sizeof(u->reset_parameters))
707 goto short_msg;
708 rc = ccid_handle_reset_parameters(cs, msg);
709 break;
710 case PC_to_RDR_SetParameters:
Eric Wild33403af2019-10-01 15:01:58 +0200711 // smallest union member
712 if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10))
Harald Welte63653742019-01-03 16:54:16 +0100713 goto short_msg;
714 rc = ccid_handle_set_parameters(cs, msg);
715 break;
716 case PC_to_RDR_Escape:
717 if (len < sizeof(u->escape))
718 goto short_msg;
719 rc = ccid_handle_escape(cs, msg);
720 break;
721 case PC_to_RDR_IccClock:
722 if (len != sizeof(u->icc_clock))
723 goto short_msg;
724 rc = ccid_handle_icc_clock(cs, msg);
725 break;
726 case PC_to_RDR_T0APDU:
727 if (len != /*FIXME*/ sizeof(u->t0apdu))
728 goto short_msg;
Harald Welte92e7c0b2019-05-14 09:32:10 +0200729 rc = ccid_handle_t0apdu(cs, msg);
Harald Welte63653742019-01-03 16:54:16 +0100730 break;
731 case PC_to_RDR_Secure:
732 if (len < sizeof(u->secure))
733 goto short_msg;
734 rc = ccid_handle_secure(cs, msg);
735 break;
736 case PC_to_RDR_Mechanical:
737 if (len != sizeof(u->mechanical))
738 goto short_msg;
739 rc = ccid_handle_mechanical(cs, msg);
740 break;
741 case PC_to_RDR_Abort:
742 if (len != sizeof(u->abort))
743 goto short_msg;
744 rc = ccid_handle_abort(cs, msg);
745 break;
746 case PC_to_RDR_SetDataRateAndClockFrequency:
747 if (len != sizeof(u->set_rate_and_clock))
748 goto short_msg;
749 rc = ccid_handle_set_rate_and_clock(cs, msg);
750 break;
751 default:
Harald Welte8772f582019-05-14 16:34:47 +0200752 /* generic response with bERror = 0 (command not supported) */
Harald Weltee73a1df2019-05-15 22:27:02 +0200753 LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
Harald Welte8772f582019-05-14 16:34:47 +0200754 resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
755 CCID_ERR_CMD_NOT_SUPPORTED);
Harald Welte922ff932019-05-16 11:21:51 +0200756 msgb_free(msg);
Harald Welte8772f582019-05-14 16:34:47 +0200757 return ccid_slot_send_unbusy(cs, resp);
Harald Welte63653742019-01-03 16:54:16 +0100758 }
Harald Welte922ff932019-05-16 11:21:51 +0200759 /* the various ccid_handle_* functions can return '1' to tell us that they took ownership
760 * of the msgb */
761 if (rc != 1)
762 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200763 return 0;
764
Harald Welte63653742019-01-03 16:54:16 +0100765short_msg:
Harald Weltee73a1df2019-05-15 22:27:02 +0200766 LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
Harald Welte922ff932019-05-16 11:21:51 +0200767 msgb_free(msg);
Harald Welte92e7c0b2019-05-14 09:32:10 +0200768 return -1;
Harald Welte63653742019-01-03 16:54:16 +0100769}
Harald Weltebcbc1972019-05-15 21:57:32 +0200770
Harald Welte6982fab2019-05-16 23:01:35 +0200771/* Section 5.3.1 ABORT */
772static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req)
773{
774 uint16_t w_value = osmo_load16le(&req->wValue);
775 uint8_t slot_nr = w_value & 0xff;
776 uint8_t seq = w_value >> 8;
777 struct ccid_slot *cs;
778
779 if (slot_nr >= ARRAY_SIZE(ci->slot))
780 return CCID_CTRL_RET_INVALID;
781
782 cs = &ci->slot[slot_nr];
783
784 LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n");
785 /* Upon receiving the Control pipe ABORT request the CCID should check
786 * the state of the requested slot. */
787
788 /* If the last Bulk-OUT message received by the CCID was a
789 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
790 * request, then the CCID will respond to the Bulk-OUT message with
791 * the RDR_to_PC_SlotStatus response. */
792
793 /* FIXME */
794
795 /* If the previous Bulk-OUT message received by the CCID was not a
796 * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT
797 * request, then the CCID will fail all Bulk-Out commands to that slot
798 * until the PC_to_RDR_Abort command with the same bSlot and bSeq is
799 * received. Bulk-OUT commands will be failed by sending a response
800 * with bmCommandStatus=Failed and bError=CMD_ABORTED. */
801
802 /* FIXME */
803 return CCID_CTRL_RET_OK;
804}
805
806/* Section 5.3.2 GET_CLOCK_FREQUENCIES */
807static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
808 const uint8_t **data_in)
809{
810 uint16_t len = osmo_load16le(&req->wLength);
811
812 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
813 return CCID_CTRL_RET_INVALID;
814
815 *data_in = (const uint8_t *) ci->clock_freqs;
816 return CCID_CTRL_RET_OK;
817}
818
819/* Section 5.3.3 GET_DATA_RATES */
820static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req,
821 const uint8_t **data_in)
822{
823 uint16_t len = osmo_load16le(&req->wLength);
824
825 if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported)
826 return CCID_CTRL_RET_INVALID;
827
828 *data_in = (const uint8_t *) ci->data_rates;
829 return CCID_CTRL_RET_OK;
830}
831
832/*! Handle [class specific] CTRL request. We assume the caller has already verified that the
833 * request was made to the correct interface as well as it is a class-specific request.
834 * \param[in] ci CCID Instance for which CTRL request was received
835 * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header
836 * \param[out] data_in data to be returned to the host in the IN transaction (if any)
837 * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN
838 */
839int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in)
840{
841 const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req;
842 int rc;
843
844 LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n",
845 req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest),
846 req->wValue, req->wIndex, req->wLength);
847
848 switch (req->bRequest) {
849 case CLASS_SPEC_CCID_ABORT:
850 rc = ccid_handle_ctrl_abort(ci, req);
851 break;
852 case CLASS_SPEC_CCID_GET_CLOCK_FREQ:
853 rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in);
854 break;
855 case CLASS_SPEC_CCID_GET_DATA_RATES:
856 rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in);
857 break;
858 default:
859 return CCID_CTRL_RET_UNKNOWN;
860 }
861 return rc;
862}
863
Harald Weltecab5d152019-05-16 13:31:16 +0200864void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops,
Harald Welte6982fab2019-05-16 23:01:35 +0200865 const struct ccid_slot_ops *slot_ops,
866 const struct usb_ccid_class_descriptor *class_desc,
867 const uint32_t *data_rates, const uint32_t *clock_freqs,
868 const char *name, void *priv)
Harald Weltebcbc1972019-05-15 21:57:32 +0200869{
870 int i;
871
Harald Weltecab5d152019-05-16 13:31:16 +0200872 ci->ops = ops;
873 ci->slot_ops = slot_ops;
Harald Welte6982fab2019-05-16 23:01:35 +0200874 ci->class_desc = class_desc;
875 ci->clock_freqs = clock_freqs;
876 ci->data_rates = data_rates;
Harald Weltebcbc1972019-05-15 21:57:32 +0200877 ci->name = name;
878 ci->priv = priv;
Harald Welted3385072019-05-16 17:38:23 +0200879
880 for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
881 struct ccid_slot *cs = &ci->slot[i];
882 cs->slot_nr = i;
883 cs->ci = ci;
884
885 slot_ops->init(cs);
886 }
887
Harald Weltebcbc1972019-05-15 21:57:32 +0200888}