aboutsummaryrefslogtreecommitdiffstats
path: root/ccid/ccid_device.c
blob: f91abb83f3cdc84f210c0cdad09365f082dd229d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>

#include <osmocom/core/msgb.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>

#include "ccid_proto.h"
#include "ccid_device.h"

/* decode on-the-wire T0 parameters into their parsed form */
static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in)
{
	/* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 */
	if (in->bmTCCKST0 & 0xFD)
		return -11;
	/* input validation: only 0x00 to 0x03 permitted for bClockSTop */
	if (in->bClockStop & 0xFC)
		return -14;

	out->fi = in->bmFindexDindex >> 4;
	out->di = in->bmFindexDindex & 0xF;
	if (in->bmTCCKST0 & 2)
		out->inverse_convention = true;
	else
		out->inverse_convention = false;
	if (in->bGuardTimeT0 == 0xff)
		out->t0.guard_time_etu = 0;
	else
		out->t0.guard_time_etu = in->bGuardTimeT0;
	out->t0.waiting_integer = in->bWaitingIntegerT0;
	out->clock_stop = in->bClockStop & 0x03;

	return 0;
}

/* encode T0 parameters from parsed form into on-the-wire encoding */
static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in)
{
	out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
	if (in->inverse_convention)
		out->bmTCCKST0 = 0x02;
	else
		out->bmTCCKST0 = 0x00;
	out->bGuardTimeT0 = in->t0.guard_time_etu;
	out->bWaitingIntegerT0 = in->t0.waiting_integer;
	out->bClockStop = in->clock_stop & 0x03;
}

/* decode on-the-wire T1 parameters into their parsed form */
static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in)
{
	/* input validation: only some values permitted for bmTCCKST0 */
	if (in->bmTCCKST1 & 0xE8)
		return -11;
	/* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */
	if (in->bWaitingIntegersT1 > 0x9F)
		return -13;
	/* input validation: only 0x00 to 0x03 permitted for bClockSTop */
	if (in->bClockStop & 0xFC)
		return -14;
	/* input validation: only 0x00 to 0xFE permitted for bIFSC */
	if (in->bIFSC > 0xFE)
		return -15;

	out->fi = in->bmFindexDindex >> 4;
	out->di = in->bmFindexDindex & 0xF;
	if (in->bmTCCKST1 & 1)
		out->t1.csum_type = CCID_CSUM_TYPE_CRC;
	else
		out->t1.csum_type = CCID_CSUM_TYPE_LRC;
	if (in->bmTCCKST1 & 2)
		out->inverse_convention = true;
	else
		out->inverse_convention = false;
	out->t1.guard_time_t1 = in->bGuardTimeT1;
	out->t1.bwi = in->bWaitingIntegersT1 >> 4;
	out->t1.cwi = in->bWaitingIntegersT1 & 0xF;
	out->clock_stop = in->bClockStop & 0x03;
	out->t1.ifsc = in->bIFSC;
	out->t1.nad = in->bNadValue;

	return 0;
}

/* encode T1 parameters from parsed form into on-the-wire encoding */
static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in)
{
	out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F);
	out->bmTCCKST1 = 0x10;
	if (in->t1.csum_type == CCID_CSUM_TYPE_CRC)
		out->bmTCCKST1 |= 0x01;
	if (in->inverse_convention)
		out->bmTCCKST1 |= 0x02;
	out->bGuardTimeT1 = in->t1.guard_time_t1;
	out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F);
	out->bClockStop = in->clock_stop & 0x03;
	out->bIFSC = in->t1.ifsc;
	out->bNadValue = in->t1.nad;
}

#define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x)
#define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x)

static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr)
{
	if (slot_nr >= sizeof(ci->slot))
		return NULL;
	else
		return &ci->slot[slot_nr];
}

static uint8_t get_icc_status(const struct ccid_slot *cs)
{
	if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset)
		return CCID_ICC_STATUS_PRES_ACT;
	else if (!cs->icc_present)
		return CCID_ICC_STATUS_NO_ICC;
	else
		return CCID_ICC_STATUS_PRES_INACT;
}

#define SET_HDR(x, msg_type, slot, seq) do {	\
	(x)->hdr.bMessageType = msg_type;	\
	(x)->hdr.dwLength = 0;		\
	(x)->hdr.bSlot = slot;			\
	(x)->hdr.bSeq = seq;			\
	} while (0)

#define SET_HDR_IN(x, msg_type, slot, seq, status, error) do {	\
	SET_HDR(&(x)->hdr, msg_type, slot, seq);		\
	(x)->hdr.bStatus = status;				\
	(x)->hdr.bError = error;				\
	} while (0)

#if 0
static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u)
{
	const struct ccid_header *ch = (const struct ccid_header *) u;
	return ch->bSeq;
}
#endif

/***********************************************************************
 * Message generation / sending
 ***********************************************************************/

static struct msgb *ccid_msgb_alloc(void)
{
	struct msgb *msg = msgb_alloc(512, "ccid");
	OSMO_ASSERT(msg);
	return msg;
}

/* Send given CCID message */
static int ccid_send(struct ccid_instance *ci, struct msgb *msg)
{
	struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);
	struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot);
	if (cs) {
		LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
			get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
	} else {
		LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n",
			get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));
	}
	return ci->ops->send_in(ci, msg);
}

/* Send given CCID message for given slot; patch bSlot into message */
static int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg)
{
	struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg);

	/* patch bSlotNr into message */
	ch->bSlot = cs->slot_nr;
	return ccid_send(cs->ci, msg);
}

/* Send given CCID message and mark slot as un-busy */
static int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg)
{
	cs->cmd_busy = false;
	return ccid_slot_send(cs, msg);
}

/* Section 6.2.1 */
static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
					   uint8_t cmd_sts, enum ccid_error_code err,
					   const uint8_t *data, uint32_t data_len)
{
	struct msgb *msg = ccid_msgb_alloc();
	struct ccid_rdr_to_pc_data_block *db =
		(struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len);
	uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;

	SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err);
	osmo_store32le(data_len, &db->hdr.hdr.dwLength);
	memcpy(db->abData, data, data_len);
	return msg;
}
static struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
					 enum ccid_error_code err, const uint8_t *data,
					 uint32_t data_len)
{
	return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
}

/* Section 6.2.2 */
static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status,
					    uint8_t seq, uint8_t cmd_sts,
					    enum ccid_error_code err)
{
	struct msgb *msg = ccid_msgb_alloc();
	struct ccid_rdr_to_pc_slot_status *ss =
		(struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss));
	uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;

	SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err);
	return msg;
}
static struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
					 enum ccid_error_code err)
{
	return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err);
}

/* Section 6.2.3 */
static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status,
					      uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
					      const struct ccid_pars_decoded *dec_par)
{
	struct msgb *msg = ccid_msgb_alloc();
	struct ccid_rdr_to_pc_parameters *par =
		(struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0));
	uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;

	SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
	if (dec_par) {
		osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength);
		encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par);
	}
	return msg;
}
static struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
					   enum ccid_error_code err)
{
	return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars);
}

static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status,
					      uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err,
					      const struct ccid_pars_decoded *dec_par)
{
	struct msgb *msg = ccid_msgb_alloc();
	struct ccid_rdr_to_pc_parameters *par =
		(struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1));
	uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;

	SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err);
	if (dec_par) {
		osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength);
		encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par);
	}
	return msg;
}
static struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
					   enum ccid_error_code err)
{
	return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars);
}


/* Section 6.2.4 */
static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts,
					enum ccid_error_code err, const uint8_t *data, uint32_t data_len)
{
	struct msgb *msg = ccid_msgb_alloc();
	struct ccid_rdr_to_pc_escape *esc =
		(struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len);
	uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;

	SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err);
	osmo_store32le(data_len, &esc->hdr.hdr.dwLength);
	memcpy(esc->abData, data, data_len);
	return msg;
}
static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
				    enum ccid_error_code err, const uint8_t *data,
				    uint32_t data_len)
{
	return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len);
}

/* Section 6.2.5 */
static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq,
						uint8_t cmd_sts, enum ccid_error_code err,
						uint32_t clock_khz, uint32_t rate_bps)
{
	struct msgb *msg = ccid_msgb_alloc();
	struct ccid_rdr_to_pc_data_rate_and_clock *drc =
		(struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc));
	uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status;

	SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err);
	osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */
	osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */
	osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */
	return msg;
}
static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts,
					    enum ccid_error_code err, uint32_t clock_khz,
					    uint32_t rate_bps)
{
	return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err,
					  clock_khz, rate_bps);
}

/*! generate an error response for given input message_type/slot_nr/seq
 *  \param[in] msg_type CCID Message Type against which response is to be created
 *  \param[in] slot_nr CCID Slot Number
 *  \param[in] icc_status ICC Status of the slot
 *  \param[in] seq CCID Sequence number
 *  \param[in] err_code CCID Error Code to send
 *  \returns dynamically-allocated message buffer containing error response */
static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status,
				 uint8_t seq, enum ccid_error_code err_code)
{
	struct msgb *resp = NULL;

	switch (msg_type) {
	case PC_to_RDR_IccPowerOn:
	case PC_to_RDR_XfrBlock:
	case PC_to_RDR_Secure:
		/* Return RDR_to_PC_DataBlock */
		resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
						err_code, NULL, 0);
		break;

	case PC_to_RDR_IccPowerOff:
	case PC_to_RDR_GetSlotStatus:
	case PC_to_RDR_IccClock:
	case PC_to_RDR_T0APDU:
	case PC_to_RDR_Mechanical:
	case PC_to_RDR_Abort:
		/* Return RDR_to_PC_SlotStatus */
		resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
						err_code);
		break;

	case PC_to_RDR_GetParameters:
	case PC_to_RDR_ResetParameters:
	case PC_to_RDR_SetParameters:
		/* Return RDR_to_PC_Parameters */
		resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
						 err_code, NULL); /* FIXME: parameters? */
		break;

	case PC_to_RDR_Escape:
		/* Return RDR_to_PC_Escape */
		resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
					  err_code, NULL, 0);
		break;

	case PC_to_RDR_SetDataRateAndClockFrequency:
		/* Return RDR_to_PC_SlotStatus */
		resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
						err_code);
		break;

	default:
		/* generate general error */
		resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED,
						CCID_ERR_CMD_NOT_SUPPORTED);
		break;
	}
	return resp;
}

/***********************************************************************
 * Message reception / parsing
 ***********************************************************************/

/* Section 6.1.3 */
static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->get_slot_status.hdr.bSeq;
	struct msgb *resp;

	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);

	return ccid_slot_send_unbusy(cs, resp);
}


/* Section 6.1.1 */
static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->icc_power_on.hdr.bSeq;
	struct msgb *resp;

	/* TODO: send actual ATR; handle error cases */
	/* TODO: handle this asynchronously */
	resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0);

	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.2 */
static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->icc_power_off.hdr.bSeq;
	struct msgb *resp;

	/* FIXME */
	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.4 */
static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->xfr_block.hdr.bSeq;
	struct msgb *resp;

	/* FIXME */
	resp = ccid_gen_data_block(cs, seq, CCID_CMD_STATUS_OK, 0, NULL, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.5 */
static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->get_parameters.hdr.bSeq;
	struct msgb *resp;

	/* FIXME: T=1 */
	resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.6 */
static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->reset_parameters.hdr.bSeq;
	struct msgb *resp;

	/* FIXME: copy default parameters from somewhere */
	/* FIXME: T=1 */
	resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.7 */
static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters;
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->set_parameters.hdr.bSeq;
	struct ccid_pars_decoded pars_dec;
	struct msgb *resp;
	int rc;

	switch (spar->bProtocolNum) {
	case CCID_PROTOCOL_NUM_T0:
		rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0);
		if (rc < 0) {
			LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse T0: %d\n", rc);
			resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
			goto out;
		}
		/* FIXME: validate parameters; abort if they are not supported */
		cs->pars = pars_dec;
		resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0);
		break;
	case CCID_PROTOCOL_NUM_T1:
		rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1);
		if (rc < 0) {
			LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse T1: %d\n", rc);
			resp = ccid_gen_parameters_t1(cs, seq, CCID_CMD_STATUS_FAILED, -rc);
			goto out;
		}
		/* FIXME: validate parameters; abort if they are not supported */
		cs->pars = pars_dec;
		resp = ccid_gen_parameters_t1(cs, seq, CCID_CMD_STATUS_OK, 0);
		break;
	default:
		LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum);
		resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0);
		break;
	}
out:
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.8 */
static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->escape.hdr.bSeq;
	struct msgb *resp;

	resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.9 */
static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->icc_clock.hdr.bSeq;
	struct msgb *resp;

	/* FIXME: Actually Stop/Start the clock */
	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.10 */
static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->t0apdu.hdr.bSeq;
	struct msgb *resp;

	/* FIXME */
	//resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.11 */
static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->secure.hdr.bSeq;
	struct msgb *resp;

	/* FIXME */
	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.12 */
static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->mechanical.hdr.bSeq;
	struct msgb *resp;

	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.13 */
static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->abort.hdr.bSeq;
	struct msgb *resp;

	/* Check if the currently in-progress message is Abortable */
	switch (0/* FIXME */) {
	case PC_to_RDR_IccPowerOn:
	case PC_to_RDR_XfrBlock:
	case PC_to_RDR_Escape:
	case PC_to_RDR_Secure:
	case PC_to_RDR_Mechanical:
	//case PC_to_RDR_Abort: /* seriously? WTF! */
		break;
	default:
		LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n");
		/* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */
		resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED);
		return ccid_slot_send_unbusy(cs, resp);
	}

	/* FIXME */
	resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0);
	return ccid_slot_send_unbusy(cs, resp);
}

/* Section 6.1.14 */
static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	uint8_t seq = u->set_rate_and_clock.hdr.bSeq;
	struct msgb *resp;

	/* FIXME */
	resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, 9600, 2500000);
	return ccid_slot_send_unbusy(cs, resp);
}

/*! Handle data arriving from the host on the OUT endpoint.
 *  \param[in] cs CCID Instance on which to operate
 *  \param[in] msgb received message buffer containing one CCID OUT EP message from the host.
 *  		    Ownership of message buffer is transferred, i.e. it's our job to msgb_free()
 *  		    it eventually, after we're done with it (could be asynchronously).
 *  \returns 0 on success; negative on error */
int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg)
{
	const union ccid_pc_to_rdr *u = msgb_ccid_out(msg);
	const struct ccid_header *ch = (const struct ccid_header *) u;
	unsigned int len = msgb_length(msg);
	struct ccid_slot *cs;
	struct msgb *resp;
	int rc;

	if (len < sizeof(*ch)) {
		/* FIXME */
		return -1;
	}

	/* Check for invalid slot number */
	cs = get_ccid_slot(ci, ch->bSlot);
	if (!cs) {
		LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot);
		resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5);
		return ccid_send(ci, resp);
	}

	/* Check if slot is already busy; Reject any additional commands meanwhile */
	if (cs->cmd_busy) {
		LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n");
		/* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */
		resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq,
					CCID_ERR_CMD_SLOT_BUSY);
		return ccid_send(ci, resp);
	}

	LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n",
		get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg));

	/* we're now processing a command for the slot; mark slot as busy */
	cs->cmd_busy = true;

	/* TODO: enqueue into the per-slot specific input queue */

	switch (ch->bMessageType) {
	case PC_to_RDR_GetSlotStatus:
		if (len != sizeof(u->get_slot_status))
			goto short_msg;
		rc = ccid_handle_get_slot_status(cs, msg);
		break;
	case PC_to_RDR_IccPowerOn:
		if (len != sizeof(u->icc_power_on))
			goto short_msg;
		rc = ccid_handle_icc_power_on(cs, msg);
		break;
	case PC_to_RDR_IccPowerOff:
		if (len != sizeof(u->icc_power_off))
			goto short_msg;
		rc = ccid_handle_icc_power_off(cs, msg);
		break;
	case PC_to_RDR_XfrBlock:
		if (len < sizeof(u->xfr_block))
			goto short_msg;
		rc = ccid_handle_xfr_block(cs, msg);
		break;
	case PC_to_RDR_GetParameters:
		if (len != sizeof(u->get_parameters))
			goto short_msg;
		rc = ccid_handle_get_parameters(cs, msg);
		break;
	case PC_to_RDR_ResetParameters:
		if (len != sizeof(u->reset_parameters))
			goto short_msg;
		rc = ccid_handle_reset_parameters(cs, msg);
		break;
	case PC_to_RDR_SetParameters:
		if (len != sizeof(u->set_parameters))
			goto short_msg;
		rc = ccid_handle_set_parameters(cs, msg);
		break;
	case PC_to_RDR_Escape:
		if (len < sizeof(u->escape))
			goto short_msg;
		rc = ccid_handle_escape(cs, msg);
		break;
	case PC_to_RDR_IccClock:
		if (len != sizeof(u->icc_clock))
			goto short_msg;
		rc = ccid_handle_icc_clock(cs, msg);
		break;
	case PC_to_RDR_T0APDU:
		if (len != /*FIXME*/ sizeof(u->t0apdu))
			goto short_msg;
		rc = ccid_handle_t0apdu(cs, msg);
		break;
	case PC_to_RDR_Secure:
		if (len < sizeof(u->secure))
			goto short_msg;
		rc = ccid_handle_secure(cs, msg);
		break;
	case PC_to_RDR_Mechanical:
		if (len != sizeof(u->mechanical))
			goto short_msg;
		rc = ccid_handle_mechanical(cs, msg);
		break;
	case PC_to_RDR_Abort:
		if (len != sizeof(u->abort))
			goto short_msg;
		rc = ccid_handle_abort(cs, msg);
		break;
	case PC_to_RDR_SetDataRateAndClockFrequency:
		if (len != sizeof(u->set_rate_and_clock))
			goto short_msg;
		rc = ccid_handle_set_rate_and_clock(cs, msg);
		break;
	default:
		/* generic response with bERror = 0 (command not supported) */
		LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType);
		resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq,
				    CCID_ERR_CMD_NOT_SUPPORTED);
		return ccid_slot_send_unbusy(cs, resp);
	}
	return 0;

short_msg:
	LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg));
	return -1;
}

void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops, const char *name,
			void *priv)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ci->slot); i++) {
		struct ccid_slot *cs = &ci->slot[i];
		cs->slot_nr = i;
		cs->ci = ci;
	}
	ci->ops= ops;
	ci->name = name;
	ci->priv = priv;
}