~ubuntu-branches/ubuntu/raring/qemu-kvm/raring

« back to all changes in this revision

Viewing changes to hw/usb-ccid.c

  • Committer: Serge Hallyn
  • Date: 2011-12-06 22:40:24 UTC
  • mfrom: (1.2.8)
  • Revision ID: serge.hallyn@ubuntu.com-20111206224024-x6bw4wnn65pgf55v
Tags: 1.0+noroms-0ubuntu1
* New upstream release
* Remaining changes from upstream:
  - removed all binary roms and tests/pi_10.com
* debian/qemu-kvm.links: qemu is now called qemu-system-i386, don't symlink
  it
* remove patches applied upstream:
  - debian/patches/vpc.patch
  - debian/patches/e1000-Dont-set-the-Capabilities-List-bit.patch
  - debian/patches/CVE-2011-4111.patch
* replace default-to-tcg.patch with simpler fallback-to-tcg.patch
* keep remaining patches:
  - larger_default_ram_size.patch
  - CVE-2011-2212-virtqueue-indirect-overflow.patch
  - qemuifup-fix-paths.patch
  - dont-try-to-hotplug-cpu.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "qemu-common.h"
38
38
#include "qemu-error.h"
39
39
#include "usb.h"
 
40
#include "usb-desc.h"
40
41
#include "monitor.h"
41
42
 
42
43
#include "hw/ccid.h"
176
177
     */
177
178
};
178
179
 
179
 
typedef struct __attribute__ ((__packed__)) CCID_Header {
 
180
typedef struct QEMU_PACKED CCID_Header {
180
181
    uint8_t     bMessageType;
181
182
    uint32_t    dwLength;
182
183
    uint8_t     bSlot;
183
184
    uint8_t     bSeq;
184
185
} CCID_Header;
185
186
 
186
 
typedef struct __attribute__ ((__packed__)) CCID_BULK_IN {
 
187
typedef struct QEMU_PACKED CCID_BULK_IN {
187
188
    CCID_Header hdr;
188
189
    uint8_t     bStatus;        /* Only used in BULK_IN */
189
190
    uint8_t     bError;         /* Only used in BULK_IN */
190
191
} CCID_BULK_IN;
191
192
 
192
 
typedef struct __attribute__ ((__packed__)) CCID_SlotStatus {
 
193
typedef struct QEMU_PACKED CCID_SlotStatus {
193
194
    CCID_BULK_IN b;
194
195
    uint8_t     bClockStatus;
195
196
} CCID_SlotStatus;
196
197
 
197
 
typedef struct __attribute__ ((__packed__)) CCID_Parameter {
 
198
typedef struct QEMU_PACKED CCID_Parameter {
198
199
    CCID_BULK_IN b;
199
200
    uint8_t     bProtocolNum;
200
201
    uint8_t     abProtocolDataStructure[0];
201
202
} CCID_Parameter;
202
203
 
203
 
typedef struct __attribute__ ((__packed__)) CCID_DataBlock {
 
204
typedef struct QEMU_PACKED CCID_DataBlock {
204
205
    CCID_BULK_IN b;
205
206
    uint8_t      bChainParameter;
206
207
    uint8_t      abData[0];
207
208
} CCID_DataBlock;
208
209
 
209
210
/* 6.1.4 PC_to_RDR_XfrBlock */
210
 
typedef struct __attribute__ ((__packed__)) CCID_XferBlock {
 
211
typedef struct QEMU_PACKED CCID_XferBlock {
211
212
    CCID_Header  hdr;
212
213
    uint8_t      bBWI; /* Block Waiting Timeout */
213
214
    uint16_t     wLevelParameter; /* XXX currently unused */
214
215
    uint8_t      abData[0];
215
216
} CCID_XferBlock;
216
217
 
217
 
typedef struct __attribute__ ((__packed__)) CCID_IccPowerOn {
 
218
typedef struct QEMU_PACKED CCID_IccPowerOn {
218
219
    CCID_Header hdr;
219
220
    uint8_t     bPowerSelect;
220
221
    uint16_t    abRFU;
221
222
} CCID_IccPowerOn;
222
223
 
223
 
typedef struct __attribute__ ((__packed__)) CCID_IccPowerOff {
 
224
typedef struct QEMU_PACKED CCID_IccPowerOff {
224
225
    CCID_Header hdr;
225
226
    uint16_t    abRFU;
226
227
} CCID_IccPowerOff;
227
228
 
228
 
typedef struct __attribute__ ((__packed__)) CCID_SetParameters {
 
229
typedef struct QEMU_PACKED CCID_SetParameters {
229
230
    CCID_Header hdr;
230
231
    uint8_t     bProtocolNum;
231
232
    uint16_t   abRFU;
306
307
 *   0dc3:1004 Athena Smartcard Solutions, Inc.
307
308
 */
308
309
 
309
 
static const uint8_t qemu_ccid_dev_descriptor[] = {
310
 
        0x12,       /*  u8 bLength; */
311
 
        USB_DT_DEVICE, /*  u8 bDescriptorType; Device */
312
 
        0x10, 0x01, /*  u16 bcdUSB; v1.1 */
313
 
 
314
 
        0x00,       /*  u8  bDeviceClass; */
315
 
        0x00,       /*  u8  bDeviceSubClass; */
316
 
        0x00,       /*  u8  bDeviceProtocol; [ low/full speeds only ] */
317
 
        0x40,       /*  u8  bMaxPacketSize0; 8 Bytes (valid: 8,16,32,64) */
318
 
 
319
 
        /* Vendor and product id are arbitrary.  */
320
 
                    /*  u16 idVendor  */
321
 
        CCID_VENDOR_ID & 0xff, CCID_VENDOR_ID >> 8,
322
 
                    /*  u16 idProduct */
323
 
        CCID_PRODUCT_ID & 0xff, CCID_PRODUCT_ID >> 8,
324
 
                    /*  u16 bcdDevice */
325
 
        CCID_DEVICE_VERSION & 0xff, CCID_DEVICE_VERSION >> 8,
326
 
        0x01,       /*  u8  iManufacturer; */
327
 
        0x02,       /*  u8  iProduct; */
328
 
        0x03,       /*  u8  iSerialNumber; */
329
 
        0x01,       /*  u8  bNumConfigurations; */
330
 
};
331
 
 
332
 
static const uint8_t qemu_ccid_config_descriptor[] = {
333
 
 
334
 
        /* one configuration */
335
 
        0x09,       /* u8  bLength; */
336
 
        USB_DT_CONFIG, /* u8  bDescriptorType; Configuration */
337
 
        0x5d, 0x00, /* u16 wTotalLength; 9+9+54+7+7+7 */
338
 
        0x01,       /* u8  bNumInterfaces; (1) */
339
 
        0x01,       /* u8  bConfigurationValue; */
340
 
        0x00,       /* u8  iConfiguration; */
341
 
        0xe0,       /* u8  bmAttributes;
342
 
                                 Bit 7: must be set,
343
 
                                     6: Self-powered,
344
 
                                     5: Remote wakeup,
345
 
                                     4..0: resvd */
346
 
        100/2,      /* u8  MaxPower; 50 == 100mA */
347
 
 
348
 
        /* one interface */
349
 
        0x09,       /* u8  if_bLength; */
350
 
        USB_DT_INTERFACE, /* u8  if_bDescriptorType; Interface */
351
 
        0x00,       /* u8  if_bInterfaceNumber; */
352
 
        0x00,       /* u8  if_bAlternateSetting; */
353
 
        0x03,       /* u8  if_bNumEndpoints; */
354
 
        0x0b,       /* u8  if_bInterfaceClass; Smart Card Device Class */
355
 
        0x00,       /* u8  if_bInterfaceSubClass; Subclass code */
356
 
        0x00,       /* u8  if_bInterfaceProtocol; Protocol code */
357
 
        0x04,       /* u8  if_iInterface; Index of string descriptor */
358
 
 
 
310
static const uint8_t qemu_ccid_descriptor[] = {
359
311
        /* Smart Card Device Class Descriptor */
360
312
        0x36,       /* u8  bLength; */
361
313
        0x21,       /* u8  bDescriptorType; Functional */
439
391
                     *                  02h PIN Modification
440
392
                     */
441
393
        0x01,       /* u8  bMaxCCIDBusySlots; */
442
 
 
443
 
        /* Interrupt-IN endpoint */
444
 
        0x07,       /* u8  ep_bLength; */
445
 
                    /* u8  ep_bDescriptorType; Endpoint */
446
 
        USB_DT_ENDPOINT,
447
 
                    /* u8  ep_bEndpointAddress; IN Endpoint 1 */
448
 
        0x80 | CCID_INT_IN_EP,
449
 
        0x03,       /* u8  ep_bmAttributes; Interrupt */
450
 
                    /* u16 ep_wMaxPacketSize; */
451
 
        CCID_MAX_PACKET_SIZE & 0xff, (CCID_MAX_PACKET_SIZE >> 8),
452
 
        0xff,       /* u8  ep_bInterval; */
453
 
 
454
 
        /* Bulk-In endpoint */
455
 
        0x07,       /* u8  ep_bLength; */
456
 
                    /* u8  ep_bDescriptorType; Endpoint */
457
 
        USB_DT_ENDPOINT,
458
 
                    /* u8  ep_bEndpointAddress; IN Endpoint 2 */
459
 
        0x80 | CCID_BULK_IN_EP,
460
 
        0x02,       /* u8  ep_bmAttributes; Bulk */
461
 
        0x40, 0x00, /* u16 ep_wMaxPacketSize; */
462
 
        0x00,       /* u8  ep_bInterval; */
463
 
 
464
 
        /* Bulk-Out endpoint */
465
 
        0x07,       /* u8  ep_bLength; */
466
 
                    /* u8  ep_bDescriptorType; Endpoint */
467
 
        USB_DT_ENDPOINT,
468
 
                    /* u8  ep_bEndpointAddress; OUT Endpoint 3 */
469
 
        CCID_BULK_OUT_EP,
470
 
        0x02,       /* u8  ep_bmAttributes; Bulk */
471
 
        0x40, 0x00, /* u16 ep_wMaxPacketSize; */
472
 
        0x00,       /* u8  ep_bInterval; */
473
 
 
 
394
};
 
395
 
 
396
enum {
 
397
    STR_MANUFACTURER = 1,
 
398
    STR_PRODUCT,
 
399
    STR_SERIALNUMBER,
 
400
    STR_INTERFACE,
 
401
};
 
402
 
 
403
static const USBDescStrings desc_strings = {
 
404
    [STR_MANUFACTURER]  = "QEMU " QEMU_VERSION,
 
405
    [STR_PRODUCT]       = "QEMU USB CCID",
 
406
    [STR_SERIALNUMBER]  = "1",
 
407
    [STR_INTERFACE]     = "CCID Interface",
 
408
};
 
409
 
 
410
static const USBDescIface desc_iface0 = {
 
411
    .bInterfaceNumber              = 0,
 
412
    .bNumEndpoints                 = 3,
 
413
    .bInterfaceClass               = 0x0b,
 
414
    .bInterfaceSubClass            = 0x00,
 
415
    .bInterfaceProtocol            = 0x00,
 
416
    .iInterface                    = STR_INTERFACE,
 
417
    .ndesc                         = 1,
 
418
    .descs = (USBDescOther[]) {
 
419
        {
 
420
            /* smartcard descriptor */
 
421
            .data = qemu_ccid_descriptor,
 
422
        },
 
423
    },
 
424
    .eps = (USBDescEndpoint[]) {
 
425
        {
 
426
            .bEndpointAddress      = USB_DIR_IN | CCID_INT_IN_EP,
 
427
            .bmAttributes          = USB_ENDPOINT_XFER_INT,
 
428
            .bInterval             = 255,
 
429
            .wMaxPacketSize        = 64,
 
430
        },{
 
431
            .bEndpointAddress      = USB_DIR_IN | CCID_BULK_IN_EP,
 
432
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
 
433
            .wMaxPacketSize        = 64,
 
434
        },{
 
435
            .bEndpointAddress      = USB_DIR_OUT | CCID_BULK_OUT_EP,
 
436
            .bmAttributes          = USB_ENDPOINT_XFER_BULK,
 
437
            .wMaxPacketSize        = 64,
 
438
        },
 
439
    }
 
440
};
 
441
 
 
442
static const USBDescDevice desc_device = {
 
443
    .bcdUSB                        = 0x0110,
 
444
    .bMaxPacketSize0               = 64,
 
445
    .bNumConfigurations            = 1,
 
446
    .confs = (USBDescConfig[]) {
 
447
        {
 
448
            .bNumInterfaces        = 1,
 
449
            .bConfigurationValue   = 1,
 
450
            .bmAttributes          = 0xa0,
 
451
            .bMaxPower             = 50,
 
452
            .nif = 1,
 
453
            .ifs = &desc_iface0,
 
454
        },
 
455
    },
 
456
};
 
457
 
 
458
static const USBDesc desc_ccid = {
 
459
    .id = {
 
460
        .idVendor          = CCID_VENDOR_ID,
 
461
        .idProduct         = CCID_PRODUCT_ID,
 
462
        .bcdDevice         = CCID_DEVICE_VERSION,
 
463
        .iManufacturer     = STR_MANUFACTURER,
 
464
        .iProduct          = STR_PRODUCT,
 
465
        .iSerialNumber     = STR_SERIALNUMBER,
 
466
    },
 
467
    .full = &desc_device,
 
468
    .str  = desc_strings,
474
469
};
475
470
 
476
471
static bool ccid_has_pending_answers(USBCCIDState *s)
610
605
    int ret = 0;
611
606
 
612
607
    DPRINTF(s, 1, "got control %x, value %x\n", request, value);
 
608
    ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
 
609
    if (ret >= 0) {
 
610
        return ret;
 
611
    }
 
612
 
613
613
    switch (request) {
614
 
    case DeviceRequest | USB_REQ_GET_STATUS:
615
 
        data[0] = (1 << USB_DEVICE_SELF_POWERED) |
616
 
            (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
617
 
        data[1] = 0x00;
618
 
        ret = 2;
619
 
        break;
620
 
    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
621
 
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
622
 
            dev->remote_wakeup = 0;
623
 
        } else {
624
 
            goto fail;
625
 
        }
626
 
        ret = 0;
627
 
        break;
628
 
    case DeviceOutRequest | USB_REQ_SET_FEATURE:
629
 
        if (value == USB_DEVICE_REMOTE_WAKEUP) {
630
 
            dev->remote_wakeup = 1;
631
 
        } else {
632
 
            goto fail;
633
 
        }
634
 
        ret = 0;
635
 
        break;
636
 
    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
637
 
        dev->addr = value;
638
 
        ret = 0;
639
 
        break;
640
 
    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
641
 
        switch (value >> 8) {
642
 
        case USB_DT_DEVICE:
643
 
            memcpy(data, qemu_ccid_dev_descriptor,
644
 
                   sizeof(qemu_ccid_dev_descriptor));
645
 
            ret = sizeof(qemu_ccid_dev_descriptor);
646
 
            break;
647
 
        case USB_DT_CONFIG:
648
 
            memcpy(data, qemu_ccid_config_descriptor,
649
 
                   sizeof(qemu_ccid_config_descriptor));
650
 
            ret = sizeof(qemu_ccid_config_descriptor);
651
 
            break;
652
 
        case USB_DT_STRING:
653
 
            switch (value & 0xff) {
654
 
            case 0:
655
 
                /* language ids */
656
 
                data[0] = 4;
657
 
                data[1] = 3;
658
 
                data[2] = 0x09;
659
 
                data[3] = 0x04;
660
 
                ret = 4;
661
 
                break;
662
 
            case 1:
663
 
                /* vendor description */
664
 
                ret = set_usb_string(data, CCID_VENDOR_DESCRIPTION);
665
 
                break;
666
 
            case 2:
667
 
                /* product description */
668
 
                ret = set_usb_string(data, CCID_PRODUCT_DESCRIPTION);
669
 
                break;
670
 
            case 3:
671
 
                /* serial number */
672
 
                ret = set_usb_string(data, CCID_SERIAL_NUMBER_STRING);
673
 
                break;
674
 
            case 4:
675
 
                /* interface name */
676
 
                ret = set_usb_string(data, CCID_INTERFACE_NAME);
677
 
                break;
678
 
            default:
679
 
                goto fail;
680
 
            }
681
 
            break;
682
 
        default:
683
 
            goto fail;
684
 
        }
685
 
        break;
686
 
    case DeviceRequest | USB_REQ_GET_CONFIGURATION:
687
 
        data[0] = 1;
688
 
        ret = 1;
689
 
        break;
690
 
    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
691
 
        /* Only one configuration - we just ignore the request */
692
 
        ret = 0;
693
 
        break;
694
614
    case DeviceRequest | USB_REQ_GET_INTERFACE:
695
615
        data[0] = 0;
696
616
        ret = 1;
698
618
    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
699
619
        ret = 0;
700
620
        break;
701
 
    case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
702
 
        ret = 0;
703
 
        break;
704
621
 
705
622
        /* Class specific requests.  */
706
623
    case InterfaceOutClass | CCID_CONTROL_ABORT:
716
633
        ret = USB_RET_STALL;
717
634
        break;
718
635
    default:
719
 
fail:
720
636
        DPRINTF(s, 1, "got unsupported/bogus control %x, value %x\n",
721
637
                request, value);
722
638
        ret = USB_RET_STALL;
895
811
        s->bmSlotICCState |= SLOT_0_CHANGED_MASK;
896
812
    }
897
813
    s->notify_slot_change = true;
 
814
    usb_wakeup(&s->dev);
898
815
}
899
816
 
900
817
static void ccid_write_data_block_error(
934
851
{
935
852
    CCID_Header *ccid_header;
936
853
 
937
 
    if (p->len + s->bulk_out_pos > BULK_OUT_DATA_SIZE) {
 
854
    if (p->iov.size + s->bulk_out_pos > BULK_OUT_DATA_SIZE) {
938
855
        return USB_RET_STALL;
939
856
    }
940
857
    ccid_header = (CCID_Header *)s->bulk_out_data;
941
 
    memcpy(s->bulk_out_data + s->bulk_out_pos, p->data, p->len);
942
 
    s->bulk_out_pos += p->len;
943
 
    if (p->len == CCID_MAX_PACKET_SIZE) {
 
858
    usb_packet_copy(p, s->bulk_out_data + s->bulk_out_pos, p->iov.size);
 
859
    s->bulk_out_pos += p->iov.size;
 
860
    if (p->iov.size == CCID_MAX_PACKET_SIZE) {
944
861
        DPRINTF(s, D_VERBOSE,
945
 
            "usb-ccid: bulk_in: expecting more packets (%d/%d)\n",
946
 
            p->len, ccid_header->dwLength);
 
862
            "usb-ccid: bulk_in: expecting more packets (%zd/%d)\n",
 
863
            p->iov.size, ccid_header->dwLength);
947
864
        return 0;
948
865
    }
949
866
    if (s->bulk_out_pos < 10) {
1006
923
    return 0;
1007
924
}
1008
925
 
1009
 
static int ccid_bulk_in_copy_to_guest(USBCCIDState *s, uint8_t *data, int len)
 
926
static int ccid_bulk_in_copy_to_guest(USBCCIDState *s, USBPacket *p)
1010
927
{
1011
928
    int ret = 0;
1012
929
 
1013
 
    assert(len > 0);
 
930
    assert(p->iov.size > 0);
1014
931
    ccid_bulk_in_get(s);
1015
932
    if (s->current_bulk_in != NULL) {
1016
 
        ret = MIN(s->current_bulk_in->len - s->current_bulk_in->pos, len);
1017
 
        memcpy(data, s->current_bulk_in->data + s->current_bulk_in->pos, ret);
 
933
        ret = MIN(s->current_bulk_in->len - s->current_bulk_in->pos,
 
934
                  p->iov.size);
 
935
        usb_packet_copy(p, s->current_bulk_in->data +
 
936
                        s->current_bulk_in->pos, ret);
1018
937
        s->current_bulk_in->pos += ret;
1019
938
        if (s->current_bulk_in->pos == s->current_bulk_in->len) {
1020
939
            ccid_bulk_in_release(s);
1025
944
    }
1026
945
    if (ret > 0) {
1027
946
        DPRINTF(s, D_MORE_INFO,
1028
 
                "%s: %d/%d req/act to guest (BULK_IN)\n", __func__, len, ret);
 
947
                "%s: %zd/%d req/act to guest (BULK_IN)\n",
 
948
                __func__, p->iov.size, ret);
1029
949
    }
1030
 
    if (ret != USB_RET_NAK && ret < len) {
 
950
    if (ret != USB_RET_NAK && ret < p->iov.size) {
1031
951
        DPRINTF(s, 1,
1032
 
            "%s: returning short (EREMOTEIO) %d < %d\n", __func__, ret, len);
 
952
                "%s: returning short (EREMOTEIO) %d < %zd\n",
 
953
                __func__, ret, p->iov.size);
1033
954
    }
1034
955
    return ret;
1035
956
}
1038
959
{
1039
960
    USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
1040
961
    int ret = 0;
1041
 
    uint8_t *data = p->data;
1042
 
    int len = p->len;
 
962
    uint8_t buf[2];
1043
963
 
1044
964
    switch (p->pid) {
1045
965
    case USB_TOKEN_OUT:
1049
969
    case USB_TOKEN_IN:
1050
970
        switch (p->devep & 0xf) {
1051
971
        case CCID_BULK_IN_EP:
1052
 
            if (!len) {
 
972
            if (!p->iov.size) {
1053
973
                ret = USB_RET_NAK;
1054
974
            } else {
1055
 
                ret = ccid_bulk_in_copy_to_guest(s, data, len);
 
975
                ret = ccid_bulk_in_copy_to_guest(s, p);
1056
976
            }
1057
977
            break;
1058
978
        case CCID_INT_IN_EP:
1059
979
            if (s->notify_slot_change) {
1060
980
                /* page 56, RDR_to_PC_NotifySlotChange */
1061
 
                data[0] = CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange;
1062
 
                data[1] = s->bmSlotICCState;
 
981
                buf[0] = CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange;
 
982
                buf[1] = s->bmSlotICCState;
 
983
                usb_packet_copy(p, buf, 2);
1063
984
                ret = 2;
1064
985
                s->notify_slot_change = false;
1065
986
                s->bmSlotICCState &= ~SLOT_0_CHANGED_MASK;
1066
987
                DPRINTF(s, D_INFO,
1067
988
                        "handle_data: int_in: notify_slot_change %X, "
1068
 
                        "requested len %d\n",
1069
 
                        s->bmSlotICCState, len);
 
989
                        "requested len %zd\n",
 
990
                        s->bmSlotICCState, p->iov.size);
1070
991
            }
1071
992
            break;
1072
993
        default:
1073
994
            DPRINTF(s, 1, "Bad endpoint\n");
 
995
            ret = USB_RET_STALL;
1074
996
            break;
1075
997
        }
1076
998
        break;
1252
1174
{
1253
1175
    USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
1254
1176
 
 
1177
    usb_desc_init(dev);
1255
1178
    qbus_create_inplace(&s->bus.qbus, &ccid_bus_info, &dev->qdev, NULL);
1256
1179
    s->bus.qbus.allow_hotplug = 1;
1257
1180
    s->card = NULL;
1377
1300
    .qdev.desc      = "CCID Rev 1.1 smartcard reader",
1378
1301
    .qdev.size      = sizeof(USBCCIDState),
1379
1302
    .init           = ccid_initfn,
 
1303
    .usb_desc       = &desc_ccid,
1380
1304
    .handle_packet  = usb_generic_handle_packet,
1381
1305
    .handle_reset   = ccid_handle_reset,
1382
1306
    .handle_control = ccid_handle_control,