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