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