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 #ifndef _SYS_USB_UCCID_H
17 #define	_SYS_USB_UCCID_H
18 
19 /*
20  * Definitions for the userland CCID interface.
21  */
22 
23 #include <sys/types.h>
24 #include <sys/usb/clients/ccid/ccid.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /*
31  * The maximum size of a normal APDU. This is the upper bound of what a user can
32  * read or write to a given card.
33  */
34 #define	UCCID_APDU_SIZE_MAX	261
35 
36 /*
37  * This is the maximum length of an ATR as per ISO/IEC 7816-3:2006.
38  */
39 #define	UCCID_ATR_MAX		33
40 
41 
42 #define	UCCID_IOCTL	(('u' << 24) | ('c' << 16) | ('d') << 8)
43 
44 #define	UCCID_VERSION_ONE	1
45 #define	UCCID_CURRENT_VERSION	UCCID_VERSION_ONE
46 
47 /*
48  * Attempt to obtain exclusive access. If the UCN_TXN_DONT_BLOCK flag is
49  * specified, the ioctl will return immediately if exclusive access cannot be
50  * gained. Otherwise, it will block in an interruptible fashion. The argument is
51  * a uccid_cmd_txn_begin_t.
52  */
53 #define	UCCID_CMD_TXN_BEGIN	(UCCID_IOCTL | 0x01)
54 #define	UCCID_TXN_DONT_BLOCK	0x01
55 
56 typedef struct uccid_cmd_txn_begin {
57 	uint32_t	uct_version;
58 	uint32_t	uct_flags;
59 } uccid_cmd_txn_begin_t;
60 
61 /*
62  * Relinquish exclusive access. Takes a uccid_cmd_txn_end_t. The callers should
63  * specify one of UCCID_TXN_END_RESET or UCCID_TXN_END_RELEASE. These indicate
64  * what behavior should be taken when we release the transaction. It is
65  * considered an error if neither is specified. If the caller exits without
66  * calling this function, then the ICC will be reset.
67  */
68 #define	UCCID_CMD_TXN_END	(UCCID_IOCTL | 0x02)
69 #define	UCCID_TXN_END_RESET	0x01
70 #define	UCCID_TXN_END_RELEASE	0x02
71 
72 typedef struct uccid_cmd_txn_end {
73 	uint32_t	uct_version;
74 	uint32_t	uct_flags;
75 } uccid_cmd_txn_end_t;
76 
77 /*
78  * Obtain the status of the slot. Returns a filled-in uccid_cmd_status_t.
79  */
80 #define	UCCID_CMD_STATUS	(UCCID_IOCTL | 0x3)
81 
82 /*
83  * Protocol definitions. This should match common/ccid/atr.h.
84  */
85 typedef enum {
86 	UCCID_PROT_NONE = 0,
87 	UCCID_PROT_T0	= 1 << 0,
88 	UCCID_PROT_T1	= 1 << 1
89 } uccid_prot_t;
90 
91 /*
92  * Bits for UCS Status
93  */
94 #define	UCCID_STATUS_F_CARD_PRESENT	0x01
95 #define	UCCID_STATUS_F_CARD_ACTIVE	0x02
96 #define	UCCID_STATUS_F_PRODUCT_VALID	0x04
97 #define	UCCID_STATUS_F_SERIAL_VALID	0x08
98 #define	UCCID_STATUS_F_PARAMS_VALID	0x10
99 
100 typedef struct uccid_cmd_status {
101 	uint32_t	ucs_version;
102 	uint32_t	ucs_status;
103 	int32_t		ucs_instance;
104 	uint32_t	ucs_slot;
105 	uint8_t		ucs_atr[UCCID_ATR_MAX];
106 	uint8_t		ucs_atrlen;
107 	uint8_t		ucs_pad[6];
108 	int8_t		ucs_product[256];
109 	int8_t		ucs_serial[256];
110 	ccid_class_descr_t	ucs_class;
111 	uccid_prot_t	ucs_prot;
112 	ccid_params_t	ucs_params;
113 } uccid_cmd_status_t;
114 
115 /*
116  * Modify the state of the ICC, if present.
117  */
118 #define	UCCID_CMD_ICC_MODIFY	(UCCID_IOCTL | 0x04)
119 
120 #define	UCCID_ICC_POWER_ON	0x01
121 #define	UCCID_ICC_POWER_OFF	0x02
122 #define	UCCID_ICC_WARM_RESET	0x03
123 
124 typedef struct uccid_cmd_icc_modify {
125 	uint32_t uci_version;
126 	uint32_t uci_action;
127 } uccid_cmd_icc_modify_t;
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 
134 #endif /* _SYS_USB_UCCID_H */
135