s390x: free tmp explicitly in every opcode for disas_a5()
[qemu.git] / hw / usb-ccid.c
blob5b6878bea4faf61adc91ce8d1581d3e6091f4eb1
1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
4 * CCID Device emulation
6 * Written by Alon Levy, with contributions from Robert Relyea.
8 * Based on usb-serial.c, see it's copyright and attributions below.
10 * This work is licensed under the terms of the GNU GPL, version 2.1 or later.
11 * See the COPYING file in the top-level directory.
12 * ------- (original copyright & attribution for usb-serial.c below) --------
13 * Copyright (c) 2006 CodeSourcery.
14 * Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org>
15 * Written by Paul Brook, reused for FTDI by Samuel Thibault,
19 * References:
21 * CCID Specification Revision 1.1 April 22nd 2005
22 * "Universal Serial Bus, Device Class: Smart Card"
23 * Specification for Integrated Circuit(s) Cards Interface Devices
25 * Endianness note: from the spec (1.3)
26 * "Fields that are larger than a byte are stored in little endian"
28 * KNOWN BUGS
29 * 1. remove/insert can sometimes result in removed state instead of inserted.
30 * This is a result of the following:
31 * symptom: dmesg shows ERMOTEIO (-121), pcscd shows -99. This can happen
32 * when a short packet is sent, as seen in uhci-usb.c, resulting from a urb
33 * from the guest requesting SPD and us returning a smaller packet.
34 * Not sure which messages trigger this.
37 #include "qemu-common.h"
38 #include "qemu-error.h"
39 #include "usb.h"
40 #include "monitor.h"
42 #include "hw/ccid.h"
44 #define DPRINTF(s, lvl, fmt, ...) \
45 do { \
46 if (lvl <= s->debug) { \
47 printf("usb-ccid: " fmt , ## __VA_ARGS__); \
48 } \
49 } while (0)
51 #define D_WARN 1
52 #define D_INFO 2
53 #define D_MORE_INFO 3
54 #define D_VERBOSE 4
56 #define CCID_DEV_NAME "usb-ccid"
59 * The two options for variable sized buffers:
60 * make them constant size, for large enough constant,
61 * or handle the migration complexity - VMState doesn't handle this case.
62 * sizes are expected never to be exceeded, unless guest misbehaves.
64 #define BULK_OUT_DATA_SIZE 65536
65 #define PENDING_ANSWERS_NUM 128
67 #define BULK_IN_BUF_SIZE 384
68 #define BULK_IN_PENDING_NUM 8
70 #define InterfaceOutClass \
71 ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE)<<8)
73 #define InterfaceInClass \
74 ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE)<<8)
76 #define CCID_MAX_PACKET_SIZE 64
78 #define CCID_CONTROL_ABORT 0x1
79 #define CCID_CONTROL_GET_CLOCK_FREQUENCIES 0x2
80 #define CCID_CONTROL_GET_DATA_RATES 0x3
82 #define CCID_PRODUCT_DESCRIPTION "QEMU USB CCID"
83 #define CCID_VENDOR_DESCRIPTION "QEMU " QEMU_VERSION
84 #define CCID_INTERFACE_NAME "CCID Interface"
85 #define CCID_SERIAL_NUMBER_STRING "1"
87 * Using Gemplus Vendor and Product id
88 * Effect on various drivers:
89 * usbccid.sys (winxp, others untested) is a class driver so it doesn't care.
90 * linux has a number of class drivers, but openct filters based on
91 * vendor/product (/etc/openct.conf under fedora), hence Gemplus.
93 #define CCID_VENDOR_ID 0x08e6
94 #define CCID_PRODUCT_ID 0x4433
95 #define CCID_DEVICE_VERSION 0x0000
98 * BULK_OUT messages from PC to Reader
99 * Defined in CCID Rev 1.1 6.1 (page 26)
101 #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn 0x62
102 #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff 0x63
103 #define CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus 0x65
104 #define CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock 0x6f
105 #define CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters 0x6c
106 #define CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters 0x6d
107 #define CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters 0x61
108 #define CCID_MESSAGE_TYPE_PC_to_RDR_Escape 0x6b
109 #define CCID_MESSAGE_TYPE_PC_to_RDR_IccClock 0x6e
110 #define CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU 0x6a
111 #define CCID_MESSAGE_TYPE_PC_to_RDR_Secure 0x69
112 #define CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical 0x71
113 #define CCID_MESSAGE_TYPE_PC_to_RDR_Abort 0x72
114 #define CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency 0x73
117 * BULK_IN messages from Reader to PC
118 * Defined in CCID Rev 1.1 6.2 (page 48)
120 #define CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock 0x80
121 #define CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus 0x81
122 #define CCID_MESSAGE_TYPE_RDR_to_PC_Parameters 0x82
123 #define CCID_MESSAGE_TYPE_RDR_to_PC_Escape 0x83
124 #define CCID_MESSAGE_TYPE_RDR_to_PC_DataRateAndClockFrequency 0x84
127 * INTERRUPT_IN messages from Reader to PC
128 * Defined in CCID Rev 1.1 6.3 (page 56)
130 #define CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange 0x50
131 #define CCID_MESSAGE_TYPE_RDR_to_PC_HardwareError 0x51
134 * Endpoints for CCID - addresses are up to us to decide.
135 * To support slot insertion and removal we must have an interrupt in ep
136 * in addition we need a bulk in and bulk out ep
137 * 5.2, page 20
139 #define CCID_INT_IN_EP 1
140 #define CCID_BULK_IN_EP 2
141 #define CCID_BULK_OUT_EP 3
143 /* bmSlotICCState masks */
144 #define SLOT_0_STATE_MASK 1
145 #define SLOT_0_CHANGED_MASK 2
147 /* Status codes that go in bStatus (see 6.2.6) */
148 enum {
149 ICC_STATUS_PRESENT_ACTIVE = 0,
150 ICC_STATUS_PRESENT_INACTIVE,
151 ICC_STATUS_NOT_PRESENT
154 enum {
155 COMMAND_STATUS_NO_ERROR = 0,
156 COMMAND_STATUS_FAILED,
157 COMMAND_STATUS_TIME_EXTENSION_REQUIRED
160 /* Error codes that go in bError (see 6.2.6) */
161 enum {
162 ERROR_CMD_NOT_SUPPORTED = 0,
163 ERROR_CMD_ABORTED = -1,
164 ERROR_ICC_MUTE = -2,
165 ERROR_XFR_PARITY_ERROR = -3,
166 ERROR_XFR_OVERRUN = -4,
167 ERROR_HW_ERROR = -5,
170 /* 6.2.6 RDR_to_PC_SlotStatus definitions */
171 enum {
172 CLOCK_STATUS_RUNNING = 0,
174 * 0 - Clock Running, 1 - Clock stopped in State L, 2 - H,
175 * 3 - unknown state. rest are RFU
179 typedef struct __attribute__ ((__packed__)) CCID_Header {
180 uint8_t bMessageType;
181 uint32_t dwLength;
182 uint8_t bSlot;
183 uint8_t bSeq;
184 } CCID_Header;
186 typedef struct __attribute__ ((__packed__)) CCID_BULK_IN {
187 CCID_Header hdr;
188 uint8_t bStatus; /* Only used in BULK_IN */
189 uint8_t bError; /* Only used in BULK_IN */
190 } CCID_BULK_IN;
192 typedef struct __attribute__ ((__packed__)) CCID_SlotStatus {
193 CCID_BULK_IN b;
194 uint8_t bClockStatus;
195 } CCID_SlotStatus;
197 typedef struct __attribute__ ((__packed__)) CCID_Parameter {
198 CCID_BULK_IN b;
199 uint8_t bProtocolNum;
200 uint8_t abProtocolDataStructure[0];
201 } CCID_Parameter;
203 typedef struct __attribute__ ((__packed__)) CCID_DataBlock {
204 CCID_BULK_IN b;
205 uint8_t bChainParameter;
206 uint8_t abData[0];
207 } CCID_DataBlock;
209 /* 6.1.4 PC_to_RDR_XfrBlock */
210 typedef struct __attribute__ ((__packed__)) CCID_XferBlock {
211 CCID_Header hdr;
212 uint8_t bBWI; /* Block Waiting Timeout */
213 uint16_t wLevelParameter; /* XXX currently unused */
214 uint8_t abData[0];
215 } CCID_XferBlock;
217 typedef struct __attribute__ ((__packed__)) CCID_IccPowerOn {
218 CCID_Header hdr;
219 uint8_t bPowerSelect;
220 uint16_t abRFU;
221 } CCID_IccPowerOn;
223 typedef struct __attribute__ ((__packed__)) CCID_IccPowerOff {
224 CCID_Header hdr;
225 uint16_t abRFU;
226 } CCID_IccPowerOff;
228 typedef struct __attribute__ ((__packed__)) CCID_SetParameters {
229 CCID_Header hdr;
230 uint8_t bProtocolNum;
231 uint16_t abRFU;
232 uint8_t abProtocolDataStructure[0];
233 } CCID_SetParameters;
235 typedef struct CCID_Notify_Slot_Change {
236 uint8_t bMessageType; /* CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange */
237 uint8_t bmSlotICCState;
238 } CCID_Notify_Slot_Change;
240 /* used for DataBlock response to XferBlock */
241 typedef struct Answer {
242 uint8_t slot;
243 uint8_t seq;
244 } Answer;
246 /* pending BULK_IN messages */
247 typedef struct BulkIn {
248 uint8_t data[BULK_IN_BUF_SIZE];
249 uint32_t len;
250 uint32_t pos;
251 } BulkIn;
253 enum {
254 MIGRATION_NONE,
255 MIGRATION_MIGRATED,
258 typedef struct CCIDBus CCIDBus;
259 typedef struct USBCCIDState USBCCIDState;
261 #define MAX_PROTOCOL_SIZE 7
264 * powered - defaults to true, changed by PowerOn/PowerOff messages
266 struct USBCCIDState {
267 USBDevice dev;
268 CCIDBus *bus;
269 CCIDCardState *card;
270 CCIDCardInfo *cardinfo; /* caching the info pointer */
271 BulkIn bulk_in_pending[BULK_IN_PENDING_NUM]; /* circular */
272 uint32_t bulk_in_pending_start;
273 uint32_t bulk_in_pending_end; /* first free */
274 uint32_t bulk_in_pending_num;
275 BulkIn *current_bulk_in;
276 uint8_t bulk_out_data[BULK_OUT_DATA_SIZE];
277 uint32_t bulk_out_pos;
278 uint64_t last_answer_error;
279 Answer pending_answers[PENDING_ANSWERS_NUM];
280 uint32_t pending_answers_start;
281 uint32_t pending_answers_end;
282 uint32_t pending_answers_num;
283 uint8_t bError;
284 uint8_t bmCommandStatus;
285 uint8_t bProtocolNum;
286 uint8_t abProtocolDataStructure[MAX_PROTOCOL_SIZE];
287 uint32_t ulProtocolDataStructureSize;
288 uint32_t state_vmstate;
289 uint32_t migration_target_ip;
290 uint16_t migration_target_port;
291 uint8_t migration_state;
292 uint8_t bmSlotICCState;
293 uint8_t powered;
294 uint8_t notify_slot_change;
295 uint8_t debug;
299 * CCID Spec chapter 4: CCID uses a standard device descriptor per Chapter 9,
300 * "USB Device Framework", section 9.6.1, in the Universal Serial Bus
301 * Specification.
303 * This device implemented based on the spec and with an Athena Smart Card
304 * Reader as reference:
305 * 0dc3:1004 Athena Smartcard Solutions, Inc.
308 static const uint8_t qemu_ccid_dev_descriptor[] = {
309 0x12, /* u8 bLength; */
310 USB_DT_DEVICE, /* u8 bDescriptorType; Device */
311 0x10, 0x01, /* u16 bcdUSB; v1.1 */
313 0x00, /* u8 bDeviceClass; */
314 0x00, /* u8 bDeviceSubClass; */
315 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
316 0x40, /* u8 bMaxPacketSize0; 8 Bytes (valid: 8,16,32,64) */
318 /* Vendor and product id are arbitrary. */
319 /* u16 idVendor */
320 CCID_VENDOR_ID & 0xff, CCID_VENDOR_ID >> 8,
321 /* u16 idProduct */
322 CCID_PRODUCT_ID & 0xff, CCID_PRODUCT_ID >> 8,
323 /* u16 bcdDevice */
324 CCID_DEVICE_VERSION & 0xff, CCID_DEVICE_VERSION >> 8,
325 0x01, /* u8 iManufacturer; */
326 0x02, /* u8 iProduct; */
327 0x03, /* u8 iSerialNumber; */
328 0x01, /* u8 bNumConfigurations; */
331 static const uint8_t qemu_ccid_config_descriptor[] = {
333 /* one configuration */
334 0x09, /* u8 bLength; */
335 USB_DT_CONFIG, /* u8 bDescriptorType; Configuration */
336 0x5d, 0x00, /* u16 wTotalLength; 9+9+54+7+7+7 */
337 0x01, /* u8 bNumInterfaces; (1) */
338 0x01, /* u8 bConfigurationValue; */
339 0x00, /* u8 iConfiguration; */
340 0xe0, /* u8 bmAttributes;
341 Bit 7: must be set,
342 6: Self-powered,
343 5: Remote wakeup,
344 4..0: resvd */
345 100/2, /* u8 MaxPower; 50 == 100mA */
347 /* one interface */
348 0x09, /* u8 if_bLength; */
349 USB_DT_INTERFACE, /* u8 if_bDescriptorType; Interface */
350 0x00, /* u8 if_bInterfaceNumber; */
351 0x00, /* u8 if_bAlternateSetting; */
352 0x03, /* u8 if_bNumEndpoints; */
353 0x0b, /* u8 if_bInterfaceClass; Smart Card Device Class */
354 0x00, /* u8 if_bInterfaceSubClass; Subclass code */
355 0x00, /* u8 if_bInterfaceProtocol; Protocol code */
356 0x04, /* u8 if_iInterface; Index of string descriptor */
358 /* Smart Card Device Class Descriptor */
359 0x36, /* u8 bLength; */
360 0x21, /* u8 bDescriptorType; Functional */
361 0x10, 0x01, /* u16 bcdCCID; CCID Specification Release Number. */
362 0x00, /*
363 * u8 bMaxSlotIndex; The index of the highest available
364 * slot on this device. All slots are consecutive starting
365 * at 00h.
367 0x07, /* u8 bVoltageSupport; 01h - 5.0v, 02h - 3.0, 03 - 1.8 */
369 0x03, 0x00, /* u32 dwProtocols; RRRR PPPP. RRRR = 0000h.*/
370 0x00, 0x00, /* PPPP: 0001h = Protocol T=0, 0002h = Protocol T=1 */
371 /* u32 dwDefaultClock; in kHZ (0x0fa0 is 4 MHz) */
372 0xa0, 0x0f, 0x00, 0x00,
373 /* u32 dwMaximumClock; */
374 0x00, 0x00, 0x01, 0x00,
375 0x00, /* u8 bNumClockSupported; *
376 * 0 means just the default and max. */
377 /* u32 dwDataRate ;bps. 9600 == 00002580h */
378 0x80, 0x25, 0x00, 0x00,
379 /* u32 dwMaxDataRate ; 11520 bps == 0001C200h */
380 0x00, 0xC2, 0x01, 0x00,
381 0x00, /* u8 bNumDataRatesSupported; 00 means all rates between
382 * default and max */
383 /* u32 dwMaxIFSD; *
384 * maximum IFSD supported by CCID for protocol *
385 * T=1 (Maximum seen from various cards) */
386 0xfe, 0x00, 0x00, 0x00,
387 /* u32 dwSyncProtocols; 1 - 2-wire, 2 - 3-wire, 4 - I2C */
388 0x00, 0x00, 0x00, 0x00,
389 /* u32 dwMechanical; 0 - no special characteristics. */
390 0x00, 0x00, 0x00, 0x00,
392 * u32 dwFeatures;
393 * 0 - No special characteristics
394 * + 2 Automatic parameter configuration based on ATR data
395 * + 4 Automatic activation of ICC on inserting
396 * + 8 Automatic ICC voltage selection
397 * + 10 Automatic ICC clock frequency change
398 * + 20 Automatic baud rate change
399 * + 40 Automatic parameters negotiation made by the CCID
400 * + 80 automatic PPS made by the CCID
401 * 100 CCID can set ICC in clock stop mode
402 * 200 NAD value other then 00 accepted (T=1 protocol)
403 * + 400 Automatic IFSD exchange as first exchange (T=1)
404 * One of the following only:
405 * + 10000 TPDU level exchanges with CCID
406 * 20000 Short APDU level exchange with CCID
407 * 40000 Short and Extended APDU level exchange with CCID
409 * + 100000 USB Wake up signaling supported on card
410 * insertion and removal. Must set bit 5 in bmAttributes
411 * in Configuration descriptor if 100000 is set.
413 0xfe, 0x04, 0x11, 0x00,
415 * u32 dwMaxCCIDMessageLength; For extended APDU in
416 * [261 + 10 , 65544 + 10]. Otherwise the minimum is
417 * wMaxPacketSize of the Bulk-OUT endpoint
419 0x12, 0x00, 0x01, 0x00,
420 0xFF, /*
421 * u8 bClassGetResponse; Significant only for CCID that
422 * offers an APDU level for exchanges. Indicates the
423 * default class value used by the CCID when it sends a
424 * Get Response command to perform the transportation of
425 * an APDU by T=0 protocol
426 * FFh indicates that the CCID echos the class of the APDU.
428 0xFF, /*
429 * u8 bClassEnvelope; EAPDU only. Envelope command for
430 * T=0
432 0x00, 0x00, /*
433 * u16 wLcdLayout; XXYY Number of lines (XX) and chars per
434 * line for LCD display used for PIN entry. 0000 - no LCD
436 0x01, /*
437 * u8 bPINSupport; 01h PIN Verification,
438 * 02h PIN Modification
440 0x01, /* u8 bMaxCCIDBusySlots; */
442 /* Interrupt-IN endpoint */
443 0x07, /* u8 ep_bLength; */
444 /* u8 ep_bDescriptorType; Endpoint */
445 USB_DT_ENDPOINT,
446 /* u8 ep_bEndpointAddress; IN Endpoint 1 */
447 0x80 | CCID_INT_IN_EP,
448 0x03, /* u8 ep_bmAttributes; Interrupt */
449 /* u16 ep_wMaxPacketSize; */
450 CCID_MAX_PACKET_SIZE & 0xff, (CCID_MAX_PACKET_SIZE >> 8),
451 0xff, /* u8 ep_bInterval; */
453 /* Bulk-In endpoint */
454 0x07, /* u8 ep_bLength; */
455 /* u8 ep_bDescriptorType; Endpoint */
456 USB_DT_ENDPOINT,
457 /* u8 ep_bEndpointAddress; IN Endpoint 2 */
458 0x80 | CCID_BULK_IN_EP,
459 0x02, /* u8 ep_bmAttributes; Bulk */
460 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
461 0x00, /* u8 ep_bInterval; */
463 /* Bulk-Out endpoint */
464 0x07, /* u8 ep_bLength; */
465 /* u8 ep_bDescriptorType; Endpoint */
466 USB_DT_ENDPOINT,
467 /* u8 ep_bEndpointAddress; OUT Endpoint 3 */
468 CCID_BULK_OUT_EP,
469 0x02, /* u8 ep_bmAttributes; Bulk */
470 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
471 0x00, /* u8 ep_bInterval; */
475 static bool ccid_has_pending_answers(USBCCIDState *s)
477 return s->pending_answers_num > 0;
480 static void ccid_clear_pending_answers(USBCCIDState *s)
482 s->pending_answers_num = 0;
483 s->pending_answers_start = 0;
484 s->pending_answers_end = 0;
487 static void ccid_print_pending_answers(USBCCIDState *s)
489 Answer *answer;
490 int i, count;
492 DPRINTF(s, D_VERBOSE, "usb-ccid: pending answers:");
493 if (!ccid_has_pending_answers(s)) {
494 DPRINTF(s, D_VERBOSE, " empty\n");
495 return;
497 for (i = s->pending_answers_start, count = s->pending_answers_num ;
498 count > 0; count--, i++) {
499 answer = &s->pending_answers[i % PENDING_ANSWERS_NUM];
500 if (count == 1) {
501 DPRINTF(s, D_VERBOSE, "%d:%d\n", answer->slot, answer->seq);
502 } else {
503 DPRINTF(s, D_VERBOSE, "%d:%d,", answer->slot, answer->seq);
508 static void ccid_add_pending_answer(USBCCIDState *s, CCID_Header *hdr)
510 Answer *answer;
512 assert(s->pending_answers_num < PENDING_ANSWERS_NUM);
513 s->pending_answers_num++;
514 answer =
515 &s->pending_answers[(s->pending_answers_end++) % PENDING_ANSWERS_NUM];
516 answer->slot = hdr->bSlot;
517 answer->seq = hdr->bSeq;
518 ccid_print_pending_answers(s);
521 static void ccid_remove_pending_answer(USBCCIDState *s,
522 uint8_t *slot, uint8_t *seq)
524 Answer *answer;
526 assert(s->pending_answers_num > 0);
527 s->pending_answers_num--;
528 answer =
529 &s->pending_answers[(s->pending_answers_start++) % PENDING_ANSWERS_NUM];
530 *slot = answer->slot;
531 *seq = answer->seq;
532 ccid_print_pending_answers(s);
535 static void ccid_bulk_in_clear(USBCCIDState *s)
537 s->bulk_in_pending_start = 0;
538 s->bulk_in_pending_end = 0;
539 s->bulk_in_pending_num = 0;
542 static void ccid_bulk_in_release(USBCCIDState *s)
544 assert(s->current_bulk_in != NULL);
545 s->current_bulk_in->pos = 0;
546 s->current_bulk_in = NULL;
549 static void ccid_bulk_in_get(USBCCIDState *s)
551 if (s->current_bulk_in != NULL || s->bulk_in_pending_num == 0) {
552 return;
554 assert(s->bulk_in_pending_num > 0);
555 s->bulk_in_pending_num--;
556 s->current_bulk_in =
557 &s->bulk_in_pending[(s->bulk_in_pending_start++) % BULK_IN_PENDING_NUM];
560 static void *ccid_reserve_recv_buf(USBCCIDState *s, uint16_t len)
562 BulkIn *bulk_in;
564 DPRINTF(s, D_VERBOSE, "%s: QUEUE: reserve %d bytes\n", __func__, len);
566 /* look for an existing element */
567 if (len > BULK_IN_BUF_SIZE) {
568 DPRINTF(s, D_WARN, "usb-ccid.c: %s: len larger then max (%d>%d). "
569 "discarding message.\n",
570 __func__, len, BULK_IN_BUF_SIZE);
571 return NULL;
573 if (s->bulk_in_pending_num >= BULK_IN_PENDING_NUM) {
574 DPRINTF(s, D_WARN, "usb-ccid.c: %s: No free bulk_in buffers. "
575 "discarding message.\n", __func__);
576 return NULL;
578 bulk_in =
579 &s->bulk_in_pending[(s->bulk_in_pending_end++) % BULK_IN_PENDING_NUM];
580 s->bulk_in_pending_num++;
581 bulk_in->len = len;
582 return bulk_in->data;
585 static void ccid_reset(USBCCIDState *s)
587 ccid_bulk_in_clear(s);
588 ccid_clear_pending_answers(s);
591 static void ccid_detach(USBCCIDState *s)
593 ccid_reset(s);
596 static void ccid_handle_reset(USBDevice *dev)
598 USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
600 DPRINTF(s, 1, "Reset\n");
602 ccid_reset(s);
605 static int ccid_handle_control(USBDevice *dev, USBPacket *p, int request,
606 int value, int index, int length, uint8_t *data)
608 USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
609 int ret = 0;
611 DPRINTF(s, 1, "got control %x, value %x\n", request, value);
612 switch (request) {
613 case DeviceRequest | USB_REQ_GET_STATUS:
614 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
615 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
616 data[1] = 0x00;
617 ret = 2;
618 break;
619 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
620 if (value == USB_DEVICE_REMOTE_WAKEUP) {
621 dev->remote_wakeup = 0;
622 } else {
623 goto fail;
625 ret = 0;
626 break;
627 case DeviceOutRequest | USB_REQ_SET_FEATURE:
628 if (value == USB_DEVICE_REMOTE_WAKEUP) {
629 dev->remote_wakeup = 1;
630 } else {
631 goto fail;
633 ret = 0;
634 break;
635 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
636 dev->addr = value;
637 ret = 0;
638 break;
639 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
640 switch (value >> 8) {
641 case USB_DT_DEVICE:
642 memcpy(data, qemu_ccid_dev_descriptor,
643 sizeof(qemu_ccid_dev_descriptor));
644 ret = sizeof(qemu_ccid_dev_descriptor);
645 break;
646 case USB_DT_CONFIG:
647 memcpy(data, qemu_ccid_config_descriptor,
648 sizeof(qemu_ccid_config_descriptor));
649 ret = sizeof(qemu_ccid_config_descriptor);
650 break;
651 case USB_DT_STRING:
652 switch (value & 0xff) {
653 case 0:
654 /* language ids */
655 data[0] = 4;
656 data[1] = 3;
657 data[2] = 0x09;
658 data[3] = 0x04;
659 ret = 4;
660 break;
661 case 1:
662 /* vendor description */
663 ret = set_usb_string(data, CCID_VENDOR_DESCRIPTION);
664 break;
665 case 2:
666 /* product description */
667 ret = set_usb_string(data, CCID_PRODUCT_DESCRIPTION);
668 break;
669 case 3:
670 /* serial number */
671 ret = set_usb_string(data, CCID_SERIAL_NUMBER_STRING);
672 break;
673 case 4:
674 /* interface name */
675 ret = set_usb_string(data, CCID_INTERFACE_NAME);
676 break;
677 default:
678 goto fail;
680 break;
681 default:
682 goto fail;
684 break;
685 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
686 data[0] = 1;
687 ret = 1;
688 break;
689 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
690 /* Only one configuration - we just ignore the request */
691 ret = 0;
692 break;
693 case DeviceRequest | USB_REQ_GET_INTERFACE:
694 data[0] = 0;
695 ret = 1;
696 break;
697 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
698 ret = 0;
699 break;
700 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
701 ret = 0;
702 break;
704 /* Class specific requests. */
705 case InterfaceOutClass | CCID_CONTROL_ABORT:
706 DPRINTF(s, 1, "ccid_control abort UNIMPLEMENTED\n");
707 ret = USB_RET_STALL;
708 break;
709 case InterfaceInClass | CCID_CONTROL_GET_CLOCK_FREQUENCIES:
710 DPRINTF(s, 1, "ccid_control get clock frequencies UNIMPLEMENTED\n");
711 ret = USB_RET_STALL;
712 break;
713 case InterfaceInClass | CCID_CONTROL_GET_DATA_RATES:
714 DPRINTF(s, 1, "ccid_control get data rates UNIMPLEMENTED\n");
715 ret = USB_RET_STALL;
716 break;
717 default:
718 fail:
719 DPRINTF(s, 1, "got unsupported/bogus control %x, value %x\n",
720 request, value);
721 ret = USB_RET_STALL;
722 break;
724 return ret;
727 static bool ccid_card_inserted(USBCCIDState *s)
729 return s->bmSlotICCState & SLOT_0_STATE_MASK;
732 static uint8_t ccid_card_status(USBCCIDState *s)
734 return ccid_card_inserted(s)
735 ? (s->powered ?
736 ICC_STATUS_PRESENT_ACTIVE
737 : ICC_STATUS_PRESENT_INACTIVE
739 : ICC_STATUS_NOT_PRESENT;
742 static uint8_t ccid_calc_status(USBCCIDState *s)
745 * page 55, 6.2.6, calculation of bStatus from bmICCStatus and
746 * bmCommandStatus
748 uint8_t ret = ccid_card_status(s) | (s->bmCommandStatus << 6);
749 DPRINTF(s, D_VERBOSE, "status = %d\n", ret);
750 return ret;
753 static void ccid_reset_error_status(USBCCIDState *s)
755 s->bError = ERROR_CMD_NOT_SUPPORTED;
756 s->bmCommandStatus = COMMAND_STATUS_NO_ERROR;
759 static void ccid_write_slot_status(USBCCIDState *s, CCID_Header *recv)
761 CCID_SlotStatus *h = ccid_reserve_recv_buf(s, sizeof(CCID_SlotStatus));
762 if (h == NULL) {
763 return;
765 h->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus;
766 h->b.hdr.dwLength = 0;
767 h->b.hdr.bSlot = recv->bSlot;
768 h->b.hdr.bSeq = recv->bSeq;
769 h->b.bStatus = ccid_calc_status(s);
770 h->b.bError = s->bError;
771 h->bClockStatus = CLOCK_STATUS_RUNNING;
772 ccid_reset_error_status(s);
775 static void ccid_write_parameters(USBCCIDState *s, CCID_Header *recv)
777 CCID_Parameter *h;
778 uint32_t len = s->ulProtocolDataStructureSize;
780 h = ccid_reserve_recv_buf(s, sizeof(CCID_Parameter) + len);
781 if (h == NULL) {
782 return;
784 h->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_Parameters;
785 h->b.hdr.dwLength = 0;
786 h->b.hdr.bSlot = recv->bSlot;
787 h->b.hdr.bSeq = recv->bSeq;
788 h->b.bStatus = ccid_calc_status(s);
789 h->b.bError = s->bError;
790 h->bProtocolNum = s->bProtocolNum;
791 memcpy(h->abProtocolDataStructure, s->abProtocolDataStructure, len);
792 ccid_reset_error_status(s);
795 static void ccid_write_data_block(USBCCIDState *s, uint8_t slot, uint8_t seq,
796 const uint8_t *data, uint32_t len)
798 CCID_DataBlock *p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
800 if (p == NULL) {
801 return;
803 p->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock;
804 p->b.hdr.dwLength = cpu_to_le32(len);
805 p->b.hdr.bSlot = slot;
806 p->b.hdr.bSeq = seq;
807 p->b.bStatus = ccid_calc_status(s);
808 p->b.bError = s->bError;
809 if (p->b.bError) {
810 DPRINTF(s, D_VERBOSE, "error %d", p->b.bError);
812 memcpy(p->abData, data, len);
813 ccid_reset_error_status(s);
816 static void ccid_write_data_block_answer(USBCCIDState *s,
817 const uint8_t *data, uint32_t len)
819 uint8_t seq;
820 uint8_t slot;
822 if (!ccid_has_pending_answers(s)) {
823 abort();
825 ccid_remove_pending_answer(s, &slot, &seq);
826 ccid_write_data_block(s, slot, seq, data, len);
829 static void ccid_write_data_block_atr(USBCCIDState *s, CCID_Header *recv)
831 const uint8_t *atr = NULL;
832 uint32_t len = 0;
834 if (s->card) {
835 atr = s->cardinfo->get_atr(s->card, &len);
837 ccid_write_data_block(s, recv->bSlot, recv->bSeq, atr, len);
840 static void ccid_set_parameters(USBCCIDState *s, CCID_Header *recv)
842 CCID_SetParameters *ph = (CCID_SetParameters *) recv;
843 uint32_t len = 0;
844 if ((ph->bProtocolNum & 3) == 0) {
845 len = 5;
847 if ((ph->bProtocolNum & 3) == 1) {
848 len = 7;
850 if (len == 0) {
851 s->bmCommandStatus = COMMAND_STATUS_FAILED;
852 s->bError = 7; /* Protocol invalid or not supported */
853 return;
855 s->bProtocolNum = ph->bProtocolNum;
856 memcpy(s->abProtocolDataStructure, ph->abProtocolDataStructure, len);
857 s->ulProtocolDataStructureSize = len;
858 DPRINTF(s, 1, "%s: using len %d\n", __func__, len);
862 * must be 5 bytes for T=0, 7 bytes for T=1
863 * See page 52
865 static const uint8_t abDefaultProtocolDataStructure[7] = {
866 0x77, 0x00, 0x00, 0x00, 0x00, 0xfe /*IFSC*/, 0x00 /*NAD*/ };
868 static void ccid_reset_parameters(USBCCIDState *s)
870 uint32_t len = sizeof(abDefaultProtocolDataStructure);
872 s->bProtocolNum = 1; /* T=1 */
873 s->ulProtocolDataStructureSize = len;
874 memcpy(s->abProtocolDataStructure, abDefaultProtocolDataStructure, len);
877 static void ccid_report_error_failed(USBCCIDState *s, uint8_t error)
879 s->bmCommandStatus = COMMAND_STATUS_FAILED;
880 s->bError = error;
883 /* NOTE: only a single slot is supported (SLOT_0) */
884 static void ccid_on_slot_change(USBCCIDState *s, bool full)
886 /* RDR_to_PC_NotifySlotChange, 6.3.1 page 56 */
887 uint8_t current = s->bmSlotICCState;
888 if (full) {
889 s->bmSlotICCState |= SLOT_0_STATE_MASK;
890 } else {
891 s->bmSlotICCState &= ~SLOT_0_STATE_MASK;
893 if (current != s->bmSlotICCState) {
894 s->bmSlotICCState |= SLOT_0_CHANGED_MASK;
896 s->notify_slot_change = true;
899 static void ccid_write_data_block_error(
900 USBCCIDState *s, uint8_t slot, uint8_t seq)
902 ccid_write_data_block(s, slot, seq, NULL, 0);
905 static void ccid_on_apdu_from_guest(USBCCIDState *s, CCID_XferBlock *recv)
907 uint32_t len;
909 if (ccid_card_status(s) != ICC_STATUS_PRESENT_ACTIVE) {
910 DPRINTF(s, 1,
911 "usb-ccid: not sending apdu to client, no card connected\n");
912 ccid_write_data_block_error(s, recv->hdr.bSlot, recv->hdr.bSeq);
913 return;
915 len = le32_to_cpu(recv->hdr.dwLength);
916 DPRINTF(s, 1, "%s: seq %d, len %d\n", __func__,
917 recv->hdr.bSeq, len);
918 ccid_add_pending_answer(s, (CCID_Header *)recv);
919 if (s->card) {
920 s->cardinfo->apdu_from_guest(s->card, recv->abData, len);
921 } else {
922 DPRINTF(s, D_WARN, "warning: discarded apdu\n");
927 * Handle a single USB_TOKEN_OUT, return value returned to guest.
928 * Return value:
929 * 0 - all ok
930 * USB_RET_STALL - failed to handle packet
932 static int ccid_handle_bulk_out(USBCCIDState *s, USBPacket *p)
934 CCID_Header *ccid_header;
936 if (p->len + s->bulk_out_pos > BULK_OUT_DATA_SIZE) {
937 return USB_RET_STALL;
939 ccid_header = (CCID_Header *)s->bulk_out_data;
940 memcpy(s->bulk_out_data + s->bulk_out_pos, p->data, p->len);
941 s->bulk_out_pos += p->len;
942 if (p->len == CCID_MAX_PACKET_SIZE) {
943 DPRINTF(s, D_VERBOSE,
944 "usb-ccid: bulk_in: expecting more packets (%d/%d)\n",
945 p->len, ccid_header->dwLength);
946 return 0;
948 if (s->bulk_out_pos < 10) {
949 DPRINTF(s, 1,
950 "%s: bad USB_TOKEN_OUT length, should be at least 10 bytes\n",
951 __func__);
952 } else {
953 DPRINTF(s, D_MORE_INFO, "%s %x\n", __func__, ccid_header->bMessageType);
954 switch (ccid_header->bMessageType) {
955 case CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus:
956 ccid_write_slot_status(s, ccid_header);
957 break;
958 case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn:
959 DPRINTF(s, 1, "PowerOn: %d\n",
960 ((CCID_IccPowerOn *)(ccid_header))->bPowerSelect);
961 s->powered = true;
962 if (!ccid_card_inserted(s)) {
963 ccid_report_error_failed(s, ERROR_ICC_MUTE);
965 /* atr is written regardless of error. */
966 ccid_write_data_block_atr(s, ccid_header);
967 break;
968 case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff:
969 DPRINTF(s, 1, "PowerOff\n");
970 ccid_reset_error_status(s);
971 s->powered = false;
972 ccid_write_slot_status(s, ccid_header);
973 break;
974 case CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock:
975 ccid_on_apdu_from_guest(s, (CCID_XferBlock *)s->bulk_out_data);
976 break;
977 case CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters:
978 ccid_reset_error_status(s);
979 ccid_set_parameters(s, ccid_header);
980 ccid_write_parameters(s, ccid_header);
981 break;
982 case CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters:
983 ccid_reset_error_status(s);
984 ccid_reset_parameters(s);
985 ccid_write_parameters(s, ccid_header);
986 break;
987 case CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters:
988 ccid_reset_error_status(s);
989 ccid_write_parameters(s, ccid_header);
990 break;
991 default:
992 DPRINTF(s, 1,
993 "handle_data: ERROR: unhandled message type %Xh\n",
994 ccid_header->bMessageType);
996 * The caller is expecting the device to respond, tell it we
997 * don't support the operation.
999 ccid_report_error_failed(s, ERROR_CMD_NOT_SUPPORTED);
1000 ccid_write_slot_status(s, ccid_header);
1001 break;
1004 s->bulk_out_pos = 0;
1005 return 0;
1008 static int ccid_bulk_in_copy_to_guest(USBCCIDState *s, uint8_t *data, int len)
1010 int ret = 0;
1012 assert(len > 0);
1013 ccid_bulk_in_get(s);
1014 if (s->current_bulk_in != NULL) {
1015 ret = MIN(s->current_bulk_in->len - s->current_bulk_in->pos, len);
1016 memcpy(data, s->current_bulk_in->data + s->current_bulk_in->pos, ret);
1017 s->current_bulk_in->pos += ret;
1018 if (s->current_bulk_in->pos == s->current_bulk_in->len) {
1019 ccid_bulk_in_release(s);
1021 } else {
1022 /* return when device has no data - usb 2.0 spec Table 8-4 */
1023 ret = USB_RET_NAK;
1025 if (ret > 0) {
1026 DPRINTF(s, D_MORE_INFO,
1027 "%s: %d/%d req/act to guest (BULK_IN)\n", __func__, len, ret);
1029 if (ret != USB_RET_NAK && ret < len) {
1030 DPRINTF(s, 1,
1031 "%s: returning short (EREMOTEIO) %d < %d\n", __func__, ret, len);
1033 return ret;
1036 static int ccid_handle_data(USBDevice *dev, USBPacket *p)
1038 USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
1039 int ret = 0;
1040 uint8_t *data = p->data;
1041 int len = p->len;
1043 switch (p->pid) {
1044 case USB_TOKEN_OUT:
1045 ret = ccid_handle_bulk_out(s, p);
1046 break;
1048 case USB_TOKEN_IN:
1049 switch (p->devep & 0xf) {
1050 case CCID_BULK_IN_EP:
1051 if (!len) {
1052 ret = USB_RET_NAK;
1053 } else {
1054 ret = ccid_bulk_in_copy_to_guest(s, data, len);
1056 break;
1057 case CCID_INT_IN_EP:
1058 if (s->notify_slot_change) {
1059 /* page 56, RDR_to_PC_NotifySlotChange */
1060 data[0] = CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange;
1061 data[1] = s->bmSlotICCState;
1062 ret = 2;
1063 s->notify_slot_change = false;
1064 s->bmSlotICCState &= ~SLOT_0_CHANGED_MASK;
1065 DPRINTF(s, D_INFO,
1066 "handle_data: int_in: notify_slot_change %X, "
1067 "requested len %d\n",
1068 s->bmSlotICCState, len);
1070 break;
1071 default:
1072 DPRINTF(s, 1, "Bad endpoint\n");
1073 break;
1075 break;
1076 default:
1077 DPRINTF(s, 1, "Bad token\n");
1078 ret = USB_RET_STALL;
1079 break;
1082 return ret;
1085 static void ccid_handle_destroy(USBDevice *dev)
1087 USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
1089 ccid_bulk_in_clear(s);
1092 static void ccid_flush_pending_answers(USBCCIDState *s)
1094 while (ccid_has_pending_answers(s)) {
1095 ccid_write_data_block_answer(s, NULL, 0);
1099 static Answer *ccid_peek_next_answer(USBCCIDState *s)
1101 return s->pending_answers_num == 0
1102 ? NULL
1103 : &s->pending_answers[s->pending_answers_start % PENDING_ANSWERS_NUM];
1106 static void ccid_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
1108 CCIDCardState *card = DO_UPCAST(CCIDCardState, qdev, qdev);
1109 CCIDCardInfo *info = DO_UPCAST(CCIDCardInfo, qdev, qdev->info);
1111 if (info->print) {
1112 info->print(mon, card, indent);
1116 struct CCIDBus {
1117 BusState qbus;
1120 static struct BusInfo ccid_bus_info = {
1121 .name = "ccid-bus",
1122 .size = sizeof(CCIDBus),
1123 .print_dev = ccid_bus_dev_print,
1124 .props = (Property[]) {
1125 DEFINE_PROP_UINT32("slot", struct CCIDCardState, slot, 0),
1126 DEFINE_PROP_END_OF_LIST(),
1130 static CCIDBus *ccid_bus_new(DeviceState *dev)
1132 CCIDBus *bus;
1134 bus = FROM_QBUS(CCIDBus, qbus_create(&ccid_bus_info, dev, NULL));
1135 bus->qbus.allow_hotplug = 1;
1137 return bus;
1140 void ccid_card_send_apdu_to_guest(CCIDCardState *card,
1141 uint8_t *apdu, uint32_t len)
1143 USBCCIDState *s = DO_UPCAST(USBCCIDState, dev.qdev,
1144 card->qdev.parent_bus->parent);
1145 Answer *answer;
1147 if (!ccid_has_pending_answers(s)) {
1148 DPRINTF(s, 1, "CCID ERROR: got an APDU without pending answers\n");
1149 return;
1151 s->bmCommandStatus = COMMAND_STATUS_NO_ERROR;
1152 answer = ccid_peek_next_answer(s);
1153 if (answer == NULL) {
1154 abort();
1156 DPRINTF(s, 1, "APDU returned to guest %d (answer seq %d, slot %d)\n",
1157 len, answer->seq, answer->slot);
1158 ccid_write_data_block_answer(s, apdu, len);
1161 void ccid_card_card_removed(CCIDCardState *card)
1163 USBCCIDState *s =
1164 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1166 ccid_on_slot_change(s, false);
1167 ccid_flush_pending_answers(s);
1168 ccid_reset(s);
1171 int ccid_card_ccid_attach(CCIDCardState *card)
1173 USBCCIDState *s =
1174 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1176 DPRINTF(s, 1, "CCID Attach\n");
1177 if (s->migration_state == MIGRATION_MIGRATED) {
1178 s->migration_state = MIGRATION_NONE;
1180 return 0;
1183 void ccid_card_ccid_detach(CCIDCardState *card)
1185 USBCCIDState *s =
1186 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1188 DPRINTF(s, 1, "CCID Detach\n");
1189 if (ccid_card_inserted(s)) {
1190 ccid_on_slot_change(s, false);
1192 ccid_detach(s);
1195 void ccid_card_card_error(CCIDCardState *card, uint64_t error)
1197 USBCCIDState *s =
1198 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1200 s->bmCommandStatus = COMMAND_STATUS_FAILED;
1201 s->last_answer_error = error;
1202 DPRINTF(s, 1, "VSC_Error: %" PRIX64 "\n", s->last_answer_error);
1203 /* TODO: these errors should be more verbose and propagated to the guest.*/
1205 * We flush all pending answers on CardRemove message in ccid-card-passthru,
1206 * so check that first to not trigger abort
1208 if (ccid_has_pending_answers(s)) {
1209 ccid_write_data_block_answer(s, NULL, 0);
1213 void ccid_card_card_inserted(CCIDCardState *card)
1215 USBCCIDState *s =
1216 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1218 s->bmCommandStatus = COMMAND_STATUS_NO_ERROR;
1219 ccid_flush_pending_answers(s);
1220 ccid_on_slot_change(s, true);
1223 static int ccid_card_exit(DeviceState *qdev)
1225 int ret = 0;
1226 CCIDCardState *card = DO_UPCAST(CCIDCardState, qdev, qdev);
1227 CCIDCardInfo *info = DO_UPCAST(CCIDCardInfo, qdev, qdev->info);
1228 USBCCIDState *s =
1229 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1231 if (ccid_card_inserted(s)) {
1232 ccid_card_card_removed(card);
1234 if (info->exitfn) {
1235 ret = info->exitfn(card);
1237 s->card = NULL;
1238 s->cardinfo = NULL;
1239 return ret;
1242 static int ccid_card_init(DeviceState *qdev, DeviceInfo *base)
1244 CCIDCardState *card = DO_UPCAST(CCIDCardState, qdev, qdev);
1245 CCIDCardInfo *info = DO_UPCAST(CCIDCardInfo, qdev, base);
1246 USBCCIDState *s =
1247 DO_UPCAST(USBCCIDState, dev.qdev, card->qdev.parent_bus->parent);
1248 int ret = 0;
1250 if (card->slot != 0) {
1251 error_report("Warning: usb-ccid supports one slot, can't add %d",
1252 card->slot);
1253 return -1;
1255 if (s->card != NULL) {
1256 error_report("Warning: usb-ccid card already full, not adding\n");
1257 return -1;
1259 ret = info->initfn ? info->initfn(card) : ret;
1260 if (ret == 0) {
1261 s->card = card;
1262 s->cardinfo = info;
1264 return ret;
1267 void ccid_card_qdev_register(CCIDCardInfo *card)
1269 card->qdev.bus_info = &ccid_bus_info;
1270 card->qdev.init = ccid_card_init;
1271 card->qdev.exit = ccid_card_exit;
1272 qdev_register(&card->qdev);
1275 static int ccid_initfn(USBDevice *dev)
1277 USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
1279 s->bus = ccid_bus_new(&dev->qdev);
1280 s->card = NULL;
1281 s->cardinfo = NULL;
1282 s->migration_state = MIGRATION_NONE;
1283 s->migration_target_ip = 0;
1284 s->migration_target_port = 0;
1285 s->dev.speed = USB_SPEED_FULL;
1286 s->notify_slot_change = false;
1287 s->powered = true;
1288 s->pending_answers_num = 0;
1289 s->last_answer_error = 0;
1290 s->bulk_in_pending_start = 0;
1291 s->bulk_in_pending_end = 0;
1292 s->current_bulk_in = NULL;
1293 ccid_reset_error_status(s);
1294 s->bulk_out_pos = 0;
1295 ccid_reset_parameters(s);
1296 ccid_reset(s);
1297 return 0;
1300 static int ccid_post_load(void *opaque, int version_id)
1302 USBCCIDState *s = opaque;
1305 * This must be done after usb_device_attach, which sets state to ATTACHED,
1306 * while it must be DEFAULT in order to accept packets (like it is after
1307 * reset, but reset will reset our addr and call our reset handler which
1308 * may change state, and we don't want to do that when migrating).
1310 s->dev.state = s->state_vmstate;
1311 return 0;
1314 static void ccid_pre_save(void *opaque)
1316 USBCCIDState *s = opaque;
1318 s->state_vmstate = s->dev.state;
1319 if (s->dev.attached) {
1321 * Migrating an open device, ignore reconnection CHR_EVENT to avoid an
1322 * erroneous detach.
1324 s->migration_state = MIGRATION_MIGRATED;
1328 static VMStateDescription bulk_in_vmstate = {
1329 .name = "CCID BulkIn state",
1330 .version_id = 1,
1331 .minimum_version_id = 1,
1332 .fields = (VMStateField[]) {
1333 VMSTATE_BUFFER(data, BulkIn),
1334 VMSTATE_UINT32(len, BulkIn),
1335 VMSTATE_UINT32(pos, BulkIn),
1336 VMSTATE_END_OF_LIST()
1340 static VMStateDescription answer_vmstate = {
1341 .name = "CCID Answer state",
1342 .version_id = 1,
1343 .minimum_version_id = 1,
1344 .fields = (VMStateField[]) {
1345 VMSTATE_UINT8(slot, Answer),
1346 VMSTATE_UINT8(seq, Answer),
1347 VMSTATE_END_OF_LIST()
1351 static VMStateDescription usb_device_vmstate = {
1352 .name = "usb_device",
1353 .version_id = 1,
1354 .minimum_version_id = 1,
1355 .fields = (VMStateField[]) {
1356 VMSTATE_UINT8(addr, USBDevice),
1357 VMSTATE_BUFFER(setup_buf, USBDevice),
1358 VMSTATE_BUFFER(data_buf, USBDevice),
1359 VMSTATE_END_OF_LIST()
1363 static VMStateDescription ccid_vmstate = {
1364 .name = CCID_DEV_NAME,
1365 .version_id = 1,
1366 .minimum_version_id = 1,
1367 .post_load = ccid_post_load,
1368 .pre_save = ccid_pre_save,
1369 .fields = (VMStateField[]) {
1370 VMSTATE_STRUCT(dev, USBCCIDState, 1, usb_device_vmstate, USBDevice),
1371 VMSTATE_UINT8(debug, USBCCIDState),
1372 VMSTATE_BUFFER(bulk_out_data, USBCCIDState),
1373 VMSTATE_UINT32(bulk_out_pos, USBCCIDState),
1374 VMSTATE_UINT8(bmSlotICCState, USBCCIDState),
1375 VMSTATE_UINT8(powered, USBCCIDState),
1376 VMSTATE_UINT8(notify_slot_change, USBCCIDState),
1377 VMSTATE_UINT64(last_answer_error, USBCCIDState),
1378 VMSTATE_UINT8(bError, USBCCIDState),
1379 VMSTATE_UINT8(bmCommandStatus, USBCCIDState),
1380 VMSTATE_UINT8(bProtocolNum, USBCCIDState),
1381 VMSTATE_BUFFER(abProtocolDataStructure, USBCCIDState),
1382 VMSTATE_UINT32(ulProtocolDataStructureSize, USBCCIDState),
1383 VMSTATE_STRUCT_ARRAY(bulk_in_pending, USBCCIDState,
1384 BULK_IN_PENDING_NUM, 1, bulk_in_vmstate, BulkIn),
1385 VMSTATE_UINT32(bulk_in_pending_start, USBCCIDState),
1386 VMSTATE_UINT32(bulk_in_pending_end, USBCCIDState),
1387 VMSTATE_STRUCT_ARRAY(pending_answers, USBCCIDState,
1388 PENDING_ANSWERS_NUM, 1, answer_vmstate, Answer),
1389 VMSTATE_UINT32(pending_answers_num, USBCCIDState),
1390 VMSTATE_UINT8(migration_state, USBCCIDState),
1391 VMSTATE_UINT32(state_vmstate, USBCCIDState),
1392 VMSTATE_END_OF_LIST()
1396 static struct USBDeviceInfo ccid_info = {
1397 .product_desc = "QEMU USB CCID",
1398 .qdev.name = CCID_DEV_NAME,
1399 .qdev.desc = "CCID Rev 1.1 smartcard reader",
1400 .qdev.size = sizeof(USBCCIDState),
1401 .init = ccid_initfn,
1402 .handle_packet = usb_generic_handle_packet,
1403 .handle_reset = ccid_handle_reset,
1404 .handle_control = ccid_handle_control,
1405 .handle_data = ccid_handle_data,
1406 .handle_destroy = ccid_handle_destroy,
1407 .usbdevice_name = "ccid",
1408 .qdev.props = (Property[]) {
1409 DEFINE_PROP_UINT8("debug", USBCCIDState, debug, 0),
1410 DEFINE_PROP_END_OF_LIST(),
1412 .qdev.vmsd = &ccid_vmstate,
1415 static void ccid_register_devices(void)
1417 usb_qdev_register(&ccid_info);
1419 device_init(ccid_register_devices)