root / hw / usb / dev-smartcard-reader.c @ ae12e3a6
History | View | Annotate | Download (46.5 kB)
1 | 36707144 | Alon Levy | /*
|
---|---|---|---|
2 | 36707144 | Alon Levy | * Copyright (C) 2011 Red Hat, Inc.
|
3 | 36707144 | Alon Levy | *
|
4 | 36707144 | Alon Levy | * CCID Device emulation
|
5 | 36707144 | Alon Levy | *
|
6 | 36707144 | Alon Levy | * Written by Alon Levy, with contributions from Robert Relyea.
|
7 | 36707144 | Alon Levy | *
|
8 | 93148aa5 | Stefan Weil | * Based on usb-serial.c, see its copyright and attributions below.
|
9 | 36707144 | Alon Levy | *
|
10 | 36707144 | Alon Levy | * This work is licensed under the terms of the GNU GPL, version 2.1 or later.
|
11 | 36707144 | Alon Levy | * See the COPYING file in the top-level directory.
|
12 | 36707144 | Alon Levy | * ------- (original copyright & attribution for usb-serial.c below) --------
|
13 | 36707144 | Alon Levy | * Copyright (c) 2006 CodeSourcery.
|
14 | 36707144 | Alon Levy | * Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org>
|
15 | 36707144 | Alon Levy | * Written by Paul Brook, reused for FTDI by Samuel Thibault,
|
16 | 36707144 | Alon Levy | */
|
17 | 36707144 | Alon Levy | |
18 | 36707144 | Alon Levy | /*
|
19 | 36707144 | Alon Levy | * References:
|
20 | 36707144 | Alon Levy | *
|
21 | 36707144 | Alon Levy | * CCID Specification Revision 1.1 April 22nd 2005
|
22 | 36707144 | Alon Levy | * "Universal Serial Bus, Device Class: Smart Card"
|
23 | 36707144 | Alon Levy | * Specification for Integrated Circuit(s) Cards Interface Devices
|
24 | 36707144 | Alon Levy | *
|
25 | 62a2ab6a | Brad Hards | * Endianness note: from the spec (1.3)
|
26 | 36707144 | Alon Levy | * "Fields that are larger than a byte are stored in little endian"
|
27 | 36707144 | Alon Levy | *
|
28 | 36707144 | Alon Levy | * KNOWN BUGS
|
29 | 36707144 | Alon Levy | * 1. remove/insert can sometimes result in removed state instead of inserted.
|
30 | 36707144 | Alon Levy | * This is a result of the following:
|
31 | 36707144 | Alon Levy | * symptom: dmesg shows ERMOTEIO (-121), pcscd shows -99. This can happen
|
32 | 36707144 | Alon Levy | * when a short packet is sent, as seen in uhci-usb.c, resulting from a urb
|
33 | 36707144 | Alon Levy | * from the guest requesting SPD and us returning a smaller packet.
|
34 | 36707144 | Alon Levy | * Not sure which messages trigger this.
|
35 | 36707144 | Alon Levy | */
|
36 | 36707144 | Alon Levy | |
37 | 36707144 | Alon Levy | #include "qemu-common.h" |
38 | 1de7afc9 | Paolo Bonzini | #include "qemu/error-report.h" |
39 | f1ae32a1 | Gerd Hoffmann | #include "hw/usb.h" |
40 | f1ae32a1 | Gerd Hoffmann | #include "hw/usb/desc.h" |
41 | 83c9089e | Paolo Bonzini | #include "monitor/monitor.h" |
42 | 36707144 | Alon Levy | |
43 | 47b43a1f | Paolo Bonzini | #include "ccid.h" |
44 | 36707144 | Alon Levy | |
45 | 36707144 | Alon Levy | #define DPRINTF(s, lvl, fmt, ...) \
|
46 | 36707144 | Alon Levy | do { \
|
47 | 36707144 | Alon Levy | if (lvl <= s->debug) { \
|
48 | 36707144 | Alon Levy | printf("usb-ccid: " fmt , ## __VA_ARGS__); \ |
49 | 36707144 | Alon Levy | } \ |
50 | 36707144 | Alon Levy | } while (0) |
51 | 36707144 | Alon Levy | |
52 | 36707144 | Alon Levy | #define D_WARN 1 |
53 | 36707144 | Alon Levy | #define D_INFO 2 |
54 | 36707144 | Alon Levy | #define D_MORE_INFO 3 |
55 | 36707144 | Alon Levy | #define D_VERBOSE 4 |
56 | 36707144 | Alon Levy | |
57 | 36707144 | Alon Levy | #define CCID_DEV_NAME "usb-ccid" |
58 | 36707144 | Alon Levy | |
59 | 36707144 | Alon Levy | /*
|
60 | 36707144 | Alon Levy | * The two options for variable sized buffers:
|
61 | 36707144 | Alon Levy | * make them constant size, for large enough constant,
|
62 | 36707144 | Alon Levy | * or handle the migration complexity - VMState doesn't handle this case.
|
63 | 36707144 | Alon Levy | * sizes are expected never to be exceeded, unless guest misbehaves.
|
64 | 36707144 | Alon Levy | */
|
65 | 36707144 | Alon Levy | #define BULK_OUT_DATA_SIZE 65536 |
66 | 36707144 | Alon Levy | #define PENDING_ANSWERS_NUM 128 |
67 | 36707144 | Alon Levy | |
68 | 36707144 | Alon Levy | #define BULK_IN_BUF_SIZE 384 |
69 | 36707144 | Alon Levy | #define BULK_IN_PENDING_NUM 8 |
70 | 36707144 | Alon Levy | |
71 | 36707144 | Alon Levy | #define CCID_MAX_PACKET_SIZE 64 |
72 | 36707144 | Alon Levy | |
73 | 36707144 | Alon Levy | #define CCID_CONTROL_ABORT 0x1 |
74 | 36707144 | Alon Levy | #define CCID_CONTROL_GET_CLOCK_FREQUENCIES 0x2 |
75 | 36707144 | Alon Levy | #define CCID_CONTROL_GET_DATA_RATES 0x3 |
76 | 36707144 | Alon Levy | |
77 | 36707144 | Alon Levy | #define CCID_PRODUCT_DESCRIPTION "QEMU USB CCID" |
78 | 93bfef4c | Crístian Viana | #define CCID_VENDOR_DESCRIPTION "QEMU" |
79 | 36707144 | Alon Levy | #define CCID_INTERFACE_NAME "CCID Interface" |
80 | 36707144 | Alon Levy | #define CCID_SERIAL_NUMBER_STRING "1" |
81 | 36707144 | Alon Levy | /*
|
82 | 36707144 | Alon Levy | * Using Gemplus Vendor and Product id
|
83 | 36707144 | Alon Levy | * Effect on various drivers:
|
84 | 36707144 | Alon Levy | * usbccid.sys (winxp, others untested) is a class driver so it doesn't care.
|
85 | 36707144 | Alon Levy | * linux has a number of class drivers, but openct filters based on
|
86 | 36707144 | Alon Levy | * vendor/product (/etc/openct.conf under fedora), hence Gemplus.
|
87 | 36707144 | Alon Levy | */
|
88 | 36707144 | Alon Levy | #define CCID_VENDOR_ID 0x08e6 |
89 | 36707144 | Alon Levy | #define CCID_PRODUCT_ID 0x4433 |
90 | 36707144 | Alon Levy | #define CCID_DEVICE_VERSION 0x0000 |
91 | 36707144 | Alon Levy | |
92 | 36707144 | Alon Levy | /*
|
93 | 36707144 | Alon Levy | * BULK_OUT messages from PC to Reader
|
94 | 36707144 | Alon Levy | * Defined in CCID Rev 1.1 6.1 (page 26)
|
95 | 36707144 | Alon Levy | */
|
96 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn 0x62 |
97 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff 0x63 |
98 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus 0x65 |
99 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock 0x6f |
100 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters 0x6c |
101 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters 0x6d |
102 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters 0x61 |
103 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_Escape 0x6b |
104 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_IccClock 0x6e |
105 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU 0x6a |
106 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_Secure 0x69 |
107 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical 0x71 |
108 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_Abort 0x72 |
109 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency 0x73 |
110 | 36707144 | Alon Levy | |
111 | 36707144 | Alon Levy | /*
|
112 | 36707144 | Alon Levy | * BULK_IN messages from Reader to PC
|
113 | 36707144 | Alon Levy | * Defined in CCID Rev 1.1 6.2 (page 48)
|
114 | 36707144 | Alon Levy | */
|
115 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock 0x80 |
116 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus 0x81 |
117 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_Parameters 0x82 |
118 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_Escape 0x83 |
119 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_DataRateAndClockFrequency 0x84 |
120 | 36707144 | Alon Levy | |
121 | 36707144 | Alon Levy | /*
|
122 | 36707144 | Alon Levy | * INTERRUPT_IN messages from Reader to PC
|
123 | 36707144 | Alon Levy | * Defined in CCID Rev 1.1 6.3 (page 56)
|
124 | 36707144 | Alon Levy | */
|
125 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange 0x50 |
126 | 36707144 | Alon Levy | #define CCID_MESSAGE_TYPE_RDR_to_PC_HardwareError 0x51 |
127 | 36707144 | Alon Levy | |
128 | 36707144 | Alon Levy | /*
|
129 | 36707144 | Alon Levy | * Endpoints for CCID - addresses are up to us to decide.
|
130 | 36707144 | Alon Levy | * To support slot insertion and removal we must have an interrupt in ep
|
131 | 36707144 | Alon Levy | * in addition we need a bulk in and bulk out ep
|
132 | 36707144 | Alon Levy | * 5.2, page 20
|
133 | 36707144 | Alon Levy | */
|
134 | 36707144 | Alon Levy | #define CCID_INT_IN_EP 1 |
135 | 36707144 | Alon Levy | #define CCID_BULK_IN_EP 2 |
136 | 36707144 | Alon Levy | #define CCID_BULK_OUT_EP 3 |
137 | 36707144 | Alon Levy | |
138 | 36707144 | Alon Levy | /* bmSlotICCState masks */
|
139 | 36707144 | Alon Levy | #define SLOT_0_STATE_MASK 1 |
140 | 36707144 | Alon Levy | #define SLOT_0_CHANGED_MASK 2 |
141 | 36707144 | Alon Levy | |
142 | 36707144 | Alon Levy | /* Status codes that go in bStatus (see 6.2.6) */
|
143 | 36707144 | Alon Levy | enum {
|
144 | 36707144 | Alon Levy | ICC_STATUS_PRESENT_ACTIVE = 0,
|
145 | 36707144 | Alon Levy | ICC_STATUS_PRESENT_INACTIVE, |
146 | 36707144 | Alon Levy | ICC_STATUS_NOT_PRESENT |
147 | 36707144 | Alon Levy | }; |
148 | 36707144 | Alon Levy | |
149 | 36707144 | Alon Levy | enum {
|
150 | 36707144 | Alon Levy | COMMAND_STATUS_NO_ERROR = 0,
|
151 | 36707144 | Alon Levy | COMMAND_STATUS_FAILED, |
152 | 36707144 | Alon Levy | COMMAND_STATUS_TIME_EXTENSION_REQUIRED |
153 | 36707144 | Alon Levy | }; |
154 | 36707144 | Alon Levy | |
155 | 36707144 | Alon Levy | /* Error codes that go in bError (see 6.2.6) */
|
156 | 36707144 | Alon Levy | enum {
|
157 | 36707144 | Alon Levy | ERROR_CMD_NOT_SUPPORTED = 0,
|
158 | 36707144 | Alon Levy | ERROR_CMD_ABORTED = -1,
|
159 | 36707144 | Alon Levy | ERROR_ICC_MUTE = -2,
|
160 | 36707144 | Alon Levy | ERROR_XFR_PARITY_ERROR = -3,
|
161 | 36707144 | Alon Levy | ERROR_XFR_OVERRUN = -4,
|
162 | 36707144 | Alon Levy | ERROR_HW_ERROR = -5,
|
163 | 36707144 | Alon Levy | }; |
164 | 36707144 | Alon Levy | |
165 | 36707144 | Alon Levy | /* 6.2.6 RDR_to_PC_SlotStatus definitions */
|
166 | 36707144 | Alon Levy | enum {
|
167 | 36707144 | Alon Levy | CLOCK_STATUS_RUNNING = 0,
|
168 | 36707144 | Alon Levy | /*
|
169 | 36707144 | Alon Levy | * 0 - Clock Running, 1 - Clock stopped in State L, 2 - H,
|
170 | 62a2ab6a | Brad Hards | * 3 - unknown state. rest are RFU
|
171 | 36707144 | Alon Levy | */
|
172 | 36707144 | Alon Levy | }; |
173 | 36707144 | Alon Levy | |
174 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_Header { |
175 | 36707144 | Alon Levy | uint8_t bMessageType; |
176 | 36707144 | Alon Levy | uint32_t dwLength; |
177 | 36707144 | Alon Levy | uint8_t bSlot; |
178 | 36707144 | Alon Levy | uint8_t bSeq; |
179 | 36707144 | Alon Levy | } CCID_Header; |
180 | 36707144 | Alon Levy | |
181 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_BULK_IN { |
182 | 36707144 | Alon Levy | CCID_Header hdr; |
183 | 36707144 | Alon Levy | uint8_t bStatus; /* Only used in BULK_IN */
|
184 | 36707144 | Alon Levy | uint8_t bError; /* Only used in BULK_IN */
|
185 | 36707144 | Alon Levy | } CCID_BULK_IN; |
186 | 36707144 | Alon Levy | |
187 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_SlotStatus { |
188 | 36707144 | Alon Levy | CCID_BULK_IN b; |
189 | 36707144 | Alon Levy | uint8_t bClockStatus; |
190 | 36707144 | Alon Levy | } CCID_SlotStatus; |
191 | 36707144 | Alon Levy | |
192 | 4942d6c3 | Alon Levy | typedef struct QEMU_PACKED CCID_T0ProtocolDataStructure { |
193 | 4942d6c3 | Alon Levy | uint8_t bmFindexDindex; |
194 | 4942d6c3 | Alon Levy | uint8_t bmTCCKST0; |
195 | 4942d6c3 | Alon Levy | uint8_t bGuardTimeT0; |
196 | 4942d6c3 | Alon Levy | uint8_t bWaitingIntegerT0; |
197 | 4942d6c3 | Alon Levy | uint8_t bClockStop; |
198 | 4942d6c3 | Alon Levy | } CCID_T0ProtocolDataStructure; |
199 | 4942d6c3 | Alon Levy | |
200 | 4942d6c3 | Alon Levy | typedef struct QEMU_PACKED CCID_T1ProtocolDataStructure { |
201 | 4942d6c3 | Alon Levy | uint8_t bmFindexDindex; |
202 | 4942d6c3 | Alon Levy | uint8_t bmTCCKST1; |
203 | 4942d6c3 | Alon Levy | uint8_t bGuardTimeT1; |
204 | 4942d6c3 | Alon Levy | uint8_t bWaitingIntegerT1; |
205 | 4942d6c3 | Alon Levy | uint8_t bClockStop; |
206 | 4942d6c3 | Alon Levy | uint8_t bIFSC; |
207 | 4942d6c3 | Alon Levy | uint8_t bNadValue; |
208 | 4942d6c3 | Alon Levy | } CCID_T1ProtocolDataStructure; |
209 | 4942d6c3 | Alon Levy | |
210 | 4942d6c3 | Alon Levy | typedef union CCID_ProtocolDataStructure { |
211 | 4942d6c3 | Alon Levy | CCID_T0ProtocolDataStructure t0; |
212 | 4942d6c3 | Alon Levy | CCID_T1ProtocolDataStructure t1; |
213 | 4942d6c3 | Alon Levy | uint8_t data[7]; /* must be = max(sizeof(t0), sizeof(t1)) */ |
214 | 4942d6c3 | Alon Levy | } CCID_ProtocolDataStructure; |
215 | 4942d6c3 | Alon Levy | |
216 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_Parameter { |
217 | 36707144 | Alon Levy | CCID_BULK_IN b; |
218 | 36707144 | Alon Levy | uint8_t bProtocolNum; |
219 | 4942d6c3 | Alon Levy | CCID_ProtocolDataStructure abProtocolDataStructure; |
220 | 36707144 | Alon Levy | } CCID_Parameter; |
221 | 36707144 | Alon Levy | |
222 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_DataBlock { |
223 | 36707144 | Alon Levy | CCID_BULK_IN b; |
224 | 36707144 | Alon Levy | uint8_t bChainParameter; |
225 | 36707144 | Alon Levy | uint8_t abData[0];
|
226 | 36707144 | Alon Levy | } CCID_DataBlock; |
227 | 36707144 | Alon Levy | |
228 | 36707144 | Alon Levy | /* 6.1.4 PC_to_RDR_XfrBlock */
|
229 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_XferBlock { |
230 | 36707144 | Alon Levy | CCID_Header hdr; |
231 | 36707144 | Alon Levy | uint8_t bBWI; /* Block Waiting Timeout */
|
232 | 36707144 | Alon Levy | uint16_t wLevelParameter; /* XXX currently unused */
|
233 | 36707144 | Alon Levy | uint8_t abData[0];
|
234 | 36707144 | Alon Levy | } CCID_XferBlock; |
235 | 36707144 | Alon Levy | |
236 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_IccPowerOn { |
237 | 36707144 | Alon Levy | CCID_Header hdr; |
238 | 36707144 | Alon Levy | uint8_t bPowerSelect; |
239 | 36707144 | Alon Levy | uint16_t abRFU; |
240 | 36707144 | Alon Levy | } CCID_IccPowerOn; |
241 | 36707144 | Alon Levy | |
242 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_IccPowerOff { |
243 | 36707144 | Alon Levy | CCID_Header hdr; |
244 | 36707144 | Alon Levy | uint16_t abRFU; |
245 | 36707144 | Alon Levy | } CCID_IccPowerOff; |
246 | 36707144 | Alon Levy | |
247 | 541dc0d4 | Stefan Weil | typedef struct QEMU_PACKED CCID_SetParameters { |
248 | 36707144 | Alon Levy | CCID_Header hdr; |
249 | 36707144 | Alon Levy | uint8_t bProtocolNum; |
250 | 36707144 | Alon Levy | uint16_t abRFU; |
251 | 4942d6c3 | Alon Levy | CCID_ProtocolDataStructure abProtocolDataStructure; |
252 | 36707144 | Alon Levy | } CCID_SetParameters; |
253 | 36707144 | Alon Levy | |
254 | 36707144 | Alon Levy | typedef struct CCID_Notify_Slot_Change { |
255 | 36707144 | Alon Levy | uint8_t bMessageType; /* CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange */
|
256 | 36707144 | Alon Levy | uint8_t bmSlotICCState; |
257 | 36707144 | Alon Levy | } CCID_Notify_Slot_Change; |
258 | 36707144 | Alon Levy | |
259 | 36707144 | Alon Levy | /* used for DataBlock response to XferBlock */
|
260 | 36707144 | Alon Levy | typedef struct Answer { |
261 | 36707144 | Alon Levy | uint8_t slot; |
262 | 36707144 | Alon Levy | uint8_t seq; |
263 | 36707144 | Alon Levy | } Answer; |
264 | 36707144 | Alon Levy | |
265 | 36707144 | Alon Levy | /* pending BULK_IN messages */
|
266 | 36707144 | Alon Levy | typedef struct BulkIn { |
267 | 36707144 | Alon Levy | uint8_t data[BULK_IN_BUF_SIZE]; |
268 | 36707144 | Alon Levy | uint32_t len; |
269 | 36707144 | Alon Levy | uint32_t pos; |
270 | 36707144 | Alon Levy | } BulkIn; |
271 | 36707144 | Alon Levy | |
272 | 36707144 | Alon Levy | enum {
|
273 | 36707144 | Alon Levy | MIGRATION_NONE, |
274 | 36707144 | Alon Levy | MIGRATION_MIGRATED, |
275 | 36707144 | Alon Levy | }; |
276 | 36707144 | Alon Levy | |
277 | 6df658f5 | Markus Armbruster | typedef struct CCIDBus { |
278 | 6df658f5 | Markus Armbruster | BusState qbus; |
279 | 6df658f5 | Markus Armbruster | } CCIDBus; |
280 | 36707144 | Alon Levy | |
281 | 36707144 | Alon Levy | /*
|
282 | 36707144 | Alon Levy | * powered - defaults to true, changed by PowerOn/PowerOff messages
|
283 | 36707144 | Alon Levy | */
|
284 | 6df658f5 | Markus Armbruster | typedef struct USBCCIDState { |
285 | 36707144 | Alon Levy | USBDevice dev; |
286 | 7567b51f | Gerd Hoffmann | USBEndpoint *intr; |
287 | 6df658f5 | Markus Armbruster | CCIDBus bus; |
288 | 36707144 | Alon Levy | CCIDCardState *card; |
289 | 36707144 | Alon Levy | BulkIn bulk_in_pending[BULK_IN_PENDING_NUM]; /* circular */
|
290 | 36707144 | Alon Levy | uint32_t bulk_in_pending_start; |
291 | 36707144 | Alon Levy | uint32_t bulk_in_pending_end; /* first free */
|
292 | 36707144 | Alon Levy | uint32_t bulk_in_pending_num; |
293 | 36707144 | Alon Levy | BulkIn *current_bulk_in; |
294 | 36707144 | Alon Levy | uint8_t bulk_out_data[BULK_OUT_DATA_SIZE]; |
295 | 36707144 | Alon Levy | uint32_t bulk_out_pos; |
296 | 36707144 | Alon Levy | uint64_t last_answer_error; |
297 | 36707144 | Alon Levy | Answer pending_answers[PENDING_ANSWERS_NUM]; |
298 | 36707144 | Alon Levy | uint32_t pending_answers_start; |
299 | 36707144 | Alon Levy | uint32_t pending_answers_end; |
300 | 36707144 | Alon Levy | uint32_t pending_answers_num; |
301 | 36707144 | Alon Levy | uint8_t bError; |
302 | 36707144 | Alon Levy | uint8_t bmCommandStatus; |
303 | 36707144 | Alon Levy | uint8_t bProtocolNum; |
304 | 4942d6c3 | Alon Levy | CCID_ProtocolDataStructure abProtocolDataStructure; |
305 | 36707144 | Alon Levy | uint32_t ulProtocolDataStructureSize; |
306 | 36707144 | Alon Levy | uint32_t state_vmstate; |
307 | 36707144 | Alon Levy | uint32_t migration_target_ip; |
308 | 36707144 | Alon Levy | uint16_t migration_target_port; |
309 | 36707144 | Alon Levy | uint8_t migration_state; |
310 | 36707144 | Alon Levy | uint8_t bmSlotICCState; |
311 | 36707144 | Alon Levy | uint8_t powered; |
312 | 36707144 | Alon Levy | uint8_t notify_slot_change; |
313 | 36707144 | Alon Levy | uint8_t debug; |
314 | 6df658f5 | Markus Armbruster | } USBCCIDState; |
315 | 36707144 | Alon Levy | |
316 | 36707144 | Alon Levy | /*
|
317 | 36707144 | Alon Levy | * CCID Spec chapter 4: CCID uses a standard device descriptor per Chapter 9,
|
318 | 36707144 | Alon Levy | * "USB Device Framework", section 9.6.1, in the Universal Serial Bus
|
319 | 36707144 | Alon Levy | * Specification.
|
320 | 36707144 | Alon Levy | *
|
321 | 36707144 | Alon Levy | * This device implemented based on the spec and with an Athena Smart Card
|
322 | 36707144 | Alon Levy | * Reader as reference:
|
323 | 36707144 | Alon Levy | * 0dc3:1004 Athena Smartcard Solutions, Inc.
|
324 | 36707144 | Alon Levy | */
|
325 | 36707144 | Alon Levy | |
326 | 97237e0a | Gerd Hoffmann | static const uint8_t qemu_ccid_descriptor[] = { |
327 | 36707144 | Alon Levy | /* Smart Card Device Class Descriptor */
|
328 | 36707144 | Alon Levy | 0x36, /* u8 bLength; */ |
329 | 36707144 | Alon Levy | 0x21, /* u8 bDescriptorType; Functional */ |
330 | 36707144 | Alon Levy | 0x10, 0x01, /* u16 bcdCCID; CCID Specification Release Number. */ |
331 | 36707144 | Alon Levy | 0x00, /* |
332 | 36707144 | Alon Levy | * u8 bMaxSlotIndex; The index of the highest available
|
333 | 36707144 | Alon Levy | * slot on this device. All slots are consecutive starting
|
334 | 36707144 | Alon Levy | * at 00h.
|
335 | 36707144 | Alon Levy | */
|
336 | 36707144 | Alon Levy | 0x07, /* u8 bVoltageSupport; 01h - 5.0v, 02h - 3.0, 03 - 1.8 */ |
337 | 36707144 | Alon Levy | |
338 | d7d218ef | Alon Levy | 0x00, 0x00, /* u32 dwProtocols; RRRR PPPP. RRRR = 0000h.*/ |
339 | d7d218ef | Alon Levy | 0x01, 0x00, /* PPPP: 0001h = Protocol T=0, 0002h = Protocol T=1 */ |
340 | 36707144 | Alon Levy | /* u32 dwDefaultClock; in kHZ (0x0fa0 is 4 MHz) */
|
341 | 36707144 | Alon Levy | 0xa0, 0x0f, 0x00, 0x00, |
342 | 36707144 | Alon Levy | /* u32 dwMaximumClock; */
|
343 | 36707144 | Alon Levy | 0x00, 0x00, 0x01, 0x00, |
344 | 36707144 | Alon Levy | 0x00, /* u8 bNumClockSupported; * |
345 | 36707144 | Alon Levy | * 0 means just the default and max. */
|
346 | 36707144 | Alon Levy | /* u32 dwDataRate ;bps. 9600 == 00002580h */
|
347 | 36707144 | Alon Levy | 0x80, 0x25, 0x00, 0x00, |
348 | 36707144 | Alon Levy | /* u32 dwMaxDataRate ; 11520 bps == 0001C200h */
|
349 | 36707144 | Alon Levy | 0x00, 0xC2, 0x01, 0x00, |
350 | 36707144 | Alon Levy | 0x00, /* u8 bNumDataRatesSupported; 00 means all rates between |
351 | 36707144 | Alon Levy | * default and max */
|
352 | 36707144 | Alon Levy | /* u32 dwMaxIFSD; *
|
353 | 36707144 | Alon Levy | * maximum IFSD supported by CCID for protocol *
|
354 | 36707144 | Alon Levy | * T=1 (Maximum seen from various cards) */
|
355 | 36707144 | Alon Levy | 0xfe, 0x00, 0x00, 0x00, |
356 | 36707144 | Alon Levy | /* u32 dwSyncProtocols; 1 - 2-wire, 2 - 3-wire, 4 - I2C */
|
357 | 36707144 | Alon Levy | 0x00, 0x00, 0x00, 0x00, |
358 | 36707144 | Alon Levy | /* u32 dwMechanical; 0 - no special characteristics. */
|
359 | 36707144 | Alon Levy | 0x00, 0x00, 0x00, 0x00, |
360 | 36707144 | Alon Levy | /*
|
361 | 36707144 | Alon Levy | * u32 dwFeatures;
|
362 | 36707144 | Alon Levy | * 0 - No special characteristics
|
363 | 36707144 | Alon Levy | * + 2 Automatic parameter configuration based on ATR data
|
364 | 36707144 | Alon Levy | * + 4 Automatic activation of ICC on inserting
|
365 | 36707144 | Alon Levy | * + 8 Automatic ICC voltage selection
|
366 | 36707144 | Alon Levy | * + 10 Automatic ICC clock frequency change
|
367 | 36707144 | Alon Levy | * + 20 Automatic baud rate change
|
368 | 36707144 | Alon Levy | * + 40 Automatic parameters negotiation made by the CCID
|
369 | 36707144 | Alon Levy | * + 80 automatic PPS made by the CCID
|
370 | 36707144 | Alon Levy | * 100 CCID can set ICC in clock stop mode
|
371 | 36707144 | Alon Levy | * 200 NAD value other then 00 accepted (T=1 protocol)
|
372 | 36707144 | Alon Levy | * + 400 Automatic IFSD exchange as first exchange (T=1)
|
373 | 36707144 | Alon Levy | * One of the following only:
|
374 | 36707144 | Alon Levy | * + 10000 TPDU level exchanges with CCID
|
375 | 36707144 | Alon Levy | * 20000 Short APDU level exchange with CCID
|
376 | 36707144 | Alon Levy | * 40000 Short and Extended APDU level exchange with CCID
|
377 | 36707144 | Alon Levy | *
|
378 | c5cd7c87 | Alon Levy | * 100000 USB Wake up signaling supported on card
|
379 | 36707144 | Alon Levy | * insertion and removal. Must set bit 5 in bmAttributes
|
380 | 36707144 | Alon Levy | * in Configuration descriptor if 100000 is set.
|
381 | 36707144 | Alon Levy | */
|
382 | c5cd7c87 | Alon Levy | 0xfe, 0x04, 0x01, 0x00, |
383 | 36707144 | Alon Levy | /*
|
384 | 36707144 | Alon Levy | * u32 dwMaxCCIDMessageLength; For extended APDU in
|
385 | 36707144 | Alon Levy | * [261 + 10 , 65544 + 10]. Otherwise the minimum is
|
386 | 36707144 | Alon Levy | * wMaxPacketSize of the Bulk-OUT endpoint
|
387 | 36707144 | Alon Levy | */
|
388 | 36707144 | Alon Levy | 0x12, 0x00, 0x01, 0x00, |
389 | 36707144 | Alon Levy | 0xFF, /* |
390 | 36707144 | Alon Levy | * u8 bClassGetResponse; Significant only for CCID that
|
391 | 36707144 | Alon Levy | * offers an APDU level for exchanges. Indicates the
|
392 | 36707144 | Alon Levy | * default class value used by the CCID when it sends a
|
393 | 36707144 | Alon Levy | * Get Response command to perform the transportation of
|
394 | 36707144 | Alon Levy | * an APDU by T=0 protocol
|
395 | 36707144 | Alon Levy | * FFh indicates that the CCID echos the class of the APDU.
|
396 | 36707144 | Alon Levy | */
|
397 | 36707144 | Alon Levy | 0xFF, /* |
398 | 36707144 | Alon Levy | * u8 bClassEnvelope; EAPDU only. Envelope command for
|
399 | 36707144 | Alon Levy | * T=0
|
400 | 36707144 | Alon Levy | */
|
401 | 36707144 | Alon Levy | 0x00, 0x00, /* |
402 | 36707144 | Alon Levy | * u16 wLcdLayout; XXYY Number of lines (XX) and chars per
|
403 | 36707144 | Alon Levy | * line for LCD display used for PIN entry. 0000 - no LCD
|
404 | 36707144 | Alon Levy | */
|
405 | 36707144 | Alon Levy | 0x01, /* |
406 | 36707144 | Alon Levy | * u8 bPINSupport; 01h PIN Verification,
|
407 | 36707144 | Alon Levy | * 02h PIN Modification
|
408 | 36707144 | Alon Levy | */
|
409 | 36707144 | Alon Levy | 0x01, /* u8 bMaxCCIDBusySlots; */ |
410 | 97237e0a | Gerd Hoffmann | }; |
411 | 36707144 | Alon Levy | |
412 | 97237e0a | Gerd Hoffmann | enum {
|
413 | 97237e0a | Gerd Hoffmann | STR_MANUFACTURER = 1,
|
414 | 97237e0a | Gerd Hoffmann | STR_PRODUCT, |
415 | 97237e0a | Gerd Hoffmann | STR_SERIALNUMBER, |
416 | 97237e0a | Gerd Hoffmann | STR_INTERFACE, |
417 | 97237e0a | Gerd Hoffmann | }; |
418 | 36707144 | Alon Levy | |
419 | 97237e0a | Gerd Hoffmann | static const USBDescStrings desc_strings = { |
420 | 93bfef4c | Crístian Viana | [STR_MANUFACTURER] = "QEMU",
|
421 | 97237e0a | Gerd Hoffmann | [STR_PRODUCT] = "QEMU USB CCID",
|
422 | 97237e0a | Gerd Hoffmann | [STR_SERIALNUMBER] = "1",
|
423 | 97237e0a | Gerd Hoffmann | [STR_INTERFACE] = "CCID Interface",
|
424 | 97237e0a | Gerd Hoffmann | }; |
425 | 97237e0a | Gerd Hoffmann | |
426 | 97237e0a | Gerd Hoffmann | static const USBDescIface desc_iface0 = { |
427 | 97237e0a | Gerd Hoffmann | .bInterfaceNumber = 0,
|
428 | 97237e0a | Gerd Hoffmann | .bNumEndpoints = 3,
|
429 | 693e4773 | Alon Levy | .bInterfaceClass = USB_CLASS_CSCID, |
430 | 693e4773 | Alon Levy | .bInterfaceSubClass = USB_SUBCLASS_UNDEFINED, |
431 | 97237e0a | Gerd Hoffmann | .bInterfaceProtocol = 0x00,
|
432 | 97237e0a | Gerd Hoffmann | .iInterface = STR_INTERFACE, |
433 | 97237e0a | Gerd Hoffmann | .ndesc = 1,
|
434 | 97237e0a | Gerd Hoffmann | .descs = (USBDescOther[]) { |
435 | 97237e0a | Gerd Hoffmann | { |
436 | 97237e0a | Gerd Hoffmann | /* smartcard descriptor */
|
437 | 97237e0a | Gerd Hoffmann | .data = qemu_ccid_descriptor, |
438 | 97237e0a | Gerd Hoffmann | }, |
439 | 97237e0a | Gerd Hoffmann | }, |
440 | 97237e0a | Gerd Hoffmann | .eps = (USBDescEndpoint[]) { |
441 | 97237e0a | Gerd Hoffmann | { |
442 | 97237e0a | Gerd Hoffmann | .bEndpointAddress = USB_DIR_IN | CCID_INT_IN_EP, |
443 | 97237e0a | Gerd Hoffmann | .bmAttributes = USB_ENDPOINT_XFER_INT, |
444 | 97237e0a | Gerd Hoffmann | .bInterval = 255,
|
445 | 97237e0a | Gerd Hoffmann | .wMaxPacketSize = 64,
|
446 | 97237e0a | Gerd Hoffmann | },{ |
447 | 97237e0a | Gerd Hoffmann | .bEndpointAddress = USB_DIR_IN | CCID_BULK_IN_EP, |
448 | 97237e0a | Gerd Hoffmann | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
449 | 97237e0a | Gerd Hoffmann | .wMaxPacketSize = 64,
|
450 | 97237e0a | Gerd Hoffmann | },{ |
451 | 97237e0a | Gerd Hoffmann | .bEndpointAddress = USB_DIR_OUT | CCID_BULK_OUT_EP, |
452 | 97237e0a | Gerd Hoffmann | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
453 | 97237e0a | Gerd Hoffmann | .wMaxPacketSize = 64,
|
454 | 97237e0a | Gerd Hoffmann | }, |
455 | 97237e0a | Gerd Hoffmann | } |
456 | 97237e0a | Gerd Hoffmann | }; |
457 | 97237e0a | Gerd Hoffmann | |
458 | 97237e0a | Gerd Hoffmann | static const USBDescDevice desc_device = { |
459 | 97237e0a | Gerd Hoffmann | .bcdUSB = 0x0110,
|
460 | 97237e0a | Gerd Hoffmann | .bMaxPacketSize0 = 64,
|
461 | 97237e0a | Gerd Hoffmann | .bNumConfigurations = 1,
|
462 | 97237e0a | Gerd Hoffmann | .confs = (USBDescConfig[]) { |
463 | 97237e0a | Gerd Hoffmann | { |
464 | 97237e0a | Gerd Hoffmann | .bNumInterfaces = 1,
|
465 | 97237e0a | Gerd Hoffmann | .bConfigurationValue = 1,
|
466 | cff17894 | Alon Levy | .bmAttributes = 0xe0,
|
467 | 97237e0a | Gerd Hoffmann | .bMaxPower = 50,
|
468 | 97237e0a | Gerd Hoffmann | .nif = 1,
|
469 | 97237e0a | Gerd Hoffmann | .ifs = &desc_iface0, |
470 | 97237e0a | Gerd Hoffmann | }, |
471 | 97237e0a | Gerd Hoffmann | }, |
472 | 97237e0a | Gerd Hoffmann | }; |
473 | 97237e0a | Gerd Hoffmann | |
474 | 97237e0a | Gerd Hoffmann | static const USBDesc desc_ccid = { |
475 | 97237e0a | Gerd Hoffmann | .id = { |
476 | 97237e0a | Gerd Hoffmann | .idVendor = CCID_VENDOR_ID, |
477 | 97237e0a | Gerd Hoffmann | .idProduct = CCID_PRODUCT_ID, |
478 | 97237e0a | Gerd Hoffmann | .bcdDevice = CCID_DEVICE_VERSION, |
479 | 97237e0a | Gerd Hoffmann | .iManufacturer = STR_MANUFACTURER, |
480 | 97237e0a | Gerd Hoffmann | .iProduct = STR_PRODUCT, |
481 | 97237e0a | Gerd Hoffmann | .iSerialNumber = STR_SERIALNUMBER, |
482 | 97237e0a | Gerd Hoffmann | }, |
483 | 97237e0a | Gerd Hoffmann | .full = &desc_device, |
484 | 97237e0a | Gerd Hoffmann | .str = desc_strings, |
485 | 36707144 | Alon Levy | }; |
486 | 36707144 | Alon Levy | |
487 | ba7c0520 | Anthony Liguori | static const uint8_t *ccid_card_get_atr(CCIDCardState *card, uint32_t *len) |
488 | ba7c0520 | Anthony Liguori | { |
489 | ba7c0520 | Anthony Liguori | CCIDCardClass *cc = CCID_CARD_GET_CLASS(card); |
490 | 4543d43c | Alon Levy | |
491 | ba7c0520 | Anthony Liguori | if (cc->get_atr) {
|
492 | ba7c0520 | Anthony Liguori | return cc->get_atr(card, len);
|
493 | ba7c0520 | Anthony Liguori | } |
494 | ba7c0520 | Anthony Liguori | return NULL; |
495 | ba7c0520 | Anthony Liguori | } |
496 | ba7c0520 | Anthony Liguori | |
497 | ba7c0520 | Anthony Liguori | static void ccid_card_apdu_from_guest(CCIDCardState *card, |
498 | ba7c0520 | Anthony Liguori | const uint8_t *apdu,
|
499 | ba7c0520 | Anthony Liguori | uint32_t len) |
500 | ba7c0520 | Anthony Liguori | { |
501 | ba7c0520 | Anthony Liguori | CCIDCardClass *cc = CCID_CARD_GET_CLASS(card); |
502 | 4543d43c | Alon Levy | |
503 | ba7c0520 | Anthony Liguori | if (cc->apdu_from_guest) {
|
504 | ba7c0520 | Anthony Liguori | cc->apdu_from_guest(card, apdu, len); |
505 | ba7c0520 | Anthony Liguori | } |
506 | ba7c0520 | Anthony Liguori | } |
507 | ba7c0520 | Anthony Liguori | |
508 | ba7c0520 | Anthony Liguori | static int ccid_card_exitfn(CCIDCardState *card) |
509 | ba7c0520 | Anthony Liguori | { |
510 | ba7c0520 | Anthony Liguori | CCIDCardClass *cc = CCID_CARD_GET_CLASS(card); |
511 | 4543d43c | Alon Levy | |
512 | ba7c0520 | Anthony Liguori | if (cc->exitfn) {
|
513 | ba7c0520 | Anthony Liguori | return cc->exitfn(card);
|
514 | ba7c0520 | Anthony Liguori | } |
515 | ba7c0520 | Anthony Liguori | return 0; |
516 | ba7c0520 | Anthony Liguori | } |
517 | ba7c0520 | Anthony Liguori | |
518 | ba7c0520 | Anthony Liguori | static int ccid_card_initfn(CCIDCardState *card) |
519 | ba7c0520 | Anthony Liguori | { |
520 | ba7c0520 | Anthony Liguori | CCIDCardClass *cc = CCID_CARD_GET_CLASS(card); |
521 | 4543d43c | Alon Levy | |
522 | ba7c0520 | Anthony Liguori | if (cc->initfn) {
|
523 | ba7c0520 | Anthony Liguori | return cc->initfn(card);
|
524 | ba7c0520 | Anthony Liguori | } |
525 | ba7c0520 | Anthony Liguori | return 0; |
526 | ba7c0520 | Anthony Liguori | } |
527 | ba7c0520 | Anthony Liguori | |
528 | 36707144 | Alon Levy | static bool ccid_has_pending_answers(USBCCIDState *s) |
529 | 36707144 | Alon Levy | { |
530 | 36707144 | Alon Levy | return s->pending_answers_num > 0; |
531 | 36707144 | Alon Levy | } |
532 | 36707144 | Alon Levy | |
533 | 36707144 | Alon Levy | static void ccid_clear_pending_answers(USBCCIDState *s) |
534 | 36707144 | Alon Levy | { |
535 | 36707144 | Alon Levy | s->pending_answers_num = 0;
|
536 | 36707144 | Alon Levy | s->pending_answers_start = 0;
|
537 | 36707144 | Alon Levy | s->pending_answers_end = 0;
|
538 | 36707144 | Alon Levy | } |
539 | 36707144 | Alon Levy | |
540 | 36707144 | Alon Levy | static void ccid_print_pending_answers(USBCCIDState *s) |
541 | 36707144 | Alon Levy | { |
542 | 36707144 | Alon Levy | Answer *answer; |
543 | 36707144 | Alon Levy | int i, count;
|
544 | 36707144 | Alon Levy | |
545 | 36707144 | Alon Levy | DPRINTF(s, D_VERBOSE, "usb-ccid: pending answers:");
|
546 | 36707144 | Alon Levy | if (!ccid_has_pending_answers(s)) {
|
547 | 36707144 | Alon Levy | DPRINTF(s, D_VERBOSE, " empty\n");
|
548 | 36707144 | Alon Levy | return;
|
549 | 36707144 | Alon Levy | } |
550 | 36707144 | Alon Levy | for (i = s->pending_answers_start, count = s->pending_answers_num ;
|
551 | 36707144 | Alon Levy | count > 0; count--, i++) {
|
552 | 36707144 | Alon Levy | answer = &s->pending_answers[i % PENDING_ANSWERS_NUM]; |
553 | 36707144 | Alon Levy | if (count == 1) { |
554 | 36707144 | Alon Levy | DPRINTF(s, D_VERBOSE, "%d:%d\n", answer->slot, answer->seq);
|
555 | 36707144 | Alon Levy | } else {
|
556 | 36707144 | Alon Levy | DPRINTF(s, D_VERBOSE, "%d:%d,", answer->slot, answer->seq);
|
557 | 36707144 | Alon Levy | } |
558 | 36707144 | Alon Levy | } |
559 | 36707144 | Alon Levy | } |
560 | 36707144 | Alon Levy | |
561 | 36707144 | Alon Levy | static void ccid_add_pending_answer(USBCCIDState *s, CCID_Header *hdr) |
562 | 36707144 | Alon Levy | { |
563 | 36707144 | Alon Levy | Answer *answer; |
564 | 36707144 | Alon Levy | |
565 | 36707144 | Alon Levy | assert(s->pending_answers_num < PENDING_ANSWERS_NUM); |
566 | 36707144 | Alon Levy | s->pending_answers_num++; |
567 | 36707144 | Alon Levy | answer = |
568 | 36707144 | Alon Levy | &s->pending_answers[(s->pending_answers_end++) % PENDING_ANSWERS_NUM]; |
569 | 36707144 | Alon Levy | answer->slot = hdr->bSlot; |
570 | 36707144 | Alon Levy | answer->seq = hdr->bSeq; |
571 | 36707144 | Alon Levy | ccid_print_pending_answers(s); |
572 | 36707144 | Alon Levy | } |
573 | 36707144 | Alon Levy | |
574 | 36707144 | Alon Levy | static void ccid_remove_pending_answer(USBCCIDState *s, |
575 | 36707144 | Alon Levy | uint8_t *slot, uint8_t *seq) |
576 | 36707144 | Alon Levy | { |
577 | 36707144 | Alon Levy | Answer *answer; |
578 | 36707144 | Alon Levy | |
579 | 36707144 | Alon Levy | assert(s->pending_answers_num > 0);
|
580 | 36707144 | Alon Levy | s->pending_answers_num--; |
581 | 36707144 | Alon Levy | answer = |
582 | 36707144 | Alon Levy | &s->pending_answers[(s->pending_answers_start++) % PENDING_ANSWERS_NUM]; |
583 | 36707144 | Alon Levy | *slot = answer->slot; |
584 | 36707144 | Alon Levy | *seq = answer->seq; |
585 | 36707144 | Alon Levy | ccid_print_pending_answers(s); |
586 | 36707144 | Alon Levy | } |
587 | 36707144 | Alon Levy | |
588 | 36707144 | Alon Levy | static void ccid_bulk_in_clear(USBCCIDState *s) |
589 | 36707144 | Alon Levy | { |
590 | 36707144 | Alon Levy | s->bulk_in_pending_start = 0;
|
591 | 36707144 | Alon Levy | s->bulk_in_pending_end = 0;
|
592 | 36707144 | Alon Levy | s->bulk_in_pending_num = 0;
|
593 | 36707144 | Alon Levy | } |
594 | 36707144 | Alon Levy | |
595 | 36707144 | Alon Levy | static void ccid_bulk_in_release(USBCCIDState *s) |
596 | 36707144 | Alon Levy | { |
597 | 36707144 | Alon Levy | assert(s->current_bulk_in != NULL);
|
598 | 36707144 | Alon Levy | s->current_bulk_in->pos = 0;
|
599 | 36707144 | Alon Levy | s->current_bulk_in = NULL;
|
600 | 36707144 | Alon Levy | } |
601 | 36707144 | Alon Levy | |
602 | 36707144 | Alon Levy | static void ccid_bulk_in_get(USBCCIDState *s) |
603 | 36707144 | Alon Levy | { |
604 | 36707144 | Alon Levy | if (s->current_bulk_in != NULL || s->bulk_in_pending_num == 0) { |
605 | 36707144 | Alon Levy | return;
|
606 | 36707144 | Alon Levy | } |
607 | 36707144 | Alon Levy | assert(s->bulk_in_pending_num > 0);
|
608 | 36707144 | Alon Levy | s->bulk_in_pending_num--; |
609 | 36707144 | Alon Levy | s->current_bulk_in = |
610 | 36707144 | Alon Levy | &s->bulk_in_pending[(s->bulk_in_pending_start++) % BULK_IN_PENDING_NUM]; |
611 | 36707144 | Alon Levy | } |
612 | 36707144 | Alon Levy | |
613 | 36707144 | Alon Levy | static void *ccid_reserve_recv_buf(USBCCIDState *s, uint16_t len) |
614 | 36707144 | Alon Levy | { |
615 | 36707144 | Alon Levy | BulkIn *bulk_in; |
616 | 36707144 | Alon Levy | |
617 | 36707144 | Alon Levy | DPRINTF(s, D_VERBOSE, "%s: QUEUE: reserve %d bytes\n", __func__, len);
|
618 | 36707144 | Alon Levy | |
619 | 36707144 | Alon Levy | /* look for an existing element */
|
620 | 36707144 | Alon Levy | if (len > BULK_IN_BUF_SIZE) {
|
621 | 36707144 | Alon Levy | DPRINTF(s, D_WARN, "usb-ccid.c: %s: len larger then max (%d>%d). "
|
622 | 36707144 | Alon Levy | "discarding message.\n",
|
623 | 36707144 | Alon Levy | __func__, len, BULK_IN_BUF_SIZE); |
624 | 36707144 | Alon Levy | return NULL; |
625 | 36707144 | Alon Levy | } |
626 | 36707144 | Alon Levy | if (s->bulk_in_pending_num >= BULK_IN_PENDING_NUM) {
|
627 | 36707144 | Alon Levy | DPRINTF(s, D_WARN, "usb-ccid.c: %s: No free bulk_in buffers. "
|
628 | 36707144 | Alon Levy | "discarding message.\n", __func__);
|
629 | 36707144 | Alon Levy | return NULL; |
630 | 36707144 | Alon Levy | } |
631 | 36707144 | Alon Levy | bulk_in = |
632 | 36707144 | Alon Levy | &s->bulk_in_pending[(s->bulk_in_pending_end++) % BULK_IN_PENDING_NUM]; |
633 | 36707144 | Alon Levy | s->bulk_in_pending_num++; |
634 | 36707144 | Alon Levy | bulk_in->len = len; |
635 | 36707144 | Alon Levy | return bulk_in->data;
|
636 | 36707144 | Alon Levy | } |
637 | 36707144 | Alon Levy | |
638 | 36707144 | Alon Levy | static void ccid_reset(USBCCIDState *s) |
639 | 36707144 | Alon Levy | { |
640 | 36707144 | Alon Levy | ccid_bulk_in_clear(s); |
641 | 36707144 | Alon Levy | ccid_clear_pending_answers(s); |
642 | 36707144 | Alon Levy | } |
643 | 36707144 | Alon Levy | |
644 | 36707144 | Alon Levy | static void ccid_detach(USBCCIDState *s) |
645 | 36707144 | Alon Levy | { |
646 | 36707144 | Alon Levy | ccid_reset(s); |
647 | 36707144 | Alon Levy | } |
648 | 36707144 | Alon Levy | |
649 | 36707144 | Alon Levy | static void ccid_handle_reset(USBDevice *dev) |
650 | 36707144 | Alon Levy | { |
651 | 36707144 | Alon Levy | USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev); |
652 | 36707144 | Alon Levy | |
653 | 36707144 | Alon Levy | DPRINTF(s, 1, "Reset\n"); |
654 | 36707144 | Alon Levy | |
655 | 36707144 | Alon Levy | ccid_reset(s); |
656 | 36707144 | Alon Levy | } |
657 | 36707144 | Alon Levy | |
658 | 7e1ac5ab | Alon Levy | static const char *ccid_control_to_str(USBCCIDState *s, int request) |
659 | 7e1ac5ab | Alon Levy | { |
660 | 7e1ac5ab | Alon Levy | switch (request) {
|
661 | 7e1ac5ab | Alon Levy | /* generic - should be factored out if there are other debugees */
|
662 | 7e1ac5ab | Alon Levy | case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
663 | 7e1ac5ab | Alon Levy | return "(generic) set address"; |
664 | 7e1ac5ab | Alon Levy | case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
665 | 7e1ac5ab | Alon Levy | return "(generic) get descriptor"; |
666 | 7e1ac5ab | Alon Levy | case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
667 | 7e1ac5ab | Alon Levy | return "(generic) get configuration"; |
668 | 7e1ac5ab | Alon Levy | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
669 | 7e1ac5ab | Alon Levy | return "(generic) set configuration"; |
670 | 7e1ac5ab | Alon Levy | case DeviceRequest | USB_REQ_GET_STATUS:
|
671 | 7e1ac5ab | Alon Levy | return "(generic) get status"; |
672 | 7e1ac5ab | Alon Levy | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
|
673 | 7e1ac5ab | Alon Levy | return "(generic) clear feature"; |
674 | 7e1ac5ab | Alon Levy | case DeviceOutRequest | USB_REQ_SET_FEATURE:
|
675 | 7e1ac5ab | Alon Levy | return "(generic) set_feature"; |
676 | 7e1ac5ab | Alon Levy | case InterfaceRequest | USB_REQ_GET_INTERFACE:
|
677 | 7e1ac5ab | Alon Levy | return "(generic) get interface"; |
678 | 7e1ac5ab | Alon Levy | case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
|
679 | 7e1ac5ab | Alon Levy | return "(generic) set interface"; |
680 | 7e1ac5ab | Alon Levy | /* class requests */
|
681 | 7e1ac5ab | Alon Levy | case ClassInterfaceOutRequest | CCID_CONTROL_ABORT:
|
682 | 7e1ac5ab | Alon Levy | return "ABORT"; |
683 | 7e1ac5ab | Alon Levy | case ClassInterfaceRequest | CCID_CONTROL_GET_CLOCK_FREQUENCIES:
|
684 | 7e1ac5ab | Alon Levy | return "GET_CLOCK_FREQUENCIES"; |
685 | 7e1ac5ab | Alon Levy | case ClassInterfaceRequest | CCID_CONTROL_GET_DATA_RATES:
|
686 | 7e1ac5ab | Alon Levy | return "GET_DATA_RATES"; |
687 | 7e1ac5ab | Alon Levy | } |
688 | 7e1ac5ab | Alon Levy | return "unknown"; |
689 | 7e1ac5ab | Alon Levy | } |
690 | 7e1ac5ab | Alon Levy | |
691 | 9a77a0f5 | Hans de Goede | static void ccid_handle_control(USBDevice *dev, USBPacket *p, int request, |
692 | 007fd62f | Hans de Goede | int value, int index, int length, uint8_t *data) |
693 | 36707144 | Alon Levy | { |
694 | 36707144 | Alon Levy | USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev); |
695 | 9a77a0f5 | Hans de Goede | int ret;
|
696 | 36707144 | Alon Levy | |
697 | 7e1ac5ab | Alon Levy | DPRINTF(s, 1, "%s: got control %s (%x), value %x\n", __func__, |
698 | 7e1ac5ab | Alon Levy | ccid_control_to_str(s, request), request, value); |
699 | 97237e0a | Gerd Hoffmann | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
700 | 97237e0a | Gerd Hoffmann | if (ret >= 0) { |
701 | 9a77a0f5 | Hans de Goede | return;
|
702 | 97237e0a | Gerd Hoffmann | } |
703 | 97237e0a | Gerd Hoffmann | |
704 | 36707144 | Alon Levy | switch (request) {
|
705 | 36707144 | Alon Levy | /* Class specific requests. */
|
706 | 693e4773 | Alon Levy | case ClassInterfaceOutRequest | CCID_CONTROL_ABORT:
|
707 | 36707144 | Alon Levy | DPRINTF(s, 1, "ccid_control abort UNIMPLEMENTED\n"); |
708 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
709 | 36707144 | Alon Levy | break;
|
710 | 693e4773 | Alon Levy | case ClassInterfaceRequest | CCID_CONTROL_GET_CLOCK_FREQUENCIES:
|
711 | 36707144 | Alon Levy | DPRINTF(s, 1, "ccid_control get clock frequencies UNIMPLEMENTED\n"); |
712 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
713 | 36707144 | Alon Levy | break;
|
714 | 693e4773 | Alon Levy | case ClassInterfaceRequest | CCID_CONTROL_GET_DATA_RATES:
|
715 | 36707144 | Alon Levy | DPRINTF(s, 1, "ccid_control get data rates UNIMPLEMENTED\n"); |
716 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
717 | 36707144 | Alon Levy | break;
|
718 | 36707144 | Alon Levy | default:
|
719 | 36707144 | Alon Levy | DPRINTF(s, 1, "got unsupported/bogus control %x, value %x\n", |
720 | 36707144 | Alon Levy | request, value); |
721 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
722 | 36707144 | Alon Levy | break;
|
723 | 36707144 | Alon Levy | } |
724 | 36707144 | Alon Levy | } |
725 | 36707144 | Alon Levy | |
726 | 36707144 | Alon Levy | static bool ccid_card_inserted(USBCCIDState *s) |
727 | 36707144 | Alon Levy | { |
728 | 36707144 | Alon Levy | return s->bmSlotICCState & SLOT_0_STATE_MASK;
|
729 | 36707144 | Alon Levy | } |
730 | 36707144 | Alon Levy | |
731 | 36707144 | Alon Levy | static uint8_t ccid_card_status(USBCCIDState *s)
|
732 | 36707144 | Alon Levy | { |
733 | 36707144 | Alon Levy | return ccid_card_inserted(s)
|
734 | 36707144 | Alon Levy | ? (s->powered ? |
735 | 36707144 | Alon Levy | ICC_STATUS_PRESENT_ACTIVE |
736 | 36707144 | Alon Levy | : ICC_STATUS_PRESENT_INACTIVE |
737 | 36707144 | Alon Levy | ) |
738 | 36707144 | Alon Levy | : ICC_STATUS_NOT_PRESENT; |
739 | 36707144 | Alon Levy | } |
740 | 36707144 | Alon Levy | |
741 | 36707144 | Alon Levy | static uint8_t ccid_calc_status(USBCCIDState *s)
|
742 | 36707144 | Alon Levy | { |
743 | 36707144 | Alon Levy | /*
|
744 | 36707144 | Alon Levy | * page 55, 6.2.6, calculation of bStatus from bmICCStatus and
|
745 | 36707144 | Alon Levy | * bmCommandStatus
|
746 | 36707144 | Alon Levy | */
|
747 | 36707144 | Alon Levy | uint8_t ret = ccid_card_status(s) | (s->bmCommandStatus << 6);
|
748 | 7e1ac5ab | Alon Levy | DPRINTF(s, D_VERBOSE, "%s: status = %d\n", __func__, ret);
|
749 | 36707144 | Alon Levy | return ret;
|
750 | 36707144 | Alon Levy | } |
751 | 36707144 | Alon Levy | |
752 | 36707144 | Alon Levy | static void ccid_reset_error_status(USBCCIDState *s) |
753 | 36707144 | Alon Levy | { |
754 | 36707144 | Alon Levy | s->bError = ERROR_CMD_NOT_SUPPORTED; |
755 | 36707144 | Alon Levy | s->bmCommandStatus = COMMAND_STATUS_NO_ERROR; |
756 | 36707144 | Alon Levy | } |
757 | 36707144 | Alon Levy | |
758 | 36707144 | Alon Levy | static void ccid_write_slot_status(USBCCIDState *s, CCID_Header *recv) |
759 | 36707144 | Alon Levy | { |
760 | 36707144 | Alon Levy | CCID_SlotStatus *h = ccid_reserve_recv_buf(s, sizeof(CCID_SlotStatus));
|
761 | 36707144 | Alon Levy | if (h == NULL) { |
762 | 36707144 | Alon Levy | return;
|
763 | 36707144 | Alon Levy | } |
764 | 36707144 | Alon Levy | h->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus; |
765 | 36707144 | Alon Levy | h->b.hdr.dwLength = 0;
|
766 | 36707144 | Alon Levy | h->b.hdr.bSlot = recv->bSlot; |
767 | 36707144 | Alon Levy | h->b.hdr.bSeq = recv->bSeq; |
768 | 36707144 | Alon Levy | h->b.bStatus = ccid_calc_status(s); |
769 | 36707144 | Alon Levy | h->b.bError = s->bError; |
770 | 36707144 | Alon Levy | h->bClockStatus = CLOCK_STATUS_RUNNING; |
771 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
772 | 36707144 | Alon Levy | } |
773 | 36707144 | Alon Levy | |
774 | 36707144 | Alon Levy | static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv) |
775 | 36707144 | Alon Levy | { |
776 | 36707144 | Alon Levy | CCID_Parameter *h; |
777 | 36707144 | Alon Levy | uint32_t len = s->ulProtocolDataStructureSize; |
778 | 36707144 | Alon Levy | |
779 | 36707144 | Alon Levy | h = ccid_reserve_recv_buf(s, sizeof(CCID_Parameter) + len);
|
780 | 36707144 | Alon Levy | if (h == NULL) { |
781 | 36707144 | Alon Levy | return;
|
782 | 36707144 | Alon Levy | } |
783 | 36707144 | Alon Levy | h->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_Parameters; |
784 | 36707144 | Alon Levy | h->b.hdr.dwLength = 0;
|
785 | 36707144 | Alon Levy | h->b.hdr.bSlot = recv->bSlot; |
786 | 36707144 | Alon Levy | h->b.hdr.bSeq = recv->bSeq; |
787 | 36707144 | Alon Levy | h->b.bStatus = ccid_calc_status(s); |
788 | 36707144 | Alon Levy | h->b.bError = s->bError; |
789 | 36707144 | Alon Levy | h->bProtocolNum = s->bProtocolNum; |
790 | 4942d6c3 | Alon Levy | h->abProtocolDataStructure = s->abProtocolDataStructure; |
791 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
792 | 36707144 | Alon Levy | } |
793 | 36707144 | Alon Levy | |
794 | 36707144 | Alon Levy | static void ccid_write_data_block(USBCCIDState *s, uint8_t slot, uint8_t seq, |
795 | 36707144 | Alon Levy | const uint8_t *data, uint32_t len)
|
796 | 36707144 | Alon Levy | { |
797 | 36707144 | Alon Levy | CCID_DataBlock *p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
|
798 | 36707144 | Alon Levy | |
799 | 36707144 | Alon Levy | if (p == NULL) { |
800 | 36707144 | Alon Levy | return;
|
801 | 36707144 | Alon Levy | } |
802 | 36707144 | Alon Levy | p->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock; |
803 | 36707144 | Alon Levy | p->b.hdr.dwLength = cpu_to_le32(len); |
804 | 36707144 | Alon Levy | p->b.hdr.bSlot = slot; |
805 | 36707144 | Alon Levy | p->b.hdr.bSeq = seq; |
806 | 36707144 | Alon Levy | p->b.bStatus = ccid_calc_status(s); |
807 | 36707144 | Alon Levy | p->b.bError = s->bError; |
808 | 36707144 | Alon Levy | if (p->b.bError) {
|
809 | 7e1ac5ab | Alon Levy | DPRINTF(s, D_VERBOSE, "error %d\n", p->b.bError);
|
810 | 36707144 | Alon Levy | } |
811 | 36707144 | Alon Levy | memcpy(p->abData, data, len); |
812 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
813 | 36707144 | Alon Levy | } |
814 | 36707144 | Alon Levy | |
815 | 47bf53af | Alon Levy | static void ccid_report_error_failed(USBCCIDState *s, uint8_t error) |
816 | 47bf53af | Alon Levy | { |
817 | 47bf53af | Alon Levy | s->bmCommandStatus = COMMAND_STATUS_FAILED; |
818 | 47bf53af | Alon Levy | s->bError = error; |
819 | 47bf53af | Alon Levy | } |
820 | 47bf53af | Alon Levy | |
821 | 36707144 | Alon Levy | static void ccid_write_data_block_answer(USBCCIDState *s, |
822 | 36707144 | Alon Levy | const uint8_t *data, uint32_t len)
|
823 | 36707144 | Alon Levy | { |
824 | 36707144 | Alon Levy | uint8_t seq; |
825 | 36707144 | Alon Levy | uint8_t slot; |
826 | 36707144 | Alon Levy | |
827 | 36707144 | Alon Levy | if (!ccid_has_pending_answers(s)) {
|
828 | 47bf53af | Alon Levy | DPRINTF(s, D_WARN, "error: no pending answer to return to guest\n");
|
829 | 47bf53af | Alon Levy | ccid_report_error_failed(s, ERROR_ICC_MUTE); |
830 | 47bf53af | Alon Levy | return;
|
831 | 36707144 | Alon Levy | } |
832 | 36707144 | Alon Levy | ccid_remove_pending_answer(s, &slot, &seq); |
833 | 36707144 | Alon Levy | ccid_write_data_block(s, slot, seq, data, len); |
834 | 36707144 | Alon Levy | } |
835 | 36707144 | Alon Levy | |
836 | 2f8f916b | Alon Levy | static uint8_t atr_get_protocol_num(const uint8_t *atr, uint32_t len) |
837 | 2f8f916b | Alon Levy | { |
838 | 2f8f916b | Alon Levy | int i;
|
839 | 2f8f916b | Alon Levy | |
840 | 2f8f916b | Alon Levy | if (len < 2 || !(atr[1] & 0x80)) { |
841 | 2f8f916b | Alon Levy | /* too short or TD1 not included */
|
842 | 2f8f916b | Alon Levy | return 0; /* T=0, default */ |
843 | 2f8f916b | Alon Levy | } |
844 | 2f8f916b | Alon Levy | i = 1 + !!(atr[1] & 0x10) + !!(atr[1] & 0x20) + !!(atr[1] & 0x40); |
845 | 2f8f916b | Alon Levy | i += !!(atr[1] & 0x80); |
846 | 2f8f916b | Alon Levy | return atr[i] & 0x0f; |
847 | 2f8f916b | Alon Levy | } |
848 | 2f8f916b | Alon Levy | |
849 | 36707144 | Alon Levy | static void ccid_write_data_block_atr(USBCCIDState *s, CCID_Header *recv) |
850 | 36707144 | Alon Levy | { |
851 | 36707144 | Alon Levy | const uint8_t *atr = NULL; |
852 | 36707144 | Alon Levy | uint32_t len = 0;
|
853 | 2f8f916b | Alon Levy | uint8_t atr_protocol_num; |
854 | 2f8f916b | Alon Levy | CCID_T0ProtocolDataStructure *t0 = &s->abProtocolDataStructure.t0; |
855 | 2f8f916b | Alon Levy | CCID_T1ProtocolDataStructure *t1 = &s->abProtocolDataStructure.t1; |
856 | 36707144 | Alon Levy | |
857 | 36707144 | Alon Levy | if (s->card) {
|
858 | ba7c0520 | Anthony Liguori | atr = ccid_card_get_atr(s->card, &len); |
859 | 36707144 | Alon Levy | } |
860 | 2f8f916b | Alon Levy | atr_protocol_num = atr_get_protocol_num(atr, len); |
861 | 2f8f916b | Alon Levy | DPRINTF(s, D_VERBOSE, "%s: atr contains protocol=%d\n", __func__,
|
862 | 2f8f916b | Alon Levy | atr_protocol_num); |
863 | 2f8f916b | Alon Levy | /* set parameters from ATR - see spec page 109 */
|
864 | 2f8f916b | Alon Levy | s->bProtocolNum = (atr_protocol_num <= 1 ? atr_protocol_num
|
865 | 2f8f916b | Alon Levy | : s->bProtocolNum); |
866 | 2f8f916b | Alon Levy | switch (atr_protocol_num) {
|
867 | 2f8f916b | Alon Levy | case 0: |
868 | 2f8f916b | Alon Levy | /* TODO: unimplemented ATR T0 parameters */
|
869 | 2f8f916b | Alon Levy | t0->bmFindexDindex = 0;
|
870 | 2f8f916b | Alon Levy | t0->bmTCCKST0 = 0;
|
871 | 2f8f916b | Alon Levy | t0->bGuardTimeT0 = 0;
|
872 | 2f8f916b | Alon Levy | t0->bWaitingIntegerT0 = 0;
|
873 | 2f8f916b | Alon Levy | t0->bClockStop = 0;
|
874 | 2f8f916b | Alon Levy | break;
|
875 | 2f8f916b | Alon Levy | case 1: |
876 | 2f8f916b | Alon Levy | /* TODO: unimplemented ATR T1 parameters */
|
877 | 2f8f916b | Alon Levy | t1->bmFindexDindex = 0;
|
878 | 2f8f916b | Alon Levy | t1->bmTCCKST1 = 0;
|
879 | 2f8f916b | Alon Levy | t1->bGuardTimeT1 = 0;
|
880 | 2f8f916b | Alon Levy | t1->bWaitingIntegerT1 = 0;
|
881 | 2f8f916b | Alon Levy | t1->bClockStop = 0;
|
882 | 2f8f916b | Alon Levy | t1->bIFSC = 0;
|
883 | 2f8f916b | Alon Levy | t1->bNadValue = 0;
|
884 | 2f8f916b | Alon Levy | break;
|
885 | 2f8f916b | Alon Levy | default:
|
886 | 2f8f916b | Alon Levy | DPRINTF(s, D_WARN, "%s: error: unsupported ATR protocol %d\n",
|
887 | 2f8f916b | Alon Levy | __func__, atr_protocol_num); |
888 | 2f8f916b | Alon Levy | } |
889 | 36707144 | Alon Levy | ccid_write_data_block(s, recv->bSlot, recv->bSeq, atr, len); |
890 | 36707144 | Alon Levy | } |
891 | 36707144 | Alon Levy | |
892 | 36707144 | Alon Levy | static void ccid_set_parameters(USBCCIDState *s, CCID_Header *recv) |
893 | 36707144 | Alon Levy | { |
894 | 36707144 | Alon Levy | CCID_SetParameters *ph = (CCID_SetParameters *) recv; |
895 | 4942d6c3 | Alon Levy | uint32_t protocol_num = ph->bProtocolNum & 3;
|
896 | 4942d6c3 | Alon Levy | |
897 | 4942d6c3 | Alon Levy | if (protocol_num != 0 && protocol_num != 1) { |
898 | 4942d6c3 | Alon Levy | ccid_report_error_failed(s, ERROR_CMD_NOT_SUPPORTED); |
899 | 36707144 | Alon Levy | return;
|
900 | 36707144 | Alon Levy | } |
901 | 4942d6c3 | Alon Levy | s->bProtocolNum = protocol_num; |
902 | 4942d6c3 | Alon Levy | s->abProtocolDataStructure = ph->abProtocolDataStructure; |
903 | 36707144 | Alon Levy | } |
904 | 36707144 | Alon Levy | |
905 | 36707144 | Alon Levy | /*
|
906 | 36707144 | Alon Levy | * must be 5 bytes for T=0, 7 bytes for T=1
|
907 | 36707144 | Alon Levy | * See page 52
|
908 | 36707144 | Alon Levy | */
|
909 | 4942d6c3 | Alon Levy | static const CCID_ProtocolDataStructure defaultProtocolDataStructure = { |
910 | 4942d6c3 | Alon Levy | .t1 = { |
911 | 4942d6c3 | Alon Levy | .bmFindexDindex = 0x77,
|
912 | 4942d6c3 | Alon Levy | .bmTCCKST1 = 0x00,
|
913 | 4942d6c3 | Alon Levy | .bGuardTimeT1 = 0x00,
|
914 | 4942d6c3 | Alon Levy | .bWaitingIntegerT1 = 0x00,
|
915 | 4942d6c3 | Alon Levy | .bClockStop = 0x00,
|
916 | 4942d6c3 | Alon Levy | .bIFSC = 0xfe,
|
917 | 4942d6c3 | Alon Levy | .bNadValue = 0x00,
|
918 | 4942d6c3 | Alon Levy | } |
919 | 4942d6c3 | Alon Levy | }; |
920 | 36707144 | Alon Levy | |
921 | 36707144 | Alon Levy | static void ccid_reset_parameters(USBCCIDState *s) |
922 | 36707144 | Alon Levy | { |
923 | d7d218ef | Alon Levy | s->bProtocolNum = 0; /* T=0 */ |
924 | 4942d6c3 | Alon Levy | s->abProtocolDataStructure = defaultProtocolDataStructure; |
925 | 36707144 | Alon Levy | } |
926 | 36707144 | Alon Levy | |
927 | 36707144 | Alon Levy | /* NOTE: only a single slot is supported (SLOT_0) */
|
928 | 36707144 | Alon Levy | static void ccid_on_slot_change(USBCCIDState *s, bool full) |
929 | 36707144 | Alon Levy | { |
930 | 36707144 | Alon Levy | /* RDR_to_PC_NotifySlotChange, 6.3.1 page 56 */
|
931 | 36707144 | Alon Levy | uint8_t current = s->bmSlotICCState; |
932 | 36707144 | Alon Levy | if (full) {
|
933 | 36707144 | Alon Levy | s->bmSlotICCState |= SLOT_0_STATE_MASK; |
934 | 36707144 | Alon Levy | } else {
|
935 | 36707144 | Alon Levy | s->bmSlotICCState &= ~SLOT_0_STATE_MASK; |
936 | 36707144 | Alon Levy | } |
937 | 36707144 | Alon Levy | if (current != s->bmSlotICCState) {
|
938 | 36707144 | Alon Levy | s->bmSlotICCState |= SLOT_0_CHANGED_MASK; |
939 | 36707144 | Alon Levy | } |
940 | 36707144 | Alon Levy | s->notify_slot_change = true;
|
941 | 8550a02d | Gerd Hoffmann | usb_wakeup(s->intr, 0);
|
942 | 36707144 | Alon Levy | } |
943 | 36707144 | Alon Levy | |
944 | 36707144 | Alon Levy | static void ccid_write_data_block_error( |
945 | 36707144 | Alon Levy | USBCCIDState *s, uint8_t slot, uint8_t seq) |
946 | 36707144 | Alon Levy | { |
947 | 36707144 | Alon Levy | ccid_write_data_block(s, slot, seq, NULL, 0); |
948 | 36707144 | Alon Levy | } |
949 | 36707144 | Alon Levy | |
950 | 36707144 | Alon Levy | static void ccid_on_apdu_from_guest(USBCCIDState *s, CCID_XferBlock *recv) |
951 | 36707144 | Alon Levy | { |
952 | 36707144 | Alon Levy | uint32_t len; |
953 | 36707144 | Alon Levy | |
954 | 36707144 | Alon Levy | if (ccid_card_status(s) != ICC_STATUS_PRESENT_ACTIVE) {
|
955 | 36707144 | Alon Levy | DPRINTF(s, 1,
|
956 | 36707144 | Alon Levy | "usb-ccid: not sending apdu to client, no card connected\n");
|
957 | 36707144 | Alon Levy | ccid_write_data_block_error(s, recv->hdr.bSlot, recv->hdr.bSeq); |
958 | 36707144 | Alon Levy | return;
|
959 | 36707144 | Alon Levy | } |
960 | 36707144 | Alon Levy | len = le32_to_cpu(recv->hdr.dwLength); |
961 | 36707144 | Alon Levy | DPRINTF(s, 1, "%s: seq %d, len %d\n", __func__, |
962 | 36707144 | Alon Levy | recv->hdr.bSeq, len); |
963 | 36707144 | Alon Levy | ccid_add_pending_answer(s, (CCID_Header *)recv); |
964 | 36707144 | Alon Levy | if (s->card) {
|
965 | ba7c0520 | Anthony Liguori | ccid_card_apdu_from_guest(s->card, recv->abData, len); |
966 | 36707144 | Alon Levy | } else {
|
967 | 36707144 | Alon Levy | DPRINTF(s, D_WARN, "warning: discarded apdu\n");
|
968 | 36707144 | Alon Levy | } |
969 | 36707144 | Alon Levy | } |
970 | 36707144 | Alon Levy | |
971 | 7e1ac5ab | Alon Levy | static const char *ccid_message_type_to_str(uint8_t type) |
972 | 7e1ac5ab | Alon Levy | { |
973 | 7e1ac5ab | Alon Levy | switch (type) {
|
974 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn: return "IccPowerOn"; |
975 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff: return "IccPowerOff"; |
976 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus: return "GetSlotStatus"; |
977 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock: return "XfrBlock"; |
978 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters: return "GetParameters"; |
979 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters: return "ResetParameters"; |
980 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters: return "SetParameters"; |
981 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_Escape: return "Escape"; |
982 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_IccClock: return "IccClock"; |
983 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU: return "T0APDU"; |
984 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_Secure: return "Secure"; |
985 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical: return "Mechanical"; |
986 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_Abort: return "Abort"; |
987 | 7e1ac5ab | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency:
|
988 | 7e1ac5ab | Alon Levy | return "SetDataRateAndClockFrequency"; |
989 | 7e1ac5ab | Alon Levy | } |
990 | 7e1ac5ab | Alon Levy | return "unknown"; |
991 | 7e1ac5ab | Alon Levy | } |
992 | 7e1ac5ab | Alon Levy | |
993 | 9a77a0f5 | Hans de Goede | static void ccid_handle_bulk_out(USBCCIDState *s, USBPacket *p) |
994 | 36707144 | Alon Levy | { |
995 | 36707144 | Alon Levy | CCID_Header *ccid_header; |
996 | 36707144 | Alon Levy | |
997 | 4f4321c1 | Gerd Hoffmann | if (p->iov.size + s->bulk_out_pos > BULK_OUT_DATA_SIZE) {
|
998 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
999 | 9a77a0f5 | Hans de Goede | return;
|
1000 | 36707144 | Alon Levy | } |
1001 | 36707144 | Alon Levy | ccid_header = (CCID_Header *)s->bulk_out_data; |
1002 | 4f4321c1 | Gerd Hoffmann | usb_packet_copy(p, s->bulk_out_data + s->bulk_out_pos, p->iov.size); |
1003 | 4f4321c1 | Gerd Hoffmann | s->bulk_out_pos += p->iov.size; |
1004 | 4f4321c1 | Gerd Hoffmann | if (p->iov.size == CCID_MAX_PACKET_SIZE) {
|
1005 | 36707144 | Alon Levy | DPRINTF(s, D_VERBOSE, |
1006 | 4f4321c1 | Gerd Hoffmann | "usb-ccid: bulk_in: expecting more packets (%zd/%d)\n",
|
1007 | 4f4321c1 | Gerd Hoffmann | p->iov.size, ccid_header->dwLength); |
1008 | 9a77a0f5 | Hans de Goede | return;
|
1009 | 36707144 | Alon Levy | } |
1010 | 36707144 | Alon Levy | if (s->bulk_out_pos < 10) { |
1011 | 36707144 | Alon Levy | DPRINTF(s, 1,
|
1012 | 36707144 | Alon Levy | "%s: bad USB_TOKEN_OUT length, should be at least 10 bytes\n",
|
1013 | 36707144 | Alon Levy | __func__); |
1014 | 36707144 | Alon Levy | } else {
|
1015 | 7e1ac5ab | Alon Levy | DPRINTF(s, D_MORE_INFO, "%s %x %s\n", __func__,
|
1016 | 7e1ac5ab | Alon Levy | ccid_header->bMessageType, |
1017 | 7e1ac5ab | Alon Levy | ccid_message_type_to_str(ccid_header->bMessageType)); |
1018 | 36707144 | Alon Levy | switch (ccid_header->bMessageType) {
|
1019 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus:
|
1020 | 36707144 | Alon Levy | ccid_write_slot_status(s, ccid_header); |
1021 | 36707144 | Alon Levy | break;
|
1022 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn:
|
1023 | 7e1ac5ab | Alon Levy | DPRINTF(s, 1, "%s: PowerOn: %d\n", __func__, |
1024 | 36707144 | Alon Levy | ((CCID_IccPowerOn *)(ccid_header))->bPowerSelect); |
1025 | 36707144 | Alon Levy | s->powered = true;
|
1026 | 36707144 | Alon Levy | if (!ccid_card_inserted(s)) {
|
1027 | 36707144 | Alon Levy | ccid_report_error_failed(s, ERROR_ICC_MUTE); |
1028 | 36707144 | Alon Levy | } |
1029 | 36707144 | Alon Levy | /* atr is written regardless of error. */
|
1030 | 36707144 | Alon Levy | ccid_write_data_block_atr(s, ccid_header); |
1031 | 36707144 | Alon Levy | break;
|
1032 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff:
|
1033 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
1034 | 36707144 | Alon Levy | s->powered = false;
|
1035 | 36707144 | Alon Levy | ccid_write_slot_status(s, ccid_header); |
1036 | 36707144 | Alon Levy | break;
|
1037 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock:
|
1038 | 36707144 | Alon Levy | ccid_on_apdu_from_guest(s, (CCID_XferBlock *)s->bulk_out_data); |
1039 | 36707144 | Alon Levy | break;
|
1040 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters:
|
1041 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
1042 | 36707144 | Alon Levy | ccid_set_parameters(s, ccid_header); |
1043 | 36707144 | Alon Levy | ccid_write_parameters(s, ccid_header); |
1044 | 36707144 | Alon Levy | break;
|
1045 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters:
|
1046 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
1047 | 36707144 | Alon Levy | ccid_reset_parameters(s); |
1048 | 36707144 | Alon Levy | ccid_write_parameters(s, ccid_header); |
1049 | 36707144 | Alon Levy | break;
|
1050 | 36707144 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters:
|
1051 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
1052 | 36707144 | Alon Levy | ccid_write_parameters(s, ccid_header); |
1053 | 36707144 | Alon Levy | break;
|
1054 | 58aeda15 | Alon Levy | case CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical:
|
1055 | 58aeda15 | Alon Levy | ccid_report_error_failed(s, 0);
|
1056 | 58aeda15 | Alon Levy | ccid_write_slot_status(s, ccid_header); |
1057 | 58aeda15 | Alon Levy | break;
|
1058 | 36707144 | Alon Levy | default:
|
1059 | 36707144 | Alon Levy | DPRINTF(s, 1,
|
1060 | 36707144 | Alon Levy | "handle_data: ERROR: unhandled message type %Xh\n",
|
1061 | 36707144 | Alon Levy | ccid_header->bMessageType); |
1062 | 36707144 | Alon Levy | /*
|
1063 | 36707144 | Alon Levy | * The caller is expecting the device to respond, tell it we
|
1064 | 36707144 | Alon Levy | * don't support the operation.
|
1065 | 36707144 | Alon Levy | */
|
1066 | 36707144 | Alon Levy | ccid_report_error_failed(s, ERROR_CMD_NOT_SUPPORTED); |
1067 | 36707144 | Alon Levy | ccid_write_slot_status(s, ccid_header); |
1068 | 36707144 | Alon Levy | break;
|
1069 | 36707144 | Alon Levy | } |
1070 | 36707144 | Alon Levy | } |
1071 | 36707144 | Alon Levy | s->bulk_out_pos = 0;
|
1072 | 36707144 | Alon Levy | } |
1073 | 36707144 | Alon Levy | |
1074 | 9a77a0f5 | Hans de Goede | static void ccid_bulk_in_copy_to_guest(USBCCIDState *s, USBPacket *p) |
1075 | 36707144 | Alon Levy | { |
1076 | 9a77a0f5 | Hans de Goede | int len = 0; |
1077 | 36707144 | Alon Levy | |
1078 | 36707144 | Alon Levy | ccid_bulk_in_get(s); |
1079 | 36707144 | Alon Levy | if (s->current_bulk_in != NULL) { |
1080 | 9a77a0f5 | Hans de Goede | len = MIN(s->current_bulk_in->len - s->current_bulk_in->pos, |
1081 | 4f4321c1 | Gerd Hoffmann | p->iov.size); |
1082 | 4f4321c1 | Gerd Hoffmann | usb_packet_copy(p, s->current_bulk_in->data + |
1083 | 9a77a0f5 | Hans de Goede | s->current_bulk_in->pos, len); |
1084 | 9a77a0f5 | Hans de Goede | s->current_bulk_in->pos += len; |
1085 | 36707144 | Alon Levy | if (s->current_bulk_in->pos == s->current_bulk_in->len) {
|
1086 | 36707144 | Alon Levy | ccid_bulk_in_release(s); |
1087 | 36707144 | Alon Levy | } |
1088 | 36707144 | Alon Levy | } else {
|
1089 | 36707144 | Alon Levy | /* return when device has no data - usb 2.0 spec Table 8-4 */
|
1090 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_NAK; |
1091 | 36707144 | Alon Levy | } |
1092 | 9a77a0f5 | Hans de Goede | if (len) {
|
1093 | 36707144 | Alon Levy | DPRINTF(s, D_MORE_INFO, |
1094 | 4f4321c1 | Gerd Hoffmann | "%s: %zd/%d req/act to guest (BULK_IN)\n",
|
1095 | 9a77a0f5 | Hans de Goede | __func__, p->iov.size, len); |
1096 | 36707144 | Alon Levy | } |
1097 | 9a77a0f5 | Hans de Goede | if (len < p->iov.size) {
|
1098 | 36707144 | Alon Levy | DPRINTF(s, 1,
|
1099 | 4f4321c1 | Gerd Hoffmann | "%s: returning short (EREMOTEIO) %d < %zd\n",
|
1100 | 9a77a0f5 | Hans de Goede | __func__, len, p->iov.size); |
1101 | 36707144 | Alon Levy | } |
1102 | 36707144 | Alon Levy | } |
1103 | 36707144 | Alon Levy | |
1104 | 9a77a0f5 | Hans de Goede | static void ccid_handle_data(USBDevice *dev, USBPacket *p) |
1105 | 36707144 | Alon Levy | { |
1106 | 36707144 | Alon Levy | USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev); |
1107 | 4f4321c1 | Gerd Hoffmann | uint8_t buf[2];
|
1108 | 36707144 | Alon Levy | |
1109 | 36707144 | Alon Levy | switch (p->pid) {
|
1110 | 36707144 | Alon Levy | case USB_TOKEN_OUT:
|
1111 | 9a77a0f5 | Hans de Goede | ccid_handle_bulk_out(s, p); |
1112 | 36707144 | Alon Levy | break;
|
1113 | 36707144 | Alon Levy | |
1114 | 36707144 | Alon Levy | case USB_TOKEN_IN:
|
1115 | 079d0b7f | Gerd Hoffmann | switch (p->ep->nr) {
|
1116 | 36707144 | Alon Levy | case CCID_BULK_IN_EP:
|
1117 | 9a77a0f5 | Hans de Goede | ccid_bulk_in_copy_to_guest(s, p); |
1118 | 36707144 | Alon Levy | break;
|
1119 | 36707144 | Alon Levy | case CCID_INT_IN_EP:
|
1120 | 36707144 | Alon Levy | if (s->notify_slot_change) {
|
1121 | 36707144 | Alon Levy | /* page 56, RDR_to_PC_NotifySlotChange */
|
1122 | 4f4321c1 | Gerd Hoffmann | buf[0] = CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange;
|
1123 | 4f4321c1 | Gerd Hoffmann | buf[1] = s->bmSlotICCState;
|
1124 | 4f4321c1 | Gerd Hoffmann | usb_packet_copy(p, buf, 2);
|
1125 | 36707144 | Alon Levy | s->notify_slot_change = false;
|
1126 | 36707144 | Alon Levy | s->bmSlotICCState &= ~SLOT_0_CHANGED_MASK; |
1127 | 36707144 | Alon Levy | DPRINTF(s, D_INFO, |
1128 | 36707144 | Alon Levy | "handle_data: int_in: notify_slot_change %X, "
|
1129 | 4f4321c1 | Gerd Hoffmann | "requested len %zd\n",
|
1130 | 4f4321c1 | Gerd Hoffmann | s->bmSlotICCState, p->iov.size); |
1131 | c4020746 | Hans de Goede | } else {
|
1132 | c4020746 | Hans de Goede | p->status = USB_RET_NAK; |
1133 | 36707144 | Alon Levy | } |
1134 | 36707144 | Alon Levy | break;
|
1135 | 36707144 | Alon Levy | default:
|
1136 | 36707144 | Alon Levy | DPRINTF(s, 1, "Bad endpoint\n"); |
1137 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
1138 | 36707144 | Alon Levy | break;
|
1139 | 36707144 | Alon Levy | } |
1140 | 36707144 | Alon Levy | break;
|
1141 | 36707144 | Alon Levy | default:
|
1142 | 36707144 | Alon Levy | DPRINTF(s, 1, "Bad token\n"); |
1143 | 9a77a0f5 | Hans de Goede | p->status = USB_RET_STALL; |
1144 | 36707144 | Alon Levy | break;
|
1145 | 36707144 | Alon Levy | } |
1146 | 36707144 | Alon Levy | } |
1147 | 36707144 | Alon Levy | |
1148 | 36707144 | Alon Levy | static void ccid_handle_destroy(USBDevice *dev) |
1149 | 36707144 | Alon Levy | { |
1150 | 36707144 | Alon Levy | USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev); |
1151 | 36707144 | Alon Levy | |
1152 | 36707144 | Alon Levy | ccid_bulk_in_clear(s); |
1153 | 36707144 | Alon Levy | } |
1154 | 36707144 | Alon Levy | |
1155 | 36707144 | Alon Levy | static void ccid_flush_pending_answers(USBCCIDState *s) |
1156 | 36707144 | Alon Levy | { |
1157 | 36707144 | Alon Levy | while (ccid_has_pending_answers(s)) {
|
1158 | 36707144 | Alon Levy | ccid_write_data_block_answer(s, NULL, 0); |
1159 | 36707144 | Alon Levy | } |
1160 | 36707144 | Alon Levy | } |
1161 | 36707144 | Alon Levy | |
1162 | 36707144 | Alon Levy | static Answer *ccid_peek_next_answer(USBCCIDState *s)
|
1163 | 36707144 | Alon Levy | { |
1164 | 36707144 | Alon Levy | return s->pending_answers_num == 0 |
1165 | 36707144 | Alon Levy | ? NULL
|
1166 | 36707144 | Alon Levy | : &s->pending_answers[s->pending_answers_start % PENDING_ANSWERS_NUM]; |
1167 | 36707144 | Alon Levy | } |
1168 | 36707144 | Alon Levy | |
1169 | 3cb75a7c | Paolo Bonzini | static Property ccid_props[] = {
|
1170 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_UINT32("slot", struct CCIDCardState, slot, 0), |
1171 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_END_OF_LIST(), |
1172 | 3cb75a7c | Paolo Bonzini | }; |
1173 | 3cb75a7c | Paolo Bonzini | |
1174 | 0d936928 | Anthony Liguori | #define TYPE_CCID_BUS "ccid-bus" |
1175 | 0d936928 | Anthony Liguori | #define CCID_BUS(obj) OBJECT_CHECK(CCIDBus, (obj), TYPE_CCID_BUS)
|
1176 | 0d936928 | Anthony Liguori | |
1177 | 0d936928 | Anthony Liguori | static const TypeInfo ccid_bus_info = { |
1178 | 0d936928 | Anthony Liguori | .name = TYPE_CCID_BUS, |
1179 | 0d936928 | Anthony Liguori | .parent = TYPE_BUS, |
1180 | 0d936928 | Anthony Liguori | .instance_size = sizeof(CCIDBus),
|
1181 | 36707144 | Alon Levy | }; |
1182 | 36707144 | Alon Levy | |
1183 | 36707144 | Alon Levy | void ccid_card_send_apdu_to_guest(CCIDCardState *card,
|
1184 | 36707144 | Alon Levy | uint8_t *apdu, uint32_t len) |
1185 | 36707144 | Alon Levy | { |
1186 | 36707144 | Alon Levy | USBCCIDState *s = DO_UPCAST(USBCCIDState, dev.qdev, |
1187 | 36707144 | Alon Levy | card->qdev.parent_bus->parent); |
1188 | 36707144 | Alon Levy | Answer *answer; |
1189 | 36707144 | Alon Levy | |
1190 | 36707144 | Alon Levy | if (!ccid_has_pending_answers(s)) {
|
1191 | 36707144 | Alon Levy | DPRINTF(s, 1, "CCID ERROR: got an APDU without pending answers\n"); |
1192 | 36707144 | Alon Levy | return;
|
1193 | 36707144 | Alon Levy | } |
1194 | 36707144 | Alon Levy | s->bmCommandStatus = COMMAND_STATUS_NO_ERROR; |
1195 | 36707144 | Alon Levy | answer = ccid_peek_next_answer(s); |
1196 | 36707144 | Alon Levy | if (answer == NULL) { |
1197 | 47bf53af | Alon Levy | DPRINTF(s, D_WARN, "%s: error: unexpected lack of answer\n", __func__);
|
1198 | 47bf53af | Alon Levy | ccid_report_error_failed(s, ERROR_HW_ERROR); |
1199 | 47bf53af | Alon Levy | return;
|
1200 | 36707144 | Alon Levy | } |
1201 | 36707144 | Alon Levy | DPRINTF(s, 1, "APDU returned to guest %d (answer seq %d, slot %d)\n", |
1202 | 36707144 | Alon Levy | len, answer->seq, answer->slot); |
1203 | 36707144 | Alon Levy | ccid_write_data_block_answer(s, apdu, len); |
1204 | 36707144 | Alon Levy | } |
1205 | 36707144 | Alon Levy | |
1206 | 36707144 | Alon Levy | void ccid_card_card_removed(CCIDCardState *card)
|
1207 | 36707144 | Alon Levy | { |
1208 | 36707144 | Alon Levy | USBCCIDState *s = |
1209 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1210 | 36707144 | Alon Levy | |
1211 | 36707144 | Alon Levy | ccid_on_slot_change(s, false);
|
1212 | 36707144 | Alon Levy | ccid_flush_pending_answers(s); |
1213 | 36707144 | Alon Levy | ccid_reset(s); |
1214 | 36707144 | Alon Levy | } |
1215 | 36707144 | Alon Levy | |
1216 | 36707144 | Alon Levy | int ccid_card_ccid_attach(CCIDCardState *card)
|
1217 | 36707144 | Alon Levy | { |
1218 | 36707144 | Alon Levy | USBCCIDState *s = |
1219 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1220 | 36707144 | Alon Levy | |
1221 | 36707144 | Alon Levy | DPRINTF(s, 1, "CCID Attach\n"); |
1222 | 36707144 | Alon Levy | if (s->migration_state == MIGRATION_MIGRATED) {
|
1223 | 36707144 | Alon Levy | s->migration_state = MIGRATION_NONE; |
1224 | 36707144 | Alon Levy | } |
1225 | 36707144 | Alon Levy | return 0; |
1226 | 36707144 | Alon Levy | } |
1227 | 36707144 | Alon Levy | |
1228 | 36707144 | Alon Levy | void ccid_card_ccid_detach(CCIDCardState *card)
|
1229 | 36707144 | Alon Levy | { |
1230 | 36707144 | Alon Levy | USBCCIDState *s = |
1231 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1232 | 36707144 | Alon Levy | |
1233 | 36707144 | Alon Levy | DPRINTF(s, 1, "CCID Detach\n"); |
1234 | 36707144 | Alon Levy | if (ccid_card_inserted(s)) {
|
1235 | 36707144 | Alon Levy | ccid_on_slot_change(s, false);
|
1236 | 36707144 | Alon Levy | } |
1237 | 36707144 | Alon Levy | ccid_detach(s); |
1238 | 36707144 | Alon Levy | } |
1239 | 36707144 | Alon Levy | |
1240 | 36707144 | Alon Levy | void ccid_card_card_error(CCIDCardState *card, uint64_t error)
|
1241 | 36707144 | Alon Levy | { |
1242 | 36707144 | Alon Levy | USBCCIDState *s = |
1243 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1244 | 36707144 | Alon Levy | |
1245 | 36707144 | Alon Levy | s->bmCommandStatus = COMMAND_STATUS_FAILED; |
1246 | 36707144 | Alon Levy | s->last_answer_error = error; |
1247 | c53c1258 | David Gibson | DPRINTF(s, 1, "VSC_Error: %" PRIX64 "\n", s->last_answer_error); |
1248 | 62a2ab6a | Brad Hards | /* TODO: these errors should be more verbose and propagated to the guest.*/
|
1249 | 36707144 | Alon Levy | /*
|
1250 | 36707144 | Alon Levy | * We flush all pending answers on CardRemove message in ccid-card-passthru,
|
1251 | 36707144 | Alon Levy | * so check that first to not trigger abort
|
1252 | 36707144 | Alon Levy | */
|
1253 | 36707144 | Alon Levy | if (ccid_has_pending_answers(s)) {
|
1254 | 36707144 | Alon Levy | ccid_write_data_block_answer(s, NULL, 0); |
1255 | 36707144 | Alon Levy | } |
1256 | 36707144 | Alon Levy | } |
1257 | 36707144 | Alon Levy | |
1258 | 36707144 | Alon Levy | void ccid_card_card_inserted(CCIDCardState *card)
|
1259 | 36707144 | Alon Levy | { |
1260 | 36707144 | Alon Levy | USBCCIDState *s = |
1261 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1262 | 36707144 | Alon Levy | |
1263 | 36707144 | Alon Levy | s->bmCommandStatus = COMMAND_STATUS_NO_ERROR; |
1264 | 36707144 | Alon Levy | ccid_flush_pending_answers(s); |
1265 | 36707144 | Alon Levy | ccid_on_slot_change(s, true);
|
1266 | 36707144 | Alon Levy | } |
1267 | 36707144 | Alon Levy | |
1268 | 36707144 | Alon Levy | static int ccid_card_exit(DeviceState *qdev) |
1269 | 36707144 | Alon Levy | { |
1270 | 36707144 | Alon Levy | int ret = 0; |
1271 | ba7c0520 | Anthony Liguori | CCIDCardState *card = CCID_CARD(qdev); |
1272 | 36707144 | Alon Levy | USBCCIDState *s = |
1273 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1274 | 36707144 | Alon Levy | |
1275 | 36707144 | Alon Levy | if (ccid_card_inserted(s)) {
|
1276 | 36707144 | Alon Levy | ccid_card_card_removed(card); |
1277 | 36707144 | Alon Levy | } |
1278 | ba7c0520 | Anthony Liguori | ret = ccid_card_exitfn(card); |
1279 | 36707144 | Alon Levy | s->card = NULL;
|
1280 | 36707144 | Alon Levy | return ret;
|
1281 | 36707144 | Alon Levy | } |
1282 | 36707144 | Alon Levy | |
1283 | d307af79 | Anthony Liguori | static int ccid_card_init(DeviceState *qdev) |
1284 | 36707144 | Alon Levy | { |
1285 | ba7c0520 | Anthony Liguori | CCIDCardState *card = CCID_CARD(qdev); |
1286 | 36707144 | Alon Levy | USBCCIDState *s = |
1287 | 36707144 | Alon Levy | DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent); |
1288 | 36707144 | Alon Levy | int ret = 0; |
1289 | 36707144 | Alon Levy | |
1290 | 36707144 | Alon Levy | if (card->slot != 0) { |
1291 | 36707144 | Alon Levy | error_report("Warning: usb-ccid supports one slot, can't add %d",
|
1292 | 36707144 | Alon Levy | card->slot); |
1293 | 36707144 | Alon Levy | return -1; |
1294 | 36707144 | Alon Levy | } |
1295 | 36707144 | Alon Levy | if (s->card != NULL) { |
1296 | 6daf194d | Markus Armbruster | error_report("Warning: usb-ccid card already full, not adding");
|
1297 | 36707144 | Alon Levy | return -1; |
1298 | 36707144 | Alon Levy | } |
1299 | ba7c0520 | Anthony Liguori | ret = ccid_card_initfn(card); |
1300 | 36707144 | Alon Levy | if (ret == 0) { |
1301 | 36707144 | Alon Levy | s->card = card; |
1302 | 36707144 | Alon Levy | } |
1303 | 36707144 | Alon Levy | return ret;
|
1304 | 36707144 | Alon Levy | } |
1305 | 36707144 | Alon Levy | |
1306 | 36707144 | Alon Levy | static int ccid_initfn(USBDevice *dev) |
1307 | 36707144 | Alon Levy | { |
1308 | 36707144 | Alon Levy | USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev); |
1309 | 36707144 | Alon Levy | |
1310 | 9d55d1ad | Gerd Hoffmann | usb_desc_create_serial(dev); |
1311 | 97237e0a | Gerd Hoffmann | usb_desc_init(dev); |
1312 | 0d936928 | Anthony Liguori | qbus_create_inplace(&s->bus.qbus, TYPE_CCID_BUS, &dev->qdev, NULL);
|
1313 | 7567b51f | Gerd Hoffmann | s->intr = usb_ep_get(dev, USB_TOKEN_IN, CCID_INT_IN_EP); |
1314 | 6df658f5 | Markus Armbruster | s->bus.qbus.allow_hotplug = 1;
|
1315 | 36707144 | Alon Levy | s->card = NULL;
|
1316 | 36707144 | Alon Levy | s->migration_state = MIGRATION_NONE; |
1317 | 36707144 | Alon Levy | s->migration_target_ip = 0;
|
1318 | 36707144 | Alon Levy | s->migration_target_port = 0;
|
1319 | 36707144 | Alon Levy | s->dev.speed = USB_SPEED_FULL; |
1320 | ba3f9bfb | Hans de Goede | s->dev.speedmask = USB_SPEED_MASK_FULL; |
1321 | 36707144 | Alon Levy | s->notify_slot_change = false;
|
1322 | 36707144 | Alon Levy | s->powered = true;
|
1323 | 36707144 | Alon Levy | s->pending_answers_num = 0;
|
1324 | 36707144 | Alon Levy | s->last_answer_error = 0;
|
1325 | 36707144 | Alon Levy | s->bulk_in_pending_start = 0;
|
1326 | 36707144 | Alon Levy | s->bulk_in_pending_end = 0;
|
1327 | 36707144 | Alon Levy | s->current_bulk_in = NULL;
|
1328 | 36707144 | Alon Levy | ccid_reset_error_status(s); |
1329 | 36707144 | Alon Levy | s->bulk_out_pos = 0;
|
1330 | 36707144 | Alon Levy | ccid_reset_parameters(s); |
1331 | 36707144 | Alon Levy | ccid_reset(s); |
1332 | b16352ac | Alon Levy | s->debug = parse_debug_env("QEMU_CCID_DEBUG", D_VERBOSE, s->debug);
|
1333 | 36707144 | Alon Levy | return 0; |
1334 | 36707144 | Alon Levy | } |
1335 | 36707144 | Alon Levy | |
1336 | 36707144 | Alon Levy | static int ccid_post_load(void *opaque, int version_id) |
1337 | 36707144 | Alon Levy | { |
1338 | 36707144 | Alon Levy | USBCCIDState *s = opaque; |
1339 | 36707144 | Alon Levy | |
1340 | 36707144 | Alon Levy | /*
|
1341 | 36707144 | Alon Levy | * This must be done after usb_device_attach, which sets state to ATTACHED,
|
1342 | 36707144 | Alon Levy | * while it must be DEFAULT in order to accept packets (like it is after
|
1343 | 36707144 | Alon Levy | * reset, but reset will reset our addr and call our reset handler which
|
1344 | 36707144 | Alon Levy | * may change state, and we don't want to do that when migrating).
|
1345 | 36707144 | Alon Levy | */
|
1346 | 36707144 | Alon Levy | s->dev.state = s->state_vmstate; |
1347 | 36707144 | Alon Levy | return 0; |
1348 | 36707144 | Alon Levy | } |
1349 | 36707144 | Alon Levy | |
1350 | 36707144 | Alon Levy | static void ccid_pre_save(void *opaque) |
1351 | 36707144 | Alon Levy | { |
1352 | 36707144 | Alon Levy | USBCCIDState *s = opaque; |
1353 | 36707144 | Alon Levy | |
1354 | 36707144 | Alon Levy | s->state_vmstate = s->dev.state; |
1355 | 36707144 | Alon Levy | if (s->dev.attached) {
|
1356 | 36707144 | Alon Levy | /*
|
1357 | 36707144 | Alon Levy | * Migrating an open device, ignore reconnection CHR_EVENT to avoid an
|
1358 | 62a2ab6a | Brad Hards | * erroneous detach.
|
1359 | 36707144 | Alon Levy | */
|
1360 | 36707144 | Alon Levy | s->migration_state = MIGRATION_MIGRATED; |
1361 | 36707144 | Alon Levy | } |
1362 | 36707144 | Alon Levy | } |
1363 | 36707144 | Alon Levy | |
1364 | 36707144 | Alon Levy | static VMStateDescription bulk_in_vmstate = {
|
1365 | 36707144 | Alon Levy | .name = "CCID BulkIn state",
|
1366 | 36707144 | Alon Levy | .version_id = 1,
|
1367 | 36707144 | Alon Levy | .minimum_version_id = 1,
|
1368 | 36707144 | Alon Levy | .fields = (VMStateField[]) { |
1369 | 36707144 | Alon Levy | VMSTATE_BUFFER(data, BulkIn), |
1370 | 36707144 | Alon Levy | VMSTATE_UINT32(len, BulkIn), |
1371 | 36707144 | Alon Levy | VMSTATE_UINT32(pos, BulkIn), |
1372 | 36707144 | Alon Levy | VMSTATE_END_OF_LIST() |
1373 | 36707144 | Alon Levy | } |
1374 | 36707144 | Alon Levy | }; |
1375 | 36707144 | Alon Levy | |
1376 | 36707144 | Alon Levy | static VMStateDescription answer_vmstate = {
|
1377 | 36707144 | Alon Levy | .name = "CCID Answer state",
|
1378 | 36707144 | Alon Levy | .version_id = 1,
|
1379 | 36707144 | Alon Levy | .minimum_version_id = 1,
|
1380 | 36707144 | Alon Levy | .fields = (VMStateField[]) { |
1381 | 36707144 | Alon Levy | VMSTATE_UINT8(slot, Answer), |
1382 | 36707144 | Alon Levy | VMSTATE_UINT8(seq, Answer), |
1383 | 36707144 | Alon Levy | VMSTATE_END_OF_LIST() |
1384 | 36707144 | Alon Levy | } |
1385 | 36707144 | Alon Levy | }; |
1386 | 36707144 | Alon Levy | |
1387 | 36707144 | Alon Levy | static VMStateDescription usb_device_vmstate = {
|
1388 | 36707144 | Alon Levy | .name = "usb_device",
|
1389 | 36707144 | Alon Levy | .version_id = 1,
|
1390 | 36707144 | Alon Levy | .minimum_version_id = 1,
|
1391 | 36707144 | Alon Levy | .fields = (VMStateField[]) { |
1392 | 36707144 | Alon Levy | VMSTATE_UINT8(addr, USBDevice), |
1393 | 36707144 | Alon Levy | VMSTATE_BUFFER(setup_buf, USBDevice), |
1394 | 36707144 | Alon Levy | VMSTATE_BUFFER(data_buf, USBDevice), |
1395 | 36707144 | Alon Levy | VMSTATE_END_OF_LIST() |
1396 | 36707144 | Alon Levy | } |
1397 | 36707144 | Alon Levy | }; |
1398 | 36707144 | Alon Levy | |
1399 | 36707144 | Alon Levy | static VMStateDescription ccid_vmstate = {
|
1400 | 36707144 | Alon Levy | .name = CCID_DEV_NAME, |
1401 | 36707144 | Alon Levy | .version_id = 1,
|
1402 | 36707144 | Alon Levy | .minimum_version_id = 1,
|
1403 | 36707144 | Alon Levy | .post_load = ccid_post_load, |
1404 | 36707144 | Alon Levy | .pre_save = ccid_pre_save, |
1405 | 36707144 | Alon Levy | .fields = (VMStateField[]) { |
1406 | 36707144 | Alon Levy | VMSTATE_STRUCT(dev, USBCCIDState, 1, usb_device_vmstate, USBDevice),
|
1407 | 36707144 | Alon Levy | VMSTATE_UINT8(debug, USBCCIDState), |
1408 | 36707144 | Alon Levy | VMSTATE_BUFFER(bulk_out_data, USBCCIDState), |
1409 | 36707144 | Alon Levy | VMSTATE_UINT32(bulk_out_pos, USBCCIDState), |
1410 | 36707144 | Alon Levy | VMSTATE_UINT8(bmSlotICCState, USBCCIDState), |
1411 | 36707144 | Alon Levy | VMSTATE_UINT8(powered, USBCCIDState), |
1412 | 36707144 | Alon Levy | VMSTATE_UINT8(notify_slot_change, USBCCIDState), |
1413 | 36707144 | Alon Levy | VMSTATE_UINT64(last_answer_error, USBCCIDState), |
1414 | 36707144 | Alon Levy | VMSTATE_UINT8(bError, USBCCIDState), |
1415 | 36707144 | Alon Levy | VMSTATE_UINT8(bmCommandStatus, USBCCIDState), |
1416 | 36707144 | Alon Levy | VMSTATE_UINT8(bProtocolNum, USBCCIDState), |
1417 | 4942d6c3 | Alon Levy | VMSTATE_BUFFER(abProtocolDataStructure.data, USBCCIDState), |
1418 | 36707144 | Alon Levy | VMSTATE_UINT32(ulProtocolDataStructureSize, USBCCIDState), |
1419 | 36707144 | Alon Levy | VMSTATE_STRUCT_ARRAY(bulk_in_pending, USBCCIDState, |
1420 | 36707144 | Alon Levy | BULK_IN_PENDING_NUM, 1, bulk_in_vmstate, BulkIn),
|
1421 | 36707144 | Alon Levy | VMSTATE_UINT32(bulk_in_pending_start, USBCCIDState), |
1422 | 36707144 | Alon Levy | VMSTATE_UINT32(bulk_in_pending_end, USBCCIDState), |
1423 | 36707144 | Alon Levy | VMSTATE_STRUCT_ARRAY(pending_answers, USBCCIDState, |
1424 | 36707144 | Alon Levy | PENDING_ANSWERS_NUM, 1, answer_vmstate, Answer),
|
1425 | 36707144 | Alon Levy | VMSTATE_UINT32(pending_answers_num, USBCCIDState), |
1426 | 36707144 | Alon Levy | VMSTATE_UINT8(migration_state, USBCCIDState), |
1427 | 36707144 | Alon Levy | VMSTATE_UINT32(state_vmstate, USBCCIDState), |
1428 | 36707144 | Alon Levy | VMSTATE_END_OF_LIST() |
1429 | 36707144 | Alon Levy | } |
1430 | 36707144 | Alon Levy | }; |
1431 | 36707144 | Alon Levy | |
1432 | 39bffca2 | Anthony Liguori | static Property ccid_properties[] = {
|
1433 | 39bffca2 | Anthony Liguori | DEFINE_PROP_UINT8("debug", USBCCIDState, debug, 0), |
1434 | 39bffca2 | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
1435 | 39bffca2 | Anthony Liguori | }; |
1436 | 39bffca2 | Anthony Liguori | |
1437 | 62aed765 | Anthony Liguori | static void ccid_class_initfn(ObjectClass *klass, void *data) |
1438 | 62aed765 | Anthony Liguori | { |
1439 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
1440 | 62aed765 | Anthony Liguori | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
1441 | 62aed765 | Anthony Liguori | |
1442 | 62aed765 | Anthony Liguori | uc->init = ccid_initfn; |
1443 | 62aed765 | Anthony Liguori | uc->product_desc = "QEMU USB CCID";
|
1444 | 62aed765 | Anthony Liguori | uc->usb_desc = &desc_ccid; |
1445 | 62aed765 | Anthony Liguori | uc->handle_reset = ccid_handle_reset; |
1446 | 62aed765 | Anthony Liguori | uc->handle_control = ccid_handle_control; |
1447 | 62aed765 | Anthony Liguori | uc->handle_data = ccid_handle_data; |
1448 | 62aed765 | Anthony Liguori | uc->handle_destroy = ccid_handle_destroy; |
1449 | 39bffca2 | Anthony Liguori | dc->desc = "CCID Rev 1.1 smartcard reader";
|
1450 | 39bffca2 | Anthony Liguori | dc->vmsd = &ccid_vmstate; |
1451 | 39bffca2 | Anthony Liguori | dc->props = ccid_properties; |
1452 | 62aed765 | Anthony Liguori | } |
1453 | 62aed765 | Anthony Liguori | |
1454 | 8c43a6f0 | Andreas Färber | static const TypeInfo ccid_info = { |
1455 | 39bffca2 | Anthony Liguori | .name = CCID_DEV_NAME, |
1456 | 39bffca2 | Anthony Liguori | .parent = TYPE_USB_DEVICE, |
1457 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(USBCCIDState),
|
1458 | 39bffca2 | Anthony Liguori | .class_init = ccid_class_initfn, |
1459 | 36707144 | Alon Levy | }; |
1460 | 36707144 | Alon Levy | |
1461 | 39bffca2 | Anthony Liguori | static void ccid_card_class_init(ObjectClass *klass, void *data) |
1462 | 39bffca2 | Anthony Liguori | { |
1463 | 39bffca2 | Anthony Liguori | DeviceClass *k = DEVICE_CLASS(klass); |
1464 | 0d936928 | Anthony Liguori | k->bus_type = TYPE_CCID_BUS; |
1465 | 39bffca2 | Anthony Liguori | k->init = ccid_card_init; |
1466 | 39bffca2 | Anthony Liguori | k->exit = ccid_card_exit; |
1467 | bce54474 | Paolo Bonzini | k->props = ccid_props; |
1468 | 39bffca2 | Anthony Liguori | } |
1469 | 39bffca2 | Anthony Liguori | |
1470 | 8c43a6f0 | Andreas Färber | static const TypeInfo ccid_card_type_info = { |
1471 | ba7c0520 | Anthony Liguori | .name = TYPE_CCID_CARD, |
1472 | ba7c0520 | Anthony Liguori | .parent = TYPE_DEVICE, |
1473 | ba7c0520 | Anthony Liguori | .instance_size = sizeof(CCIDCardState),
|
1474 | ba7c0520 | Anthony Liguori | .abstract = true,
|
1475 | ba7c0520 | Anthony Liguori | .class_size = sizeof(CCIDCardClass),
|
1476 | 39bffca2 | Anthony Liguori | .class_init = ccid_card_class_init, |
1477 | ba7c0520 | Anthony Liguori | }; |
1478 | ba7c0520 | Anthony Liguori | |
1479 | 83f7d43a | Andreas Färber | static void ccid_register_types(void) |
1480 | 36707144 | Alon Levy | { |
1481 | 0d936928 | Anthony Liguori | type_register_static(&ccid_bus_info); |
1482 | ba7c0520 | Anthony Liguori | type_register_static(&ccid_card_type_info); |
1483 | 39bffca2 | Anthony Liguori | type_register_static(&ccid_info); |
1484 | ba02430f | Anthony Liguori | usb_legacy_register(CCID_DEV_NAME, "ccid", NULL); |
1485 | 36707144 | Alon Levy | } |
1486 | 83f7d43a | Andreas Färber | |
1487 | 83f7d43a | Andreas Färber | type_init(ccid_register_types) |