xref: /illumos-gate/usr/src/common/ccid/atr.c (revision eb6b10e69fa5ba733da194d3ad71a0e63338be29)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019, Joyent, Inc.
14  */
15 
16 /*
17  * ATR parsing routines shared between userland (ccidadm) and the kernel (CCID
18  * driver)
19  */
20 
21 #include "atr.h"
22 #include <sys/debug.h>
23 #include <sys/sysmacros.h>
24 
25 #ifdef	_KERNEL
26 #include <sys/inttypes.h>
27 #include <sys/sunddi.h>
28 #include <sys/kmem.h>
29 #else
30 #include <inttypes.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #endif
36 
37 /*
38  * The ATR must have at least 2 bytes and then may have up to 33 bytes. The
39  * first byte is always TS and the second required byte is T0.
40  */
41 #define	ATR_TS_IDX	0
42 #define	ATR_T0_IDX	1
43 
44 /*
45  * There are two valid values for TS. It must either be 0x3F or 0x3B. This is
46  * required per ISO/IEC 7816-3:2006 section 8.1.
47  */
48 #define	ATR_TS_INVERSE	0x3F
49 #define	ATR_TS_DIRECT	0x3B
50 
51 /*
52  * After TS, each word is used to indicate a combination of protocol and the
53  * number of bits defined for that protocol. The lower nibble is treated as the
54  * protocol. The upper nibble is treated to indicate which of four defined words
55  * are present. These are usually referred to as TA, TB, TC, and TD. TD is
56  * always used to indicate the next protocol and the number of bytes present for
57  * that. T0 works in a similar way, except that it defines the number of
58  * historical bytes present in its protocol section and then it refers to a set
59  * of pre-defined global bytes that may be present.
60  */
61 #define	ATR_TD_PROT(x)	((x) & 0x0f)
62 #define	ATR_TD_NBITS(x)	(((x) & 0xf0) >> 4)
63 #define	ATR_TA_MASK	0x1
64 #define	ATR_TB_MASK	0x2
65 #define	ATR_TC_MASK	0x4
66 #define	ATR_TD_MASK	0x8
67 
68 #define	ATR_TA1_FTABLE(x)	(((x) & 0xf0) >> 4)
69 #define	ATR_TA1_DITABLE(x)	((x) & 0x0f)
70 
71 #define	ATR_TA2_CANCHANGE(x)	(((x) & 0x80) == 0)
72 #define	ATR_TA2_HONORTA1(x)	(((x) & 0x10) == 0)
73 #define	ATR_TA2_PROTOCOL(x)	((x) & 0x0f)
74 
75 /*
76  * When the checksum is required in the ATR, each byte must XOR to zero.
77  */
78 #define	ATR_CKSUM_TARGET	0
79 
80 /*
81  * Maximum number of historic ATR bytes. This is limited by the fact that it's a
82  * 4-bit nibble.
83  */
84 #define	ATR_HISTORICAL_MAX	15
85 
86 /*
87  * The maximum number of TA, TB, TC, and TD levels that can be encountered in a
88  * given structure. In the best case, there are 30 bytes available (TS, T0, and
89  * TCK use the others). Given that each one of these needs 4 bytes to be
90  * represented, the maximum number of layers that can fit is seven.
91  */
92 #define	ATR_TI_MAX	7
93 
94 /*
95  * Defined protocol values. See ISO/IEC 7816-3:2006 8.2.3 for this list.
96  * Reserved values are noted but not defined.
97  */
98 #define	ATR_PROTOCOL_T0		0
99 #define	ATR_PROTOCOL_T1		1
100 
101 #define	ATR_T1_TB0_CWI(x)	((x) & 0x0f)
102 #define	ATR_T1_TB0_BWI(x)	(((x) & 0xf0) >> 4)
103 #define	ATR_T1_TC0_CRC(x)	(((x) & 0x01) != 0)
104 
105 /*
106  * T=2 and T=3 are reserved for future full-duplex operation.
107  * T=4 is reserved for enhanced half-duplex character transmission.
108  * T=5-13 are reserved for future use by ISO/IEC JTC 1/SC 17.
109  * T=14 is for protocols not standardized by ISO/IEC JTC 1/SC 17.
110  */
111 #define	ATR_PROTOCOL_T15	15
112 
113 #define	ATR_T15_TA0_CLOCK(x)	(((x) & 0xc0) >> 6)
114 #define	ATR_T15_TA0_VOLTAGE(x)	((x) & 0x3f)
115 
116 #define	ATR_T15_TB0_SPU_STANDARD(x)	(((x & 0x80)) != 0)
117 
118 /*
119  * Various definitions for the configuration of historical data. This comes from
120  * ISO/IEC 7816-4:2013 Section 12.1.1.
121  */
122 
123 /*
124  * The first historical byte is used to indicate the encoding of the data. Only
125  * values 0x00, 0x80-0x8f are defined. All others are proprietary. 0x81-0x8f are
126  * reserved for future use.
127  */
128 #define	ATR_HIST_CAT_MAND_STATUS	0x00
129 #define	ATR_HIST_CAT_TLV_STATUS		0x80
130 #define	ATR_HIST_CAT_RFU_MIN		0x81
131 #define	ATR_HIST_CAT_RFU_MAX		0x8f
132 
133 /*
134  * From ISO/IEC 7816-3:2006 Section 8.3.
135  *
136  * The default value for Fi is 372 which is table entry 1. The default value for
137  * Di is 1, which is table entry 1.
138  */
139 #define	ATR_FI_DEFAULT_INDEX	1
140 #define	ATR_DI_DEFAULT_INDEX	1
141 #define	ATR_EXTRA_GUARDTIME_DEFAULT	0
142 
143 /*
144  * From ISO/IEC 7816-3:2006 Section 10.2.
145  */
146 #define	ATR_T0_WI_DEFAULT	10
147 
148 /*
149  * From ISO/IEC 7816-3:2006 Section 11.4.3.
150  */
151 #define	ATR_T1_CWI_DEFAULT	13
152 
153 /*
154  * From ISO/IEC 7816-3:2006 Section 11.4.3.
155  */
156 #define	ATR_T1_BWI_DEFAULT	4
157 
158 /*
159  * From ISO/IEC 7816-3:2006 Section 11.4.2.
160  */
161 #define	ATR_T1_IFSC_DEFAULT	32
162 
163 /*
164  * From ISO/IEC 7816-3:2006 Section 11.4.4
165  */
166 #define	ATR_T1_CHECKSUM_DEFAULT	ATR_T1_CHECKSUM_LRC
167 
168 /*
169  * Definitions for PPS construction. These are derived from ISO/IEC 7816-3:2006
170  * section 9, Protocol and parameters selection.
171  */
172 #define	PPS_LEN_MIN	3	/* PPSS, PPS0, PCK */
173 #define	PPS_LEN_MAX	PPS_BUFFER_MAX
174 #define	PPS_PPSS_INDEX	0
175 #define	PPS_PPSS_VAL	0xff
176 #define	PPS_PPS0_INDEX	0x01
177 #define	PPS_PPS0_PROT(x)	((x) & 0x0f)
178 #define	PPS_PPS0_PPS1		(1 << 4)
179 #define	PPS_PPS0_PPS2		(1 << 5)
180 #define	PPS_PPS0_PPS3		(1 << 6)
181 #define	PPS_PPS1_SETVAL(f, d)	((((f) & 0x0f) << 4) | ((d) & 0x0f))
182 
183 /*
184  * This enum and subsequent structure is used to represent a single level of
185  * 'T'. This includes the possibility for all three values to be set and records
186  * the protocol.
187  */
188 typedef enum atr_ti_flags {
189 	ATR_TI_HAVE_TA	= 1 << 0,
190 	ATR_TI_HAVE_TB	= 1 << 1,
191 	ATR_TI_HAVE_TC	= 1 << 2,
192 	ATR_TI_HAVE_TD	= 1 << 3
193 } atr_ti_flags_t;
194 
195 typedef struct atr_ti {
196 	uint8_t		atrti_protocol;
197 	uint8_t		atrti_ti_val;
198 	uint8_t		atrti_td_idx;
199 	atr_ti_flags_t	atrti_flags;
200 	uint8_t		atrti_ta;
201 	uint8_t		atrti_tb;
202 	uint8_t		atrti_tc;
203 	uint8_t		atrti_td;
204 } atr_ti_t;
205 
206 typedef enum atr_flags {
207 	ATR_F_USES_DIRECT	= 1 << 0,
208 	ATR_F_USES_INVERSE	= 1 << 1,
209 	ATR_F_HAS_CHECKSUM	= 1 << 2,
210 	ATR_F_VALID		= 1 << 3
211 } atr_flags_t;
212 
213 
214 struct atr_data {
215 	atr_flags_t	atr_flags;
216 	uint8_t		atr_nti;
217 	atr_ti_t	atr_ti[ATR_TI_MAX];
218 	uint8_t		atr_nhistoric;
219 	uint8_t		atr_historic[ATR_HISTORICAL_MAX];
220 	uint8_t		atr_cksum;
221 	uint8_t		atr_raw[ATR_LEN_MAX];
222 	uint8_t		atr_nraw;
223 };
224 
225 /*
226  * These tables maps the bit values for Fi from 7816-3:2006 section 8.3 Table 7.
227  */
228 static uint_t atr_fi_valtable[16] = {
229 	372,		/* 0000 */
230 	372,		/* 0001 */
231 	558,		/* 0010 */
232 	744,		/* 0011 */
233 	1116,		/* 0100 */
234 	1488,		/* 0101 */
235 	1860,		/* 0110 */
236 	0,		/* 0111 */
237 	0,		/* 1000 */
238 	512,		/* 1001 */
239 	768,		/* 1010 */
240 	1024,		/* 1011 */
241 	1536,		/* 1100 */
242 	2048,		/* 1101 */
243 	0,		/* 1110 */
244 	0		/* 1111 */
245 };
246 
247 static const char *atr_fi_table[16] = {
248 	"372",		/* 0000 */
249 	"372",		/* 0001 */
250 	"558",		/* 0010 */
251 	"744",		/* 0011 */
252 	"1116",		/* 0100 */
253 	"1488",		/* 0101 */
254 	"1860",		/* 0110 */
255 	"RFU",		/* 0111 */
256 	"RFU",		/* 1000 */
257 	"512",		/* 1001 */
258 	"768",		/* 1010 */
259 	"1024",		/* 1011 */
260 	"1536",		/* 1100 */
261 	"2048",		/* 1101 */
262 	"RFU",		/* 1110 */
263 	"RFU",		/* 1111 */
264 };
265 
266 /*
267  * This table maps the bit values for f(max) from 7816-3:2006 section 8.3
268  * Table 7.
269  */
270 static const char *atr_fmax_table[16] = {
271 	"4",		/* 0000 */
272 	"5",		/* 0001 */
273 	"6",		/* 0010 */
274 	"8",		/* 0011 */
275 	"12",		/* 0100 */
276 	"16",		/* 0101 */
277 	"20",		/* 0110 */
278 	"-",		/* 0111 */
279 	"-",		/* 1000 */
280 	"5",		/* 1001 */
281 	"7.5",		/* 1010 */
282 	"10",		/* 1011 */
283 	"15",		/* 1100 */
284 	"20",		/* 1101 */
285 	"-",		/* 1110 */
286 	"-",		/* 1111 */
287 };
288 
289 /*
290  * This table maps the bit values for Di from 7816-3:2006 section 8.3 Table 8.
291  */
292 static uint_t atr_di_valtable[16] = {
293 	0,		/* 0000 */
294 	1,		/* 0001 */
295 	2,		/* 0010 */
296 	4,		/* 0011 */
297 	8,		/* 0100 */
298 	16,		/* 0101 */
299 	32,		/* 0110 */
300 	64,		/* 0111 */
301 	12,		/* 1000 */
302 	20,		/* 1001 */
303 	0,		/* 1010 */
304 	0,		/* 1011 */
305 	0,		/* 1100 */
306 	0,		/* 1101 */
307 	0,		/* 1110 */
308 	0		/* 1111 */
309 };
310 
311 static const char *atr_di_table[16] = {
312 	"RFU",		/* 0000 */
313 	"1",		/* 0001 */
314 	"2",		/* 0010 */
315 	"4",		/* 0011 */
316 	"8",		/* 0100 */
317 	"16",		/* 0101 */
318 	"32",		/* 0110 */
319 	"64",		/* 0111 */
320 	"12",		/* 1000 */
321 	"20",		/* 1001 */
322 	"RFU",		/* 1010 */
323 	"RFU",		/* 1011 */
324 	"RFU",		/* 1100 */
325 	"RFU",		/* 1101 */
326 	"RFU",		/* 1110 */
327 	"RFU",		/* 1111 */
328 };
329 
330 /*
331  * This table maps the bit values for the clock stop indicator from 7816-3:2006
332  * section 8.3 Table 9.
333  */
334 static const char *atr_clock_table[4] = {
335 	"disallowed",		/* 00 */
336 	"signal low",		/* 01 */
337 	"signal high",		/* 10 */
338 	"signal low or high"	/* 11 */
339 };
340 
341 uint_t
342 atr_fi_index_to_value(uint8_t val)
343 {
344 	if (val >= ARRAY_SIZE(atr_fi_valtable)) {
345 		return (0);
346 	}
347 
348 	return (atr_fi_valtable[val]);
349 }
350 
351 const char *
352 atr_fi_index_to_string(uint8_t val)
353 {
354 	if (val >= ARRAY_SIZE(atr_fi_table)) {
355 		return ("<invalid>");
356 	}
357 
358 	return (atr_fi_table[val]);
359 }
360 
361 const char *
362 atr_fmax_index_to_string(uint8_t val)
363 {
364 	if (val >= ARRAY_SIZE(atr_fmax_table)) {
365 		return ("<invalid>");
366 	}
367 
368 	return (atr_fmax_table[val]);
369 }
370 
371 uint_t
372 atr_di_index_to_value(uint8_t val)
373 {
374 	if (val >= ARRAY_SIZE(atr_di_valtable)) {
375 		return (0);
376 	}
377 
378 	return (atr_di_valtable[val]);
379 }
380 const char *
381 atr_di_index_to_string(uint8_t val)
382 {
383 	if (val >= ARRAY_SIZE(atr_di_table)) {
384 		return ("<invalid>");
385 	}
386 
387 	return (atr_di_table[val]);
388 }
389 
390 const char *
391 atr_clock_stop_to_string(atr_clock_stop_t val)
392 {
393 	if (val >= ARRAY_SIZE(atr_clock_table)) {
394 		return ("<invalid>");
395 	}
396 
397 	return (atr_clock_table[val]);
398 }
399 
400 const char *
401 atr_protocol_to_string(atr_protocol_t prot)
402 {
403 	if (prot == ATR_P_NONE) {
404 		return ("none");
405 	}
406 
407 	if ((prot & ATR_P_T0) == ATR_P_T0) {
408 		return ("T=0");
409 	} else if ((prot & ATR_P_T1) == ATR_P_T1) {
410 		return ("T=1");
411 	} else {
412 		return ("T=0, T=1");
413 	}
414 }
415 
416 const char *
417 atr_convention_to_string(atr_convention_t conv)
418 {
419 	if (conv == ATR_CONVENTION_DIRECT) {
420 		return ("direct");
421 	} else if (conv == ATR_CONVENTION_INVERSE) {
422 		return ("inverse");
423 	} else {
424 		return ("<invalid convention>");
425 	}
426 }
427 
428 const char *
429 atr_strerror(atr_parsecode_t code)
430 {
431 	switch (code) {
432 	case ATR_CODE_OK:
433 		return ("ATR parsed successfully");
434 	case ATR_CODE_TOO_SHORT:
435 		return ("Specified buffer too short");
436 	case ATR_CODE_TOO_LONG:
437 		return ("Specified buffer too long");
438 	case ATR_CODE_INVALID_TS:
439 		return ("ATR has invalid TS byte value");
440 	case ATR_CODE_OVERRUN:
441 		return ("ATR data requires more bytes than provided");
442 	case ATR_CODE_UNDERRUN:
443 		return ("ATR data did not use all provided bytes");
444 	case ATR_CODE_CHECKSUM_ERROR:
445 		return ("ATR data did not checksum correctly");
446 	case ATR_CODE_INVALID_TD1:
447 		return ("ATR data has invalid protocol in TD1");
448 	default:
449 		return ("Unknown Parse Code");
450 	}
451 }
452 
453 static uint_t
454 atr_count_cbits(uint8_t x)
455 {
456 	uint_t ret = 0;
457 
458 	if (x & ATR_TA_MASK)
459 		ret++;
460 	if (x & ATR_TB_MASK)
461 		ret++;
462 	if (x & ATR_TC_MASK)
463 		ret++;
464 	if (x & ATR_TD_MASK)
465 		ret++;
466 	return (ret);
467 }
468 
469 /*
470  * Parse out ATR values. Focus on only parsing it and not interpreting it.
471  * Interpretation should be done in other functions that can walk over the data
472  * and be more protocol-aware.
473  */
474 atr_parsecode_t
475 atr_parse(const uint8_t *buf, size_t len, atr_data_t *data)
476 {
477 	uint_t nhist, cbits, ncbits, idx, Ti, prot;
478 	uint_t ncksum = 0;
479 	atr_ti_t *atp;
480 
481 	/*
482 	 * Zero out data in case someone's come back around for another loop on
483 	 * the same data.
484 	 */
485 	bzero(data, sizeof (atr_data_t));
486 
487 	if (len < ATR_LEN_MIN) {
488 		return (ATR_CODE_TOO_SHORT);
489 	}
490 
491 	if (len > ATR_LEN_MAX) {
492 		return (ATR_CODE_TOO_LONG);
493 	}
494 
495 	if (buf[ATR_TS_IDX] != ATR_TS_INVERSE &&
496 	    buf[ATR_TS_IDX] != ATR_TS_DIRECT) {
497 		return (ATR_CODE_INVALID_TS);
498 	}
499 
500 	bcopy(buf, data->atr_raw, len);
501 	data->atr_nraw = len;
502 
503 	if (buf[ATR_TS_IDX] == ATR_TS_DIRECT) {
504 		data->atr_flags |= ATR_F_USES_DIRECT;
505 	} else {
506 		data->atr_flags |= ATR_F_USES_INVERSE;
507 	}
508 
509 	/*
510 	 * The protocol of T0 is the number of historical bits present.
511 	 */
512 	nhist = ATR_TD_PROT(buf[ATR_T0_IDX]);
513 	cbits = ATR_TD_NBITS(buf[ATR_T0_IDX]);
514 	idx = ATR_T0_IDX + 1;
515 	ncbits = atr_count_cbits(cbits);
516 
517 	/*
518 	 * Ti is used to track the current iteration of T[A,B,C,D] that we are
519 	 * on, as the ISO/IEC standard suggests. The way that values are
520 	 * interpreted depends on the value of Ti.
521 	 *
522 	 * When Ti is one, TA, TB, and TC represent global properties. TD's
523 	 * protocol represents the preferred protocol.
524 	 *
525 	 * When Ti is two, TA, TB, and TC also represent global properties.
526 	 * However, TC only has meaning if the protocol is T=0.
527 	 *
528 	 * When Ti is 15, it indicates more global properties.
529 	 *
530 	 * For all other values of Ti, the meaning depends on the protocol in
531 	 * question and they are all properties specific to that protocol.
532 	 */
533 	Ti = 1;
534 	/*
535 	 * Initialize prot to an invalid protocol to help us deal with the
536 	 * normal workflow and make sure that we don't mistakenly do anything.
537 	 */
538 	prot = UINT32_MAX;
539 	for (;;) {
540 		atp = &data->atr_ti[data->atr_nti];
541 		data->atr_nti++;
542 		ASSERT3U(data->atr_nti, <=, ATR_TI_MAX);
543 
544 		/*
545 		 * Make sure that we have enough space to read all the cbits.
546 		 * idx points to the first cbit, which could also potentially be
547 		 * over the length of the buffer. This is why we subtract one
548 		 * from idx when doing the calculation.
549 		 */
550 		if (idx - 1 + ncbits >= len) {
551 			return (ATR_CODE_OVERRUN);
552 		}
553 
554 		ASSERT3U(Ti, !=, 0);
555 
556 		/*
557 		 * At the moment we opt to ignore reserved protocols.
558 		 */
559 		atp->atrti_protocol = prot;
560 		atp->atrti_ti_val = Ti;
561 		atp->atrti_td_idx = idx - 1;
562 
563 		if (cbits & ATR_TA_MASK) {
564 			atp->atrti_flags |= ATR_TI_HAVE_TA;
565 			atp->atrti_ta = buf[idx];
566 			idx++;
567 		}
568 
569 		if (cbits & ATR_TB_MASK) {
570 			atp->atrti_flags |= ATR_TI_HAVE_TB;
571 			atp->atrti_tb = buf[idx];
572 			idx++;
573 		}
574 
575 		if (cbits & ATR_TC_MASK) {
576 			atp->atrti_flags |= ATR_TI_HAVE_TC;
577 			atp->atrti_tc = buf[idx];
578 			idx++;
579 		}
580 
581 		if (cbits & ATR_TD_MASK) {
582 			atp->atrti_flags |= ATR_TI_HAVE_TD;
583 			atp->atrti_td = buf[idx];
584 			cbits = ATR_TD_NBITS(buf[idx]);
585 			prot = ATR_TD_PROT(buf[idx]);
586 			ncbits = atr_count_cbits(cbits);
587 			if (prot != 0)
588 				ncksum = 1;
589 
590 			/*
591 			 * T=15 is not allowed in TD1 per 8.2.3.
592 			 */
593 			if (Ti == 1 && prot == 0xf)
594 				return (ATR_CODE_INVALID_TD1);
595 
596 			idx++;
597 			/*
598 			 * Encountering TD means that once we take the next loop
599 			 * and we need to increment Ti.
600 			 */
601 			Ti++;
602 		} else {
603 			break;
604 		}
605 	}
606 
607 	/*
608 	 * We've parsed all of the cbits. At this point, we should take into
609 	 * account all of the historical bits and potentially the checksum.
610 	 */
611 	if (idx - 1 + nhist + ncksum >= len) {
612 		return (ATR_CODE_OVERRUN);
613 	}
614 
615 	if (idx + nhist + ncksum != len) {
616 		return (ATR_CODE_UNDERRUN);
617 	}
618 
619 	if (nhist > 0) {
620 		data->atr_nhistoric = nhist;
621 		bcopy(&buf[idx], data->atr_historic, nhist);
622 	}
623 
624 	if (ncksum > 0) {
625 		size_t i;
626 		uint8_t val;
627 
628 		/*
629 		 * Per ISO/IEC 7816-3:2006 Section 8.2.5 the checksum is all
630 		 * bytes excluding TS. Therefore, we must start at byte 1.
631 		 */
632 		for (val = 0, i = 1; i < len; i++) {
633 			val ^= buf[i];
634 		}
635 
636 		if (val != ATR_CKSUM_TARGET) {
637 			return (ATR_CODE_CHECKSUM_ERROR);
638 		}
639 		data->atr_flags |= ATR_F_HAS_CHECKSUM;
640 		data->atr_cksum = buf[len - 1];
641 	}
642 
643 	data->atr_flags |= ATR_F_VALID;
644 	return (ATR_CODE_OK);
645 }
646 
647 uint8_t
648 atr_fi_default_index(void)
649 {
650 	return (ATR_FI_DEFAULT_INDEX);
651 }
652 
653 uint8_t
654 atr_di_default_index(void)
655 {
656 	return (ATR_DI_DEFAULT_INDEX);
657 }
658 
659 /*
660  * Parse the data to determine which protocols are supported in this atr data.
661  * Based on this, users can come and ask us to fill in protocol information.
662  */
663 atr_protocol_t
664 atr_supported_protocols(atr_data_t *data)
665 {
666 	uint_t i;
667 	atr_protocol_t prot;
668 
669 	if ((data->atr_flags & ATR_F_VALID) == 0)
670 		return (ATR_P_NONE);
671 
672 	/*
673 	 * Based on 8.2.3 of ISO/IEC 7816-3:2006, if TD1 is present, then that
674 	 * indicates the first protocol. However, if it is not present, then
675 	 * that implies that T=0 is the only supported protocol. Otherwise, all
676 	 * protocols are referenced in ascending order. The first entry in
677 	 * atr_ti refers to data from T0, so the protocol in the second entry
678 	 * would have the TD1 data.
679 	 */
680 	if (data->atr_nti < 2) {
681 		return (ATR_P_T0);
682 	}
683 
684 	prot = ATR_P_NONE;
685 	for (i = 0; i < data->atr_nti; i++) {
686 		switch (data->atr_ti[i].atrti_protocol) {
687 		case ATR_PROTOCOL_T0:
688 			prot |= ATR_P_T0;
689 			break;
690 		case ATR_PROTOCOL_T1:
691 			prot |= ATR_P_T1;
692 			break;
693 		default:
694 			/*
695 			 * T=15 is not a protocol, and all other protocol values
696 			 * are currently reserved for future use.
697 			 */
698 			continue;
699 		}
700 	}
701 
702 	/*
703 	 * It's possible we've found nothing specific in the above loop (for
704 	 * example, only T=15 global bits were found). In that case, the card
705 	 * defaults to T=0.
706 	 */
707 	if (prot == ATR_P_NONE)
708 		prot = ATR_P_T0;
709 
710 	return (prot);
711 }
712 
713 boolean_t
714 atr_params_negotiable(atr_data_t *data)
715 {
716 	/* If for some reason we're called with invalid data, assume it's not */
717 	if ((data->atr_flags & ATR_F_VALID) == 0)
718 		return (B_FALSE);
719 
720 
721 	/*
722 	 * Whether or not we're negotiable is in the second global page, so atr
723 	 * index 1. If TA2 is missing, then the card always is negotiable.
724 	 */
725 	if (data->atr_nti < 2 ||
726 	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) == 0) {
727 		return (B_TRUE);
728 	}
729 
730 	if (ATR_TA2_CANCHANGE(data->atr_ti[1].atrti_ta)) {
731 		return (B_TRUE);
732 	}
733 
734 	return (B_FALSE);
735 }
736 
737 atr_protocol_t
738 atr_default_protocol(atr_data_t *data)
739 {
740 	uint8_t prot;
741 
742 	if ((data->atr_flags & ATR_F_VALID) == 0)
743 		return (ATR_P_NONE);
744 	/*
745 	 * If we don't have an TA2 byte, then the system defaults to T=0.
746 	 */
747 	if (data->atr_nti < 2) {
748 		return (ATR_P_T0);
749 	}
750 
751 	/*
752 	 * If TA2 is present, then it encodes the default protocol. Otherwise,
753 	 * we have to grab the protocol value from TD1, which is called the
754 	 * 'first offered protocol'.
755 	 */
756 	if ((data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) {
757 		prot = ATR_TA2_PROTOCOL(data->atr_ti[1].atrti_ta);
758 	} else {
759 		prot = data->atr_ti[1].atrti_protocol;
760 	}
761 
762 	switch (prot) {
763 	case ATR_PROTOCOL_T0:
764 		return (ATR_P_T0);
765 	case ATR_PROTOCOL_T1:
766 		return (ATR_P_T1);
767 	default:
768 		return (ATR_P_NONE);
769 	}
770 }
771 
772 uint8_t
773 atr_fi_index(atr_data_t *data)
774 {
775 	if (data->atr_nti < 1) {
776 		return (ATR_FI_DEFAULT_INDEX);
777 	}
778 
779 	/*
780 	 * If TA is specified, it is present in TA1. TA2 may override its
781 	 * presence, so if it is here, check that first to determine whether or
782 	 * not we should check TA1.
783 	 */
784 	if (data->atr_nti >= 2 &&
785 	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) {
786 		if (!ATR_TA2_HONORTA1(data->atr_ti[1].atrti_ta)) {
787 			return (ATR_FI_DEFAULT_INDEX);
788 		}
789 	}
790 
791 	if ((data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TA) != 0) {
792 		return (ATR_TA1_FTABLE(data->atr_ti[0].atrti_ta));
793 	}
794 
795 	return (ATR_FI_DEFAULT_INDEX);
796 }
797 
798 uint8_t
799 atr_di_index(atr_data_t *data)
800 {
801 	if (data->atr_nti < 1) {
802 		return (ATR_DI_DEFAULT_INDEX);
803 	}
804 
805 	/*
806 	 * If TA is specified, it is present in TA1. TA2 may override its
807 	 * presence, so if it is here, check that first to determine whether or
808 	 * not we should check TA1.
809 	 */
810 	if (data->atr_nti >= 2 &&
811 	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TA) != 0) {
812 		if (!ATR_TA2_HONORTA1(data->atr_ti[1].atrti_ta)) {
813 			return (ATR_DI_DEFAULT_INDEX);
814 		}
815 	}
816 
817 	if ((data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TA) != 0) {
818 		return (ATR_TA1_DITABLE(data->atr_ti[0].atrti_ta));
819 	}
820 
821 	return (ATR_DI_DEFAULT_INDEX);
822 }
823 
824 atr_convention_t
825 atr_convention(atr_data_t *data)
826 {
827 	if ((data->atr_flags & ATR_F_USES_DIRECT) != 0) {
828 		return (ATR_CONVENTION_DIRECT);
829 	}
830 	return (ATR_CONVENTION_INVERSE);
831 }
832 
833 uint8_t
834 atr_extra_guardtime(atr_data_t *data)
835 {
836 	if ((data->atr_flags & ATR_F_VALID) == 0)
837 		return (ATR_EXTRA_GUARDTIME_DEFAULT);
838 
839 	if (data->atr_nti >= 1 &&
840 	    (data->atr_ti[0].atrti_flags & ATR_TI_HAVE_TC) != 0) {
841 		return (data->atr_ti[0].atrti_tc);
842 	}
843 
844 	return (ATR_EXTRA_GUARDTIME_DEFAULT);
845 }
846 
847 uint8_t
848 atr_t0_wi(atr_data_t *data)
849 {
850 	if ((data->atr_flags & ATR_F_VALID) == 0)
851 		return (ATR_T0_WI_DEFAULT);
852 
853 	/*
854 	 * This is stored in the optional global byte in TC2; however, it only
855 	 * applies to T=0.
856 	 */
857 	if (data->atr_nti >= 2 &&
858 	    data->atr_ti[1].atrti_protocol == ATR_PROTOCOL_T0 &&
859 	    (data->atr_ti[1].atrti_flags & ATR_TI_HAVE_TC) != 0) {
860 		return (data->atr_ti[1].atrti_tc);
861 	}
862 
863 	return (ATR_T0_WI_DEFAULT);
864 }
865 
866 uint8_t
867 atr_t1_cwi(atr_data_t *data)
868 {
869 	uint8_t i;
870 
871 	if (data->atr_nti <= 2) {
872 		return (ATR_T1_CWI_DEFAULT);
873 	}
874 
875 	for (i = 2; i < data->atr_nti; i++) {
876 		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
877 			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TB) !=
878 			    0) {
879 				uint8_t tb = data->atr_ti[i].atrti_tb;
880 				return (ATR_T1_TB0_CWI(tb));
881 			}
882 
883 			return (ATR_T1_CWI_DEFAULT);
884 		}
885 	}
886 
887 	return (ATR_T1_CWI_DEFAULT);
888 }
889 
890 atr_clock_stop_t
891 atr_clock_stop(atr_data_t *data)
892 {
893 	uint8_t i;
894 
895 	for (i = 0; i < data->atr_nti; i++) {
896 		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T15) {
897 			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TA) !=
898 			    0) {
899 				uint8_t ta = data->atr_ti[i].atrti_ta;
900 				return (ATR_T15_TA0_CLOCK(ta));
901 			}
902 
903 			return (ATR_CLOCK_STOP_NONE);
904 		}
905 	}
906 
907 	return (ATR_CLOCK_STOP_NONE);
908 }
909 
910 atr_t1_checksum_t
911 atr_t1_checksum(atr_data_t *data)
912 {
913 	uint8_t i;
914 
915 	if (data->atr_nti <= 2) {
916 		return (ATR_T1_CHECKSUM_DEFAULT);
917 	}
918 
919 	for (i = 2; i < data->atr_nti; i++) {
920 		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
921 			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TC) !=
922 			    0) {
923 				if (ATR_T1_TC0_CRC(data->atr_ti[i].atrti_tc)) {
924 					return (ATR_T1_CHECKSUM_CRC);
925 				} else {
926 					return (ATR_T1_CHECKSUM_LRC);
927 				}
928 			}
929 
930 			return (ATR_T1_CHECKSUM_DEFAULT);
931 		}
932 	}
933 
934 	return (ATR_T1_CHECKSUM_DEFAULT);
935 
936 }
937 
938 uint8_t
939 atr_t1_bwi(atr_data_t *data)
940 {
941 	uint8_t i;
942 
943 	if (data->atr_nti <= 2) {
944 		return (ATR_T1_BWI_DEFAULT);
945 	}
946 
947 	for (i = 2; i < data->atr_nti; i++) {
948 		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
949 			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TB) !=
950 			    0) {
951 				uint8_t tb = data->atr_ti[i].atrti_tb;
952 				return (ATR_T1_TB0_BWI(tb));
953 			}
954 
955 			return (ATR_T1_BWI_DEFAULT);
956 		}
957 	}
958 
959 	return (ATR_T1_BWI_DEFAULT);
960 }
961 
962 uint8_t
963 atr_t1_ifsc(atr_data_t *data)
964 {
965 	uint8_t i;
966 
967 	if (data->atr_nti <= 2) {
968 		return (ATR_T1_IFSC_DEFAULT);
969 	}
970 
971 	for (i = 2; i < data->atr_nti; i++) {
972 		if (data->atr_ti[i].atrti_protocol == ATR_PROTOCOL_T1) {
973 			if ((data->atr_ti[i].atrti_flags & ATR_TI_HAVE_TA) !=
974 			    0) {
975 				return (data->atr_ti[i].atrti_ta);
976 			}
977 
978 			return (ATR_T1_IFSC_DEFAULT);
979 		}
980 	}
981 
982 	return (ATR_T1_IFSC_DEFAULT);
983 }
984 
985 /*
986  * Attempt to determine which set of data rates we should be able to use for a
987  * given class of protocol. Here we want to do the calculation based on the CCID
988  * specification, section 9.4.x. To use these higher rates we need:
989  *
990  * + Reader's data rate > frequency * Di / Fi.
991  *
992  * To determine which rate and frequency we use, we look at the reader's
993  * features. If the reader supports both the Automatic baud rate and automatic
994  * ICC clock frequency change, then we use the _maximum_ rate. Otherwise we will
995  * indicate that we can use the ATR's properties, but will require changing the
996  * default data rate.
997  *
998  * Now, some ICC devices are not negotiable. In those cases, we'll see if we can
999  * fit it in with either the default or maximum data rates. If not, then we'll
1000  * not be able to support this card.
1001  *
1002  * There are two wrinkles that exist in this. The first is supported frequencies
1003  * and data rates. If there are no additional data rates supported, then all of
1004  * the data rates between the default and max are supported. If not, then only
1005  * those specified in the data rates array are supported.
1006  *
1007  * The second hurdle is that we need to do this division and try and avoid the
1008  * pitfalls of floating point arithmetic, as floating point is not allowed in
1009  * the kernel (and this is shared). Importantly that means only integers are
1010  * allowed here.
1011  */
1012 atr_data_rate_choice_t
1013 atr_data_rate(atr_data_t *data, ccid_class_descr_t *class, uint32_t *rates,
1014     uint_t nrates, uint32_t *dataratep)
1015 {
1016 	uint_t nfeats = CCID_CLASS_F_AUTO_ICC_CLOCK | CCID_CLASS_F_AUTO_BAUD;
1017 	uint8_t di, fi;
1018 	uint_t dival, fival;
1019 	boolean_t autospeed, negotiable, exprates;
1020 	uint64_t maxval, defval;
1021 
1022 	if ((data->atr_flags & ATR_F_VALID) == 0)
1023 		return (ATR_RATE_UNSUPPORTED);
1024 
1025 	di = atr_di_index(data);
1026 	fi = atr_fi_index(data);
1027 	dival = atr_di_index_to_value(di);
1028 	fival = atr_fi_index_to_value(fi);
1029 	autospeed = (class->ccd_dwFeatures & nfeats) == nfeats;
1030 	exprates = class->ccd_bNumDataRatesSupported != 0;
1031 	negotiable = atr_params_negotiable(data);
1032 
1033 	/*
1034 	 * We don't support cards with fixed rates at this time as it's not
1035 	 * clear what that rate should be. If it's negotiable, we'll let them
1036 	 * run at the default. Otherwise, we have to fail the request until
1037 	 * we implement the logic to search their data rates.
1038 	 */
1039 	if (exprates) {
1040 		if (negotiable) {
1041 			return (ATR_RATE_USEDEFAULT);
1042 		}
1043 		return (ATR_RATE_UNSUPPORTED);
1044 	}
1045 
1046 	/*
1047 	 * This indicates that the card gave us values that were reserved for
1048 	 * future use. If we could negotiate it, then just stick with the
1049 	 * default paramters. Otherwise, return that we can't support this ICC.
1050 	 */
1051 	if (dival == 0 || fival == 0) {
1052 		if (negotiable)
1053 			return (ATR_RATE_USEDEFAULT);
1054 		return (ATR_RATE_UNSUPPORTED);
1055 	}
1056 
1057 	/*
1058 	 * Calculate the maximum and default values.
1059 	 */
1060 	maxval = class->ccd_dwMaximumClock * 1000;
1061 	maxval *= dival;
1062 	maxval /= fival;
1063 
1064 	defval = class->ccd_dwDefaultClock * 1000;
1065 	defval *= dival;
1066 	defval /= fival;
1067 
1068 	/*
1069 	 * We're allowed any set of data rates between the default and the
1070 	 * maximum. Check if the maximum data rate will work for either the
1071 	 * default or maximum clock. If so, then we can use the cards rates.
1072 	 *
1073 	 * To account for the fact that we may have had a fractional value,
1074 	 * we require a strict greater than comparison.
1075 	 */
1076 	if ((uint64_t)class->ccd_dwMaxDataRate > maxval ||
1077 	    (uint64_t)class->ccd_dwMaxDataRate > defval) {
1078 		if (autospeed) {
1079 			return (ATR_RATE_USEATR);
1080 		}
1081 	}
1082 
1083 	/*
1084 	 * If the CCID reader can't handle the ICC's proposed rates, then fall
1085 	 * back to the defaults if we're allowed to negotiate. Otherwise, we're
1086 	 * not able to use this ICC.
1087 	 */
1088 	if (negotiable) {
1089 		return (ATR_RATE_USEDEFAULT);
1090 	}
1091 
1092 	return (ATR_RATE_UNSUPPORTED);
1093 }
1094 
1095 void
1096 atr_data_reset(atr_data_t *data)
1097 {
1098 	bzero(data, sizeof (*data));
1099 }
1100 
1101 #ifdef	_KERNEL
1102 atr_data_t *
1103 atr_data_alloc(void)
1104 {
1105 	return (kmem_zalloc(sizeof (atr_data_t), KM_SLEEP));
1106 }
1107 
1108 void
1109 atr_data_free(atr_data_t *data)
1110 {
1111 	kmem_free(data, sizeof (atr_data_t));
1112 }
1113 
1114 /*
1115  * Make sure that the response we got from the ICC is valid. It must pass
1116  * checksum and have the PPSS value set correctly. The protocol must match
1117  * what we requested; however, the PPS1-3 bits are a bit different. They may
1118  * only be set in the response if we set them in the request. However, they
1119  * do not have to be set in the response.
1120  */
1121 boolean_t
1122 atr_pps_valid(void *reqbuf, size_t reqlen, void *respbuf, size_t resplen)
1123 {
1124 	uint8_t val, i, reqidx, respidx;
1125 	uint8_t *req = reqbuf, *resp = respbuf;
1126 
1127 	if (resplen > PPS_LEN_MAX || resplen < PPS_LEN_MIN)
1128 		return (B_FALSE);
1129 
1130 	/*
1131 	 * Before we validate the data, make sure the checksum is valid.
1132 	 */
1133 	for (i = 0, val = 0; i < resplen; i++) {
1134 		val ^= resp[i];
1135 	}
1136 
1137 	/* Checksum failure */
1138 	if (val != 0) {
1139 		return (B_FALSE);
1140 	}
1141 
1142 	/*
1143 	 * We should always have PPSS echoed back as we set it.
1144 	 */
1145 	if (resp[PPS_PPSS_INDEX] != PPS_PPSS_VAL) {
1146 		return (B_FALSE);
1147 	}
1148 
1149 	/*
1150 	 * Go through and make sure the number of bytes present makes sense for
1151 	 * the number of bits set in PPS1.
1152 	 */
1153 	val = PPS_LEN_MIN;
1154 	if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1)
1155 		val++;
1156 	if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS2)
1157 		val++;
1158 	if (resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS3)
1159 		val++;
1160 	if (val != resplen)
1161 		return (B_FALSE);
1162 
1163 	/*
1164 	 * Now we've finally verified that the response is syntactically valid.
1165 	 * We must go through and make sure that it is semantically valid.
1166 	 */
1167 	if (PPS_PPS0_PROT(req[PPS_PPS0_INDEX]) !=
1168 	    PPS_PPS0_PROT(resp[PPS_PPS0_INDEX])) {
1169 		return (B_FALSE);
1170 	}
1171 
1172 	/*
1173 	 * When checking the PPS bit and extensions, we first check in the
1174 	 * response as a bit in the request is allowed to not be in the
1175 	 * response. But not the opposite way around. We also have to keep track
1176 	 * of the fact that the index for values will vary.
1177 	 */
1178 	reqidx = respidx = PPS_PPS0_INDEX + 1;
1179 	if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0) {
1180 		if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) == 0) {
1181 			return (B_FALSE);
1182 		}
1183 
1184 		if (req[reqidx] != resp[respidx]) {
1185 			return (B_FALSE);
1186 		}
1187 
1188 		reqidx++;
1189 		respidx++;
1190 	} else if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0) {
1191 		reqidx++;
1192 	}
1193 
1194 	if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) != 0) {
1195 		if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) == 0) {
1196 			return (B_FALSE);
1197 		}
1198 
1199 		if (req[reqidx] != resp[respidx]) {
1200 			return (B_FALSE);
1201 		}
1202 
1203 		reqidx++;
1204 		respidx++;
1205 	} else if ((req[PPS_PPS0_INDEX] & PPS_PPS0_PPS2) != 0) {
1206 		reqidx++;
1207 	}
1208 
1209 	if ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS3) != 0) {
1210 		/*
1211 		 * At this time, we never specify PPS3 in a request. Therefore
1212 		 * if it is present in the response, treat this as an invalid
1213 		 * request.
1214 		 */
1215 		return (B_FALSE);
1216 	}
1217 
1218 	return (B_TRUE);
1219 }
1220 
1221 uint_t
1222 atr_pps_generate(uint8_t *buf, size_t buflen, atr_protocol_t prot,
1223     boolean_t pps1, uint8_t fi, uint8_t di, boolean_t pps2, uint8_t spu)
1224 {
1225 	uint8_t protval, cksum, i;
1226 	uint_t len = 0;
1227 
1228 	if (buflen < PPS_BUFFER_MAX)
1229 		return (0);
1230 
1231 	buf[PPS_PPSS_INDEX] = PPS_PPSS_VAL;
1232 	switch (prot) {
1233 	case ATR_P_T0:
1234 		protval = 0;
1235 		break;
1236 	case ATR_P_T1:
1237 		protval = 1;
1238 		break;
1239 	default:
1240 		return (0);
1241 	}
1242 
1243 	buf[PPS_PPS0_INDEX] = PPS_PPS0_PROT(protval);
1244 	len = 2;
1245 	if (pps1) {
1246 		buf[PPS_PPS0_INDEX] |= PPS_PPS0_PPS1;
1247 		buf[len++] = PPS_PPS1_SETVAL(fi, di);
1248 	}
1249 
1250 	if (pps2) {
1251 		buf[PPS_PPS0_INDEX] |= PPS_PPS0_PPS2;
1252 		buf[len++] = spu;
1253 	}
1254 
1255 	/*
1256 	 * The checksum must xor to zero.
1257 	 */
1258 	for (i = 0, cksum = 0; i < len; i++) {
1259 		cksum ^= buf[i];
1260 	}
1261 	buf[len++] = cksum;
1262 	return (len);
1263 }
1264 
1265 /*
1266  * The caller of this wants to know if the Fi/Di values that they proposed were
1267  * accepted. The caller must have already called atr_pps_valid(). At this point,
1268  * we can say that the value was accepted if the PPS1 bit is set.
1269  */
1270 boolean_t
1271 atr_pps_fidi_accepted(void *respbuf, size_t len)
1272 {
1273 	uint8_t *resp = respbuf;
1274 	return ((resp[PPS_PPS0_INDEX] & PPS_PPS0_PPS1) != 0);
1275 }
1276 
1277 #else	/* !_KERNEL */
1278 atr_data_t *
1279 atr_data_alloc(void)
1280 {
1281 	return (calloc(1, sizeof (atr_data_t)));
1282 }
1283 
1284 void
1285 atr_data_free(atr_data_t *data)
1286 {
1287 	if (data == NULL)
1288 		return;
1289 	free(data);
1290 }
1291 
1292 /*
1293  * This table maps the bit values for Fi from 7816-3:2006 section 8.3 Table 9.
1294  * The table is up to 6 bits wide. Entries not present are RFU. We use NULL as a
1295  * sentinel to indicate that.
1296  */
1297 static const char *atr_voltage_table[64] = {
1298 	NULL,			/* 00 0000 */
1299 	"5V",			/* 00 0001 */
1300 	"3V",			/* 00 0010 */
1301 	"5V, 3V",		/* 00 0011 */
1302 	"1.5V",			/* 00 0100 */
1303 	NULL,			/* 00 0101 */
1304 	"3V, 1.5V",		/* 00 0110 */
1305 	"5V, 3V, 1.5V"		/* 00 0111 */
1306 };
1307 
1308 static void
1309 atr_data_dump_ta(atr_ti_t *atp, FILE *out, uint_t level)
1310 {
1311 	uint8_t ta;
1312 
1313 	if (!(atp->atrti_flags & ATR_TI_HAVE_TA)) {
1314 		return;
1315 	}
1316 
1317 	ta = atp->atrti_ta;
1318 	(void) fprintf(out, "   %c%c%c+-> TA%u 0x%02x",
1319 	    atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ',
1320 	    atp->atrti_flags & ATR_TI_HAVE_TC ? '|' : ' ',
1321 	    atp->atrti_flags & ATR_TI_HAVE_TB ? '|' : ' ',
1322 	    atp->atrti_ti_val, ta);
1323 	switch (atp->atrti_ti_val) {
1324 	case 1:
1325 		(void) fprintf(out, "; Fi: %s, F(max): %s MHz, Di: %s",
1326 		    atr_fi_table[ATR_TA1_FTABLE(ta)],
1327 		    atr_fmax_table[ATR_TA1_FTABLE(ta)],
1328 		    atr_di_table[ATR_TA1_DITABLE(ta)]);
1329 		break;
1330 	case 2:
1331 		(void) fprintf(out, "; ICC in %s mode; %shonoring TA1; default "
1332 		    "T=%u",
1333 		    ATR_TA2_CANCHANGE(ta) ? "negotiable" : "specific",
1334 		    ATR_TA2_HONORTA1(ta) ? "" : "not ",
1335 		    ATR_TA2_PROTOCOL(ta));
1336 		break;
1337 	default:
1338 		switch (atp->atrti_protocol) {
1339 		case ATR_PROTOCOL_T1:
1340 			if (level != 0)
1341 				break;
1342 			if (ta == 0 || ta == 0xff) {
1343 				(void) fprintf(out, "; IFSC: RFU");
1344 			} else {
1345 				(void) fprintf(out, "; IFSC: %u", ta);
1346 			}
1347 			break;
1348 		case ATR_PROTOCOL_T15:
1349 			if (level != 0)
1350 				break;
1351 			(void) fprintf(out, "; Clock stop: %s, Supported "
1352 			    "Voltage: %s",
1353 			    atr_clock_table[ATR_T15_TA0_CLOCK(ta)],
1354 			    atr_voltage_table[ATR_T15_TA0_VOLTAGE(ta)] != NULL ?
1355 			    atr_voltage_table[ATR_T15_TA0_VOLTAGE(ta)] : "RFU");
1356 			break;
1357 		default:
1358 			break;
1359 		}
1360 	}
1361 	(void) fprintf(out, "\n");
1362 }
1363 
1364 static void
1365 atr_data_dump_tb(atr_ti_t *atp, FILE *out, uint_t level)
1366 {
1367 	uint8_t tb;
1368 
1369 	if (!(atp->atrti_flags & ATR_TI_HAVE_TB)) {
1370 		return;
1371 	}
1372 
1373 	tb = atp->atrti_tb;
1374 	(void) fprintf(out, "   %c%c+--> TB%u 0x%02x",
1375 	    atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ',
1376 	    atp->atrti_flags & ATR_TI_HAVE_TC ? '|' : ' ',
1377 	    atp->atrti_ti_val, tb);
1378 	switch (atp->atrti_ti_val) {
1379 	case 1:
1380 	case 2:
1381 		(void) fprintf(out, "; deprecated");
1382 		break;
1383 	default:
1384 		switch (atp->atrti_protocol) {
1385 		case ATR_PROTOCOL_T1:
1386 			if (level != 0)
1387 				break;
1388 			(void) fprintf(out, "; CWI: %u, BWI: %u\n",
1389 			    ATR_T1_TB0_CWI(tb),
1390 			    ATR_T1_TB0_BWI(tb));
1391 			break;
1392 		case ATR_PROTOCOL_T15:
1393 			if (level != 0)
1394 				break;
1395 			(void) fprintf(out, "; SPU: %s", tb == 0 ? "not used" :
1396 			    ATR_T15_TB0_SPU_STANDARD(tb) ? "standard" :
1397 			    "proprietary");
1398 			break;
1399 		default:
1400 			break;
1401 		}
1402 	}
1403 	(void) fprintf(out, "\n");
1404 }
1405 
1406 static void
1407 atr_data_dump_tc(atr_ti_t *atp, FILE *out, uint_t level)
1408 {
1409 	uint8_t tc;
1410 
1411 	if (!(atp->atrti_flags & ATR_TI_HAVE_TC)) {
1412 		return;
1413 	}
1414 
1415 	tc = atp->atrti_tc;
1416 	(void) fprintf(out, "   %c+---> TC%u 0x%02x",
1417 	    atp->atrti_flags & ATR_TI_HAVE_TD ? '|' : ' ',
1418 	    atp->atrti_ti_val, tc);
1419 
1420 	switch (atp->atrti_ti_val) {
1421 	case 1:
1422 		(void) fprintf(out, "; Extra Guard Time Integer: %u", tc);
1423 		break;
1424 	case 2:
1425 		if (atp->atrti_protocol != ATR_PROTOCOL_T0) {
1426 			(void) fprintf(out, "; illegal value -- only valid for "
1427 			    "T=0");
1428 		} else {
1429 			(void) fprintf(out, "; Waiting Time Integer: %u", tc);
1430 		}
1431 		break;
1432 	default:
1433 		switch (atp->atrti_protocol) {
1434 		case ATR_PROTOCOL_T1:
1435 			if (level != 0)
1436 				break;
1437 			(void) fprintf(out, "; Error Detection Code: %s",
1438 			    ATR_T1_TC0_CRC(tc) ? "CRC" : "LRC");
1439 			break;
1440 		default:
1441 			break;
1442 		}
1443 	}
1444 	(void) fprintf(out, "\n");
1445 }
1446 
1447 void
1448 atr_data_hexdump(const uint8_t *buf, size_t nbytes, FILE *out)
1449 {
1450 	size_t i, j;
1451 
1452 	/* Print out the header */
1453 	(void) fprintf(out, "%*s    0", 4, "");
1454 	for (i = 1; i < 16; i++) {
1455 		if (i % 4 == 0 && i % 16 != 0) {
1456 			(void) fprintf(out, " ");
1457 		}
1458 
1459 		(void) fprintf(out, "%2x", i);
1460 	}
1461 	(void) fprintf(out, "  0123456789abcdef\n");
1462 
1463 	/* Print out data */
1464 	for (i = 0; i < nbytes; i++) {
1465 
1466 		if (i % 16 == 0) {
1467 			(void) fprintf(out, "%04x:  ", i);
1468 		}
1469 
1470 		if (i % 4 == 0 && i % 16 != 0) {
1471 			(void) fprintf(out, " ");
1472 		}
1473 
1474 		(void) fprintf(out, "%02x", buf[i]);
1475 
1476 		if (i % 16 == 15 || i + 1 == nbytes) {
1477 			for (j = (i % 16) + 1; j < 16; j++) {
1478 				if (j % 4 == 0 && j % 16 != 0) {
1479 					(void) fprintf(out, " ");
1480 				}
1481 
1482 				(void) fprintf(out, "  ");
1483 			}
1484 
1485 			(void) fprintf(out, "  ");
1486 			for (j = i - (i % 16); j <= i; j++) {
1487 				(void) fprintf(out, "%c",
1488 				    isprint(buf[j]) ? buf[j] : '.');
1489 			}
1490 			(void) printf("\n");
1491 		}
1492 	}
1493 }
1494 
1495 static void
1496 atr_data_hexdump_historical(atr_data_t *data, FILE *out)
1497 {
1498 	(void) fprintf(out, "Dumping raw historical bytes\n");
1499 
1500 	atr_data_hexdump(data->atr_historic, data->atr_nhistoric, out);
1501 }
1502 
1503 static void
1504 atr_data_dump_historical(atr_data_t *data, FILE *out)
1505 {
1506 	uint8_t cat;
1507 
1508 	(void) fprintf(out, "Historic Data: %u bytes", data->atr_nhistoric);
1509 	if (data->atr_nhistoric == 0) {
1510 		(void) fprintf(out, "\n");
1511 		return;
1512 	}
1513 
1514 	cat = data->atr_historic[0];
1515 	(void) fprintf(out, "; format (0x%02x) ", cat);
1516 	if (cat == ATR_HIST_CAT_MAND_STATUS) {
1517 		(void) fprintf(out, "card status, not shown");
1518 	} else if (cat == ATR_HIST_CAT_TLV_STATUS) {
1519 		(void) fprintf(out, "COMPACT-TLV, not shown");
1520 	} else if (cat >= ATR_HIST_CAT_RFU_MIN && cat <= ATR_HIST_CAT_RFU_MAX) {
1521 		(void) fprintf(out, "reserved\n");
1522 		atr_data_hexdump_historical(data, out);
1523 		return;
1524 	} else {
1525 		(void) fprintf(out, "proprietary\n");
1526 		atr_data_hexdump_historical(data, out);
1527 		return;
1528 	}
1529 }
1530 
1531 void
1532 atr_data_dump(atr_data_t *data, FILE *out)
1533 {
1534 	uint8_t i, level;
1535 	if ((data->atr_flags & ATR_F_VALID) == 0)
1536 		return;
1537 
1538 	(void) fprintf(out, "TS  0x%02u - ", data->atr_raw[0]);
1539 	if (data->atr_flags & ATR_F_USES_DIRECT) {
1540 		(void) fprintf(out, "direct convention\n");
1541 	} else {
1542 		(void) fprintf(out, "inverse convention\n");
1543 	}
1544 
1545 	level = 0;
1546 	for (i = 0; i < data->atr_nti; i++) {
1547 		atr_ti_t *atp = &data->atr_ti[i];
1548 
1549 		/*
1550 		 * Various protocols may appear multiple times, indicating
1551 		 * different sets of bits each time. When dealing with T0 and
1552 		 * TD1, the protocol doesn't matter. Otherwise if we have the
1553 		 * same value, we should increment this.
1554 		 */
1555 		if (i <= 2) {
1556 			level = 0;
1557 		} else if (atp->atrti_protocol ==
1558 		    data->atr_ti[i - 1].atrti_protocol) {
1559 			level++;
1560 		} else {
1561 			level = 0;
1562 		}
1563 
1564 		if (i == 0) {
1565 			(void) fprintf(out, "T0  ");
1566 		} else {
1567 			(void) fprintf(out, "TD%u ", i);
1568 		}
1569 		(void) fprintf(out, "0x%02x\n",
1570 		    data->atr_raw[atp->atrti_td_idx]);
1571 		(void) fprintf(out, "      |+-> ");
1572 		if (i == 0) {
1573 			(void) fprintf(out, "%u historical bytes\n",
1574 			    data->atr_nhistoric);
1575 		} else {
1576 			(void) fprintf(out, "protocol T=%u\n",
1577 			    atp->atrti_protocol);
1578 		}
1579 		(void) fprintf(out, "      v\n");
1580 		(void) fprintf(out, " 0r%u%u%u%u\n",
1581 		    atp->atrti_flags & ATR_TI_HAVE_TD ? 1 : 0,
1582 		    atp->atrti_flags & ATR_TI_HAVE_TC ? 1 : 0,
1583 		    atp->atrti_flags & ATR_TI_HAVE_TB ? 1 : 0,
1584 		    atp->atrti_flags & ATR_TI_HAVE_TA ? 1 : 0);
1585 
1586 		atr_data_dump_ta(atp, out, level);
1587 		atr_data_dump_tb(atp, out, level);
1588 		atr_data_dump_tc(atp, out, level);
1589 		if (atp->atrti_flags & ATR_TI_HAVE_TD) {
1590 			(void) fprintf(out, "   v\n");
1591 		}
1592 	}
1593 
1594 	atr_data_dump_historical(data, out);
1595 
1596 	if (data->atr_flags & ATR_F_HAS_CHECKSUM) {
1597 		(void) fprintf(out, "TCK  0x%02x\n", data->atr_cksum);
1598 	} else {
1599 		(void) fprintf(out, "TCK  ----; Checksum not present\n");
1600 	}
1601 
1602 }
1603 #endif	/* _KERNEL */
1604