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