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