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,
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 * Endianess note: from the spec (1.3)
26 * "Fields that are larger than a byte are stored in little endian"
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"
44 #define DPRINTF(s, lvl, fmt, ...) \
46 if (lvl <= s->debug) { \
47 printf("usb-ccid: " fmt , ## __VA_ARGS__); \
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
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) */
149 ICC_STATUS_PRESENT_ACTIVE
= 0,
150 ICC_STATUS_PRESENT_INACTIVE
,
151 ICC_STATUS_NOT_PRESENT
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) */
162 ERROR_CMD_NOT_SUPPORTED
= 0,
163 ERROR_CMD_ABORTED
= -1,
165 ERROR_XFR_PARITY_ERROR
= -3,
166 ERROR_XFR_OVERRUN
= -4,
170 /* 6.2.6 RDR_to_PC_SlotStatus definitions */
172 CLOCK_STATUS_RUNNING
= 0,
174 * 0 - Clock Running, 1 - Clock stopped in State L, 2 - H,
175 * 3 - unkonwn state. rest are RFU
179 typedef struct __attribute__ ((__packed__
)) CCID_Header
{
180 uint8_t bMessageType
;
186 typedef struct __attribute__ ((__packed__
)) CCID_BULK_IN
{
188 uint8_t bStatus
; /* Only used in BULK_IN */
189 uint8_t bError
; /* Only used in BULK_IN */
192 typedef struct __attribute__ ((__packed__
)) CCID_SlotStatus
{
194 uint8_t bClockStatus
;
197 typedef struct __attribute__ ((__packed__
)) CCID_Parameter
{
199 uint8_t bProtocolNum
;
200 uint8_t abProtocolDataStructure
[0];
203 typedef struct __attribute__ ((__packed__
)) CCID_DataBlock
{
205 uint8_t bChainParameter
;
209 /* 6.1.4 PC_to_RDR_XfrBlock */
210 typedef struct __attribute__ ((__packed__
)) CCID_XferBlock
{
212 uint8_t bBWI
; /* Block Waiting Timeout */
213 uint16_t wLevelParameter
; /* XXX currently unused */
217 typedef struct __attribute__ ((__packed__
)) CCID_IccPowerOn
{
219 uint8_t bPowerSelect
;
223 typedef struct __attribute__ ((__packed__
)) CCID_IccPowerOff
{
228 typedef struct __attribute__ ((__packed__
)) CCID_SetParameters
{
230 uint8_t bProtocolNum
;
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
{
246 /* pending BULK_IN messages */
247 typedef struct BulkIn
{
248 uint8_t data
[BULK_IN_BUF_SIZE
];
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
{
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
;
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
;
294 uint8_t notify_slot_change
;
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
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. */
320 CCID_VENDOR_ID
& 0xff, CCID_VENDOR_ID
>> 8,
322 CCID_PRODUCT_ID
& 0xff, CCID_PRODUCT_ID
>> 8,
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;
345 100/2, /* u8 MaxPower; 50 == 100mA */
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. */
363 * u8 bMaxSlotIndex; The index of the highest available
364 * slot on this device. All slots are consecutive starting
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
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,
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,
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.
429 * u8 bClassEnvelope; EAPDU only. Envelope command for
433 * u16 wLcdLayout; XXYY Number of lines (XX) and chars per
434 * line for LCD display used for PIN entry. 0000 - no LCD
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 */
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 */
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 */
467 /* u8 ep_bEndpointAddress; OUT Endpoint 3 */
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
)
492 DPRINTF(s
, D_VERBOSE
, "usb-ccid: pending answers:");
493 if (!ccid_has_pending_answers(s
)) {
494 DPRINTF(s
, D_VERBOSE
, " empty\n");
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
];
501 DPRINTF(s
, D_VERBOSE
, "%d:%d\n", answer
->slot
, answer
->seq
);
503 DPRINTF(s
, D_VERBOSE
, "%d:%d,", answer
->slot
, answer
->seq
);
508 static void ccid_add_pending_answer(USBCCIDState
*s
, CCID_Header
*hdr
)
512 assert(s
->pending_answers_num
< PENDING_ANSWERS_NUM
);
513 s
->pending_answers_num
++;
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
)
526 assert(s
->pending_answers_num
> 0);
527 s
->pending_answers_num
--;
529 &s
->pending_answers
[(s
->pending_answers_start
++) % PENDING_ANSWERS_NUM
];
530 *slot
= answer
->slot
;
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) {
554 assert(s
->bulk_in_pending_num
> 0);
555 s
->bulk_in_pending_num
--;
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
)
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
);
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__
);
579 &s
->bulk_in_pending
[(s
->bulk_in_pending_end
++) % BULK_IN_PENDING_NUM
];
580 s
->bulk_in_pending_num
++;
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
)
596 static void ccid_handle_reset(USBDevice
*dev
)
598 USBCCIDState
*s
= DO_UPCAST(USBCCIDState
, dev
, dev
);
600 DPRINTF(s
, 1, "Reset\n");
605 static int ccid_handle_control(USBDevice
*dev
, int request
, int value
,
606 int index
, int length
, uint8_t *data
)
608 USBCCIDState
*s
= DO_UPCAST(USBCCIDState
, dev
, dev
);
611 DPRINTF(s
, 1, "got control %x, value %x\n", request
, value
);
613 case DeviceRequest
| USB_REQ_GET_STATUS
:
614 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
615 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
619 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
620 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
621 dev
->remote_wakeup
= 0;
627 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
628 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
629 dev
->remote_wakeup
= 1;
635 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
639 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
640 switch (value
>> 8) {
642 memcpy(data
, qemu_ccid_dev_descriptor
,
643 sizeof(qemu_ccid_dev_descriptor
));
644 ret
= sizeof(qemu_ccid_dev_descriptor
);
647 memcpy(data
, qemu_ccid_config_descriptor
,
648 sizeof(qemu_ccid_config_descriptor
));
649 ret
= sizeof(qemu_ccid_config_descriptor
);
652 switch (value
& 0xff) {
662 /* vendor description */
663 ret
= set_usb_string(data
, CCID_VENDOR_DESCRIPTION
);
666 /* product description */
667 ret
= set_usb_string(data
, CCID_PRODUCT_DESCRIPTION
);
671 ret
= set_usb_string(data
, CCID_SERIAL_NUMBER_STRING
);
675 ret
= set_usb_string(data
, CCID_INTERFACE_NAME
);
685 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
689 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
690 /* Only one configuration - we just ignore the request */
693 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
697 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
700 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
704 /* Class specific requests. */
705 case InterfaceOutClass
| CCID_CONTROL_ABORT
:
706 DPRINTF(s
, 1, "ccid_control abort UNIMPLEMENTED\n");
709 case InterfaceInClass
| CCID_CONTROL_GET_CLOCK_FREQUENCIES
:
710 DPRINTF(s
, 1, "ccid_control get clock frequencies UNIMPLEMENTED\n");
713 case InterfaceInClass
| CCID_CONTROL_GET_DATA_RATES
:
714 DPRINTF(s
, 1, "ccid_control get data rates UNIMPLEMENTED\n");
719 DPRINTF(s
, 1, "got unsupported/bogus control %x, value %x\n",
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
)
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
748 uint8_t ret
= ccid_card_status(s
) | (s
->bmCommandStatus
<< 6);
749 DPRINTF(s
, D_VERBOSE
, "status = %d\n", 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
));
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
)
778 uint32_t len
= s
->ulProtocolDataStructureSize
;
780 h
= ccid_reserve_recv_buf(s
, sizeof(CCID_Parameter
) + len
);
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
);
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
;
807 p
->b
.bStatus
= ccid_calc_status(s
);
808 p
->b
.bError
= s
->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
)
822 if (!ccid_has_pending_answers(s
)) {
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
;
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
;
844 if ((ph
->bProtocolNum
& 3) == 0) {
847 if ((ph
->bProtocolNum
& 3) == 1) {
851 s
->bmCommandStatus
= COMMAND_STATUS_FAILED
;
852 s
->bError
= 7; /* Protocol invalid or not supported */
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
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
;
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
;
889 s
->bmSlotICCState
|= SLOT_0_STATE_MASK
;
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
)
909 if (ccid_card_status(s
) != ICC_STATUS_PRESENT_ACTIVE
) {
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
);
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
);
920 s
->cardinfo
->apdu_from_guest(s
->card
, recv
->abData
, len
);
922 DPRINTF(s
, D_WARN
, "warning: discarded apdu\n");
927 * Handle a single USB_TOKEN_OUT, return value returned to guest.
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
);
948 if (s
->bulk_out_pos
< 10) {
950 "%s: bad USB_TOKEN_OUT length, should be at least 10 bytes\n",
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
);
958 case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn
:
959 DPRINTF(s
, 1, "PowerOn: %d\n",
960 ((CCID_IccPowerOn
*)(ccid_header
))->bPowerSelect
);
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
);
968 case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff
:
969 DPRINTF(s
, 1, "PowerOff\n");
970 ccid_reset_error_status(s
);
972 ccid_write_slot_status(s
, ccid_header
);
974 case CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock
:
975 ccid_on_apdu_from_guest(s
, (CCID_XferBlock
*)s
->bulk_out_data
);
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
);
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
);
987 case CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters
:
988 ccid_reset_error_status(s
);
989 ccid_write_parameters(s
, ccid_header
);
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
);
1004 s
->bulk_out_pos
= 0;
1008 static int ccid_bulk_in_copy_to_guest(USBCCIDState
*s
, uint8_t *data
, int len
)
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
);
1022 /* return when device has no data - usb 2.0 spec Table 8-4 */
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
) {
1031 "%s: returning short (EREMOTEIO) %d < %d\n", __func__
, ret
, len
);
1036 static int ccid_handle_data(USBDevice
*dev
, USBPacket
*p
)
1038 USBCCIDState
*s
= DO_UPCAST(USBCCIDState
, dev
, dev
);
1040 uint8_t *data
= p
->data
;
1045 ret
= ccid_handle_bulk_out(s
, p
);
1049 switch (p
->devep
& 0xf) {
1050 case CCID_BULK_IN_EP
:
1054 ret
= ccid_bulk_in_copy_to_guest(s
, data
, len
);
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
;
1063 s
->notify_slot_change
= false;
1064 s
->bmSlotICCState
&= ~SLOT_0_CHANGED_MASK
;
1066 "handle_data: int_in: notify_slot_change %X, "
1067 "requested len %d\n",
1068 s
->bmSlotICCState
, len
);
1072 DPRINTF(s
, 1, "Bad endpoint\n");
1077 DPRINTF(s
, 1, "Bad token\n");
1078 ret
= USB_RET_STALL
;
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
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
);
1112 info
->print(mon
, card
, indent
);
1120 static struct BusInfo ccid_bus_info
= {
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
)
1134 bus
= FROM_QBUS(CCIDBus
, qbus_create(&ccid_bus_info
, dev
, NULL
));
1135 bus
->qbus
.allow_hotplug
= 1;
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
);
1147 if (!ccid_has_pending_answers(s
)) {
1148 DPRINTF(s
, 1, "CCID ERROR: got an APDU without pending answers\n");
1151 s
->bmCommandStatus
= COMMAND_STATUS_NO_ERROR
;
1152 answer
= ccid_peek_next_answer(s
);
1153 if (answer
== NULL
) {
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
)
1164 DO_UPCAST(USBCCIDState
, dev
.qdev
, card
->qdev
.parent_bus
->parent
);
1166 ccid_on_slot_change(s
, false);
1167 ccid_flush_pending_answers(s
);
1171 int ccid_card_ccid_attach(CCIDCardState
*card
)
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
;
1183 void ccid_card_ccid_detach(CCIDCardState
*card
)
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);
1195 void ccid_card_card_error(CCIDCardState
*card
, uint64_t error
)
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: %lX\n", s
->last_answer_error
);
1203 /* TODO: these error's should be more verbose and propogated 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
)
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
)
1226 CCIDCardState
*card
= DO_UPCAST(CCIDCardState
, qdev
, qdev
);
1227 CCIDCardInfo
*info
= DO_UPCAST(CCIDCardInfo
, qdev
, qdev
->info
);
1229 DO_UPCAST(USBCCIDState
, dev
.qdev
, card
->qdev
.parent_bus
->parent
);
1231 if (ccid_card_inserted(s
)) {
1232 ccid_card_card_removed(card
);
1235 ret
= info
->exitfn(card
);
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
);
1247 DO_UPCAST(USBCCIDState
, dev
.qdev
, card
->qdev
.parent_bus
->parent
);
1250 if (card
->slot
!= 0) {
1251 error_report("Warning: usb-ccid supports one slot, can't add %d",
1255 if (s
->card
!= NULL
) {
1256 error_report("Warning: usb-ccid card already full, not adding\n");
1259 ret
= info
->initfn
? info
->initfn(card
) : 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
);
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;
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
);
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
;
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
1324 s
->migration_state
= MIGRATION_MIGRATED
;
1328 static VMStateDescription bulk_in_vmstate
= {
1329 .name
= "CCID BulkIn state",
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",
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",
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
,
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
)