qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 1/6] usb-ccid: add CCID bus


From: Alon Levy
Subject: Re: [Qemu-devel] [PATCH 1/6] usb-ccid: add CCID bus
Date: Sun, 28 Nov 2010 11:58:06 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

On Fri, Nov 26, 2010 at 07:05:02PM +0000, Blue Swirl wrote:
> On Thu, Nov 25, 2010 at 4:22 PM, Alon Levy <address@hidden> wrote:
...
> > +/* 6.2.6 RDR_to_PC_SlotStatus definitions */
> > +enum {
> > +    CLOCK_STATUS_RUNNING = 0,
> > +    /* 0 - Clock Running, 1 - Clock stopped in State L, 2 - H,
> > +       3 - unkonwn state. rest are RFU
> > +     */
> > +};
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> 
> Please don't use anonymous structs.
> 

ok, fixing for v8.

> > +    uint8_t     bMessageType;
> 
> Why aHungarian nNotation? It's not QEMU style.
> 

These names are taken directly from the spec 
(http://www.usb.org/developers/devclass_docs/DWG_Smart-Card_CCID_Rev110.pdf). I 
did it to make searching in the spec easier for anyone reading the source.

> > +    uint32_t    dwLength;
> > +    uint8_t     bSlot;
> > +    uint8_t     bSeq;
> > +} CCID_Header;
> 
> Packed structure with unaligned fields. Will this work?
> 

Works for x86, same situation for the endianess issue.  I'll change it to 
unpacked and change the writing to the guest to explicitly copy individual 
structs (I'll have to do that for endianess anyway). Should the USB device be 
little endian always? so I need to convert any field written to the guest to 
little endian, and read from the guest to host endianess?

> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_Header hdr;
> > +    uint8_t     bStatus;        /* Only used in BULK_IN */
> > +    uint8_t     bError;         /* Only used in BULK_IN */
> > +} CCID_BULK_IN;
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_BULK_IN b;
> > +    uint8_t     bClockStatus;
> > +} CCID_SlotStatus;
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_BULK_IN b;
> > +    uint8_t     bProtocolNum;
> > +    uint8_t     abProtocolDataStructure[0];
> > +} CCID_Parameter;
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_BULK_IN b;
> > +    uint8_t      bChainParameter;
> > +    uint8_t      abData[0];
> > +} CCID_DataBlock;
> > +
> > +/* 6.1.4 PC_to_RDR_XfrBlock */
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_Header  hdr;
> > +    uint8_t      bBWI; /* Block Waiting Timeout */
> > +    uint16_t     wLevelParameter;
> > +    uint8_t      abData[0];
> > +} CCID_XferBlock;
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_Header hdr;
> > +    uint8_t     bPowerSelect;
> > +    uint16_t    abRFU;
> > +} CCID_IccPowerOn;
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_Header hdr;
> > +    uint16_t    abRFU;
> > +} CCID_IccPowerOff;
> > +
> > +typedef struct __attribute__ ((__packed__)) {
> > +    CCID_Header hdr;
> > +    uint8_t     bProtocolNum;
> > +    uint8_t    abProtocolDataStructure[0];
> > +} CCID_SetParameter;
> > +
> > +typedef struct {
> > +    uint8_t     bMessageType; /* 
> > CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange */
> > +    uint8_t     bmSlotICCState;
> > +} CCID_Notify_Slot_Change;
> > +
> > +/* used for DataBlock response to XferBlock */
> > +typedef struct answer_t {
> > +    uint8_t slot;
> > +    uint8_t seq;
> > +} answer_t;
> > +
> 
> answer_t is not in line with CODING_STYLE.
> 
> > +/* pending BULK_IN messages */
> > +typedef struct bulk_in_t {
> > +    uint8_t  data[BULK_IN_BUF_SIZE];
> > +    uint32_t len;
> > +    uint32_t pos;
> > +} bulk_in_t;
> 
> Neither is this.
> 

Fixing both for v8.

> > +
> > +enum {
> > +    MIGRATION_NONE,
> > +    MIGRATION_MIGRATED,
> > +};
> > +
> > +typedef struct CCIDBus CCIDBus;
> > +typedef struct USBCCIDState USBCCIDState;
> > +
> > +#define MAX_PROTOCOL_SIZE   7
> > +
> > +/**
> > + * powered - defaults to true, changed by PowerOn/PowerOff messages
> > + */
> > +struct USBCCIDState {
> > +    USBDevice dev;
> > +    CCIDBus *bus;
> > +    CCIDCardState *card;
> > +    CCIDCardInfo *cardinfo; /* caching the info pointer */
> > +    uint8_t  debug;
> > +    uint8_t  auto_attach;
> > +    bulk_in_t bulk_in_pending[BULK_IN_PENDING_NUM]; /* circular */
> > +    uint32_t bulk_in_pending_start;
> > +    uint32_t bulk_in_pending_end; /* first free */
> > +    uint32_t bulk_in_pending_num;
> > +    bulk_in_t *current_bulk_in;
> > +    uint8_t  bulk_out_data[BULK_OUT_DATA_SIZE];
> > +    uint32_t bulk_out_pos;
> > +    uint8_t  bmSlotICCState;
> > +    uint8_t  powered;
> > +    uint8_t  notify_slot_change;
> > +    uint64_t last_answer_error;
> > +    answer_t pending_answers[PENDING_ANSWERS_NUM];
> > +    uint32_t pending_answers_start;
> > +    uint32_t pending_answers_end;
> > +    uint32_t pending_answers_num;
> > +    uint8_t  bError;
> > +    uint8_t  bmCommandStatus;
> > +    uint8_t  bProtocolNum;
> > +    uint8_t  abProtocolDataStructure[MAX_PROTOCOL_SIZE];
> > +    uint32_t ulProtocolDataStructureSize;
> > +    uint8_t  attached_vmstate;
> > +    uint32_t state_vmstate;
> > +    uint8_t  migration_state;
> > +    uint32_t migration_target_ip;
> > +    uint16_t migration_target_port;
> > +};
> > +
> > +/* Slot specific variables. We emulate a single slot card reader.
> > + */
> > +
> > +
> > +/* CCID Spec chapter 4: CCID uses a standard device descriptor per Chapter 
> > 9,
> > + * "USB Device Framework", section 9.6.1, in the Universal Serial Bus
> > + * Specification.
> > + *
> > + * This device implemented based on the spec and with an Athena Smart Card
> > + * Reader as reference:
> > + *   0dc3:1004 Athena Smartcard Solutions, Inc.
> > + */
> > +
> > +static const uint8_t qemu_ccid_dev_descriptor[] = {
> > +        0x12,       /*  u8 bLength; */
> > +        USB_DT_DEVICE, /*  u8 bDescriptorType; Device */
> > +        0x10, 0x01, /*  u16 bcdUSB; v1.1 */
> > +
> > +        0x00,       /*  u8  bDeviceClass; */
> > +        0x00,       /*  u8  bDeviceSubClass; */
> > +        0x00,       /*  u8  bDeviceProtocol; [ low/full speeds only ] */
> > +        0x40,       /*  u8  bMaxPacketSize0; 8 Bytes (valid: 8,16,32,64) */
> > +
> > +        /* Vendor and product id are arbitrary.  */
> > +                    /*  u16 idVendor  */
> > +        CCID_VENDOR_ID & 0xff, CCID_VENDOR_ID >> 8,
> > +                    /*  u16 idProduct */
> > +        CCID_PRODUCT_ID & 0xff, CCID_PRODUCT_ID >> 8,
> > +                    /*  u16 bcdDevice */
> > +        CCID_DEVICE_VERSION & 0xff, CCID_DEVICE_VERSION >> 8,
> > +        0x01,       /*  u8  iManufacturer; */
> > +        0x02,       /*  u8  iProduct; */
> > +        0x03,       /*  u8  iSerialNumber; */
> > +        0x01,       /*  u8  bNumConfigurations; */
> > +};
> > +
> > +static const uint8_t qemu_ccid_config_descriptor[] = {
> > +
> > +        /* one configuration */
> > +        0x09,       /*  u8  bLength; */
> > +        USB_DT_CONFIG, /*  u8  bDescriptorType; Configuration */
> > +        0x5d, 0x00, /*  u16 wTotalLength; 9+9+54+7+7+7 */
> > +        0x01,       /*  u8  bNumInterfaces; (1) */
> > +        0x01,       /*  u8  bConfigurationValue; */
> > +        0x00,       /*  u8  iConfiguration; */
> > +        0xe0,       /*  u8  bmAttributes;
> > +                                 Bit 7: must be set,
> > +                                     6: Self-powered,
> > +                                     5: Remote wakeup,
> > +                                     4..0: resvd */
> > +        100/2,      /*  u8  MaxPower; 50 == 100mA */
> > +
> > +        /* one interface */
> > +        0x09,       /*  u8  if_bLength; */
> > +        USB_DT_INTERFACE, /*  u8  if_bDescriptorType; Interface */
> > +        0x00,       /*  u8  if_bInterfaceNumber; */
> > +        0x00,       /*  u8  if_bAlternateSetting; */
> > +        0x03,       /*  u8  if_bNumEndpoints; */
> > +        0x0b,       /*  u8  if_bInterfaceClass; Smart Card Device Class */
> > +        0x00,       /*  u8  if_bInterfaceSubClass; Subclass code */
> > +        0x00,       /*  u8  if_bInterfaceProtocol; Protocol code */
> > +        0x04,       /*  u8  if_iInterface; Index of string descriptor */
> > +
> > +        /* Smart Card Device Class Descriptor */
> > +        0x36,       /*  u8  bLength; */
> > +        0x21,       /*  u8  bDescriptorType; Functional */
> > +        0x10, 0x01, /*  u16 bcdCCID; CCID Specification Release Number. */
> > +        0x00,       /*  u8  bMaxSlotIndex; The index of the highest 
> > available
> > +                        slot on this device. All slots are consecutive 
> > starting
> > +                        at 00h. */
> > +        0x07,       /*  u8  bVoltageSupport; 01h - 5.0v, 02h - 3.0, 03 - 
> > 1.8 */
> > +
> > +        0x03, 0x00, /*  u32 dwProtocols; RRRR PPPP. RRRR = 0000h.*/
> > +        0x00, 0x00, /*  PPPP: 0001h = Protocol T=0, 0002h = Protocol T=1 */
> > +                    /*  u32 dwDefaultClock; in kHZ (0x0fa0 is 4 MHz) */
> > +        0xa0, 0x0f, 0x00, 0x00,
> > +                    /*  u32 dwMaximumClock; */
> > +        0x00, 0x00, 0x01, 0x00,
> > +        0x00,       /*  u8 bNumClockSupported; 0 means just the default 
> > and max. */
> > +                    /*  u32 dwDataRate ;bps. 9600 == 00002580h */
> > +        0x80, 0x25, 0x00, 0x00,
> > +                    /*  u32 dwMaxDataRate ; 11520 bps == 0001C200h */
> > +        0x00, 0xC2, 0x01, 0x00,
> > +        0x00,       /*  u8  bNumDataRatesSupported; 00 means all rates 
> > between
> > +                     *      default and max */
> > +                    /*  u32 dwMaxIFSD; maximum IFSD supported by CCID for 
> > protocol
> > +                     *      T=1 (Maximum seen from various cards) */
> > +        0xfe, 0x00, 0x00, 0x00,
> > +                    /*  u32 dwSyncProtocols; 1 - 2-wire, 2 - 3-wire, 4 - 
> > I2C */
> > +        0x00, 0x00, 0x00, 0x00,
> > +                    /*  u32 dwMechanical;  0 - no special characteristics. 
> > */
> > +        0x00, 0x00, 0x00, 0x00,
> > +                    /*  u32 dwFeatures;
> > +                     *  0 - No special characteristics
> > +                     *  + 2 Automatic parameter configuration based on ATR 
> > data
> > +                     *  + 4 Automatic activation of ICC on inserting
> > +                     *  + 8 Automatic ICC voltage selection
> > +                     *  + 10 Automatic ICC clock frequency change
> > +                     *  + 20 Automatic baud rate change
> > +                     *  + 40 Automatic parameters negotiation made by the 
> > CCID
> > +                     *  + 80 automatic PPS made by the CCID
> > +                     *  100 CCID can set ICC in clock stop mode
> > +                     *  200 NAD value other then 00 accepted (T=1 protocol)
> > +                     *  + 400 Automatic IFSD exchange as first exchange 
> > (T=1)
> > +                     *  One of the following only:
> > +                     *  + 10000 TPDU level exchanges with CCID
> > +                     *  20000 Short APDU level exchange with CCID
> > +                     *  40000 Short and Extended APDU level exchange with 
> > CCID
> > +                     *
> > +                     *  + 100000 USB Wake up signaling supported on card 
> > insertion
> > +                     *  and removal. Must set bit 5 in bmAttributes in 
> > Configuration
> > +                     *  descriptor if 100000 is set.*/
> > +        0xfe, 0x04, 0x11, 0x00,
> > +                    /*  u32 dwMaxCCIDMessageLength; For extended APDU in 
> > [261 + 10
> > +                     *  , 65544 + 10]. Otherwise the minimum is 
> > wMaxPacketSize of
> > +                     *  the Bulk-OUT endpoint */
> > +        0x12, 0x00, 0x01, 0x00,
> > +        0xFF,       /*  u8  bClassGetResponse; Significant only for CCID 
> > that
> > +                     *  offers an APDU level for exchanges. Indicates the 
> > default
> > +                     *  class value used by the CCID when it sends a Get 
> > Response
> > +                     *  command to perform the transportation of an APDU 
> > by T=0
> > +                     *  protocol
> > +                     *  FFh indicates that the CCID echos the class of the 
> > APDU.
> > +                     */
> > +        0xFF,       /*  u8  bClassEnvelope; EAPDU only. Envelope command 
> > for T=0 */
> > +        0x00, 0x00, /*  u16 wLcdLayout; XXYY Number of lines (XX) and 
> > chars per
> > +                     *  line for LCD display used for PIN entry. 0000 - no 
> > LCD */
> > +        0x01,       /*  u8  bPINSupport; 01h PIN Verification,
> > +                     *                   02h PIN Modification */
> > +        0x01,       /*  u8  bMaxCCIDBusySlots; */
> > +
> > +        /* Interrupt-IN endpoint */
> > +        0x07,       /*  u8  ep_bLength; */
> > +                    /*  u8  ep_bDescriptorType; Endpoint */
> > +        USB_DT_ENDPOINT,
> > +                    /*  u8  ep_bEndpointAddress; IN Endpoint 1 */
> > +        0x80 | CCID_INT_IN_EP,
> > +        0x03,       /*  u8  ep_bmAttributes; Interrupt */
> > +        0x40, 0x00, /*  u16 ep_wMaxPacketSize; */
> > +        0xff,       /*  u8  ep_bInterval; */
> > +
> > +        /* Bulk-In endpoint */
> > +        0x07,       /*  u8  ep_bLength; */
> > +                    /*  u8  ep_bDescriptorType; Endpoint */
> > +        USB_DT_ENDPOINT,
> > +                    /*  u8  ep_bEndpointAddress; IN Endpoint 2 */
> > +        0x80 | CCID_BULK_IN_EP,
> > +        0x02,       /*  u8  ep_bmAttributes; Bulk */
> > +        0x40, 0x00, /*  u16 ep_wMaxPacketSize; */
> > +        0x00,       /*  u8  ep_bInterval; */
> > +
> > +        /* Bulk-Out endpoint */
> > +        0x07,       /*  u8  ep_bLength; */
> > +                    /*  u8  ep_bDescriptorType; Endpoint */
> > +        USB_DT_ENDPOINT,
> > +                    /*  u8  ep_bEndpointAddress; OUT Endpoint 3 */
> > +        CCID_BULK_OUT_EP,
> > +        0x02,       /*  u8  ep_bmAttributes; Bulk */
> > +        0x40, 0x00, /*  u16 ep_wMaxPacketSize; */
> > +        0x00,       /*  u8  ep_bInterval; */
> > +
> > +};
> > +
> > +static bool ccid_has_pending_answers(USBCCIDState *s)
> > +{
> > +    return s->pending_answers_num > 0;
> > +}
> > +
> > +static void ccid_clear_pending_answers(USBCCIDState *s)
> > +{
> > +    s->pending_answers_num = 0;
> > +    s->pending_answers_start = 0;
> > +    s->pending_answers_end = 0;
> > +}
> > +
> > +static void ccid_print_pending_answers(USBCCIDState *s)
> > +{
> > +#ifdef DEBUG_CCID
> > +    answer_t *answer;
> > +    int i, count;
> > +
> > +    printf("usb-ccid: pending answers:");
> > +    if (!ccid_has_pending_answers(s)) {
> > +        printf(" empty\n");
> > +        return;
> > +    }
> > +    for (i = s->pending_answers_start, count=s->pending_answers_num ;
> > +         count > 0; count--, i++) {
> > +        answer = &s->pending_answers[i % PENDING_ANSWERS_NUM];
> > +        if (count == 1) {
> > +            printf("%d:%d\n", answer->slot, answer->seq);
> > +        } else {
> > +            printf("%d:%d,", answer->slot, answer->seq);
> > +        }
> > +    }
> > +#endif
> > +}
> > +
> > +static void ccid_add_pending_answer(USBCCIDState *s, CCID_Header *hdr)
> > +{
> > +    answer_t* answer;
> > +
> > +    assert(s->pending_answers_num++ < PENDING_ANSWERS_NUM);
> > +    answer = &s->pending_answers[(s->pending_answers_end++) % 
> > PENDING_ANSWERS_NUM];
> > +    answer->slot = hdr->bSlot;
> > +    answer->seq = hdr->bSeq;
> > +    ccid_print_pending_answers(s);
> > +}
> > +
> > +static void ccid_remove_pending_answer(USBCCIDState *s,
> > +    uint8_t *slot, uint8_t *seq)
> > +{
> > +    answer_t *answer;
> > +
> > +    assert(s->pending_answers_num-- > 0);
> > +    answer = &s->pending_answers[(s->pending_answers_start++) % 
> > PENDING_ANSWERS_NUM];
> > +    *slot = answer->slot;
> > +    *seq = answer->seq;
> > +    ccid_print_pending_answers(s);
> > +}
> > +
> > +static void ccid_bulk_in_clear(USBCCIDState *s)
> > +{
> > +    s->bulk_in_pending_start = 0;
> > +    s->bulk_in_pending_end = 0;
> > +    s->bulk_in_pending_num = 0;
> > +}
> > +
> > +static void ccid_bulk_in_release(USBCCIDState *s)
> > +{
> > +    assert(s->current_bulk_in != NULL);
> > +    s->current_bulk_in->pos = 0;
> > +    s->current_bulk_in = NULL;
> > +}
> > +
> > +static void ccid_bulk_in_get(USBCCIDState *s)
> > +{
> > +    if (s->current_bulk_in != NULL || s->bulk_in_pending_num == 0) {
> > +        return;
> > +    }
> > +    assert(s->bulk_in_pending_num > 0);
> > +    s->bulk_in_pending_num--;
> > +    s->current_bulk_in = &s->bulk_in_pending[
> > +        (s->bulk_in_pending_start++) % BULK_IN_PENDING_NUM];
> > +}
> > +
> > +static void* ccid_reserve_recv_buf(USBCCIDState* s, uint16_t len)
> > +{
> > +    bulk_in_t* bulk_in;
> > +
> > +    DPRINTF(s, 4, "%s: QUEUE: reserve %d bytes\n", __func__, len);
> > +
> > +    /* look for an existing element */
> > +    if (len > BULK_IN_BUF_SIZE) {
> > +        printf("usb-ccid.c: %s: len larger then max (%d>%d). discarding 
> > message.\n",
> > +            __func__, len, BULK_IN_BUF_SIZE);
> > +        return NULL;
> > +    }
> > +    if (s->bulk_in_pending_num >= BULK_IN_PENDING_NUM) {
> > +        printf("usb-ccid.c: %s: No free bulk_in buffers. discarding 
> > message.\n",
> > +                __func__);
> > +        return NULL;
> > +    }
> > +    bulk_in = &s->bulk_in_pending[(s->bulk_in_pending_end++) % 
> > BULK_IN_PENDING_NUM];
> > +    s->bulk_in_pending_num++;
> > +    bulk_in->len = len;
> > +    return bulk_in->data;
> > +}
> > +
> > +static void ccid_reset(USBCCIDState *s)
> > +{
> > +    ccid_bulk_in_clear(s);
> > +    ccid_clear_pending_answers(s);
> > +}
> > +
> > +static void ccid_detach(USBCCIDState *s)
> > +{
> > +    ccid_reset(s);
> > +    if (s->auto_attach == 0 && s->dev.attached) {
> > +        usb_device_detach(&s->dev);
> > +    }
> > +}
> > +
> > +static void ccid_handle_reset(USBDevice *dev)
> > +{
> > +    USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
> > +
> > +    DPRINTF(s, 1, "Reset\n");
> > +
> > +    ccid_reset(s);
> > +}
> > +
> > +static int ccid_handle_control(USBDevice *dev, int request, int value,
> > +                                  int index, int length, uint8_t *data)
> > +{
> > +    USBCCIDState *s = DO_UPCAST(USBCCIDState, dev, dev);
> > +    int ret = 0;
> > +
> > +    DPRINTF(s, 1, "got control %x, value %x\n",request, value);
> > +    switch (request) {
> > +    case DeviceRequest | USB_REQ_GET_STATUS:
> > +        data[0] = (0 << USB_DEVICE_SELF_POWERED) |
> > +            (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
> > +        data[1] = 0x00;
> > +        ret = 2;
> > +        break;
> > +    case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
> > +        if (value == USB_DEVICE_REMOTE_WAKEUP) {
> > +            dev->remote_wakeup = 0;
> > +        } else {
> > +            goto fail;
> > +        }
> > +        ret = 0;
> > +        break;
> > +    case DeviceOutRequest | USB_REQ_SET_FEATURE:
> > +        if (value == USB_DEVICE_REMOTE_WAKEUP) {
> > +            dev->remote_wakeup = 1;
> > +        } else {
> > +            goto fail;
> > +        }
> > +        ret = 0;
> > +        break;
> > +    case DeviceOutRequest | USB_REQ_SET_ADDRESS:
> > +        dev->addr = value;
> > +        ret = 0;
> > +        break;
> > +    case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
> > +        switch(value >> 8) {
> > +        case USB_DT_DEVICE:
> > +            memcpy(data, qemu_ccid_dev_descriptor,
> > +                   sizeof(qemu_ccid_dev_descriptor));
> > +            ret = sizeof(qemu_ccid_dev_descriptor);
> > +            break;
> > +        case USB_DT_CONFIG:
> > +            memcpy(data, qemu_ccid_config_descriptor,
> > +                   sizeof(qemu_ccid_config_descriptor));
> > +            ret = sizeof(qemu_ccid_config_descriptor);
> > +            break;
> > +        case USB_DT_STRING:
> > +            switch(value & 0xff) {
> > +            case 0:
> > +                /* language ids */
> > +                data[0] = 4;
> > +                data[1] = 3;
> > +                data[2] = 0x09;
> > +                data[3] = 0x04;
> > +                ret = 4;
> > +                break;
> > +            case 1:
> > +                /* vendor description */
> > +                ret = set_usb_string(data, CCID_VENDOR_DESCRIPTION);
> > +                break;
> > +            case 2:
> > +                /* product description */
> > +                ret = set_usb_string(data, CCID_PRODUCT_DESCRIPTION);
> > +                break;
> > +            case 3:
> > +                /* serial number */
> > +                ret = set_usb_string(data, CCID_SERIAL_NUMBER_STRING);
> > +                break;
> > +            case 4:
> > +                /* interface name */
> > +                ret = set_usb_string(data, CCID_INTERFACE_NAME);
> > +                break;
> > +            default:
> > +                goto fail;
> > +            }
> > +            break;
> > +        default:
> > +            goto fail;
> > +        }
> > +        break;
> > +    case DeviceRequest | USB_REQ_GET_CONFIGURATION:
> > +        data[0] = 1;
> > +        ret = 1;
> > +        break;
> > +    case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
> > +        /* Only one configuration - we just ignore the request */
> > +        ret = 0;
> > +        break;
> > +    case DeviceRequest | USB_REQ_GET_INTERFACE:
> > +        data[0] = 0;
> > +        ret = 1;
> > +        break;
> > +    case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
> > +        ret = 0;
> > +        break;
> > +    case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
> > +        ret = 0;
> > +        break;
> > +
> > +        /* Class specific requests.  */
> > +    case InterfaceOutClass | CCID_CONTROL_ABORT:
> > +        DPRINTF(s, 1, "ccid_control abort UNIMPLEMENTED\n");
> > +        ret = USB_RET_STALL;
> > +        break;
> > +    case InterfaceInClass | CCID_CONTROL_GET_CLOCK_FREQUENCIES:
> > +        DPRINTF(s, 1, "ccid_control get clock frequencies 
> > UNIMPLEMENTED\n");
> > +        ret = USB_RET_STALL;
> > +        break;
> > +    case InterfaceInClass | CCID_CONTROL_GET_DATA_RATES:
> > +        DPRINTF(s, 1, "ccid_control get data rates UNIMPLEMENTED\n");
> > +        ret = USB_RET_STALL;
> > +        break;
> > +    default:
> > +    fail:
> > +        DPRINTF(s, 1, "got unsupported/bogus control %x, value %x\n", 
> > request, value);
> > +        ret = USB_RET_STALL;
> > +        break;
> > +    }
> > +    return ret;
> > +}
> > +
> > +static bool ccid_card_inserted(USBCCIDState *s)
> > +{
> > +    return s->bmSlotICCState & SLOT_0_STATE_MASK;
> > +}
> > +
> > +static uint8_t ccid_card_status(USBCCIDState *s)
> > +{
> > +    return ccid_card_inserted(s)
> > +            ? (s->powered ?
> > +                ICC_STATUS_PRESENT_ACTIVE
> > +              : ICC_STATUS_PRESENT_INACTIVE
> > +              )
> > +            : ICC_STATUS_NOT_PRESENT;
> > +}
> > +
> > +static uint8_t ccid_calc_status(USBCCIDState *s)
> > +{
> > +    /* page 55, 6.2.6, calculation of bStatus from bmICCStatus and
> > +       bmCommandStatus
> > +     */
> > +    uint8_t ret = ccid_card_status(s) | (s->bmCommandStatus << 6);
> > +    DPRINTF(s, 4, "status = %d\n", ret);
> > +    return ret;
> > +}
> > +
> > +static void ccid_reset_error_status(USBCCIDState* s)
> > +{
> > +    s->bError = ERROR_CMD_NOT_SUPPORTED;
> > +    s->bmCommandStatus = COMMAND_STATUS_NO_ERROR;
> > +}
> > +
> > +static void ccid_write_slot_status(USBCCIDState* s, CCID_Header* recv)
> > +{
> > +    CCID_SlotStatus *h = ccid_reserve_recv_buf(s, sizeof(CCID_SlotStatus));
> > +    if (h == NULL) {
> > +        return;
> > +    }
> > +    h->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus;
> > +    h->b.hdr.dwLength = 0;
> > +    h->b.hdr.bSlot = recv->bSlot;
> > +    h->b.hdr.bSeq = recv->bSeq;
> > +    h->b.bStatus = ccid_calc_status(s);
> > +    h->b.bError = s->bError;
> > +    h->bClockStatus = CLOCK_STATUS_RUNNING;
> > +    ccid_reset_error_status(s);
> > +}
> > +
> > +static void ccid_write_parameters(USBCCIDState* s, CCID_Header* recv)
> > +{
> > +    CCID_Parameter *h;
> > +    uint32_t len = s->ulProtocolDataStructureSize;
> > +
> > +    h = ccid_reserve_recv_buf(s, sizeof(CCID_Parameter) + len);
> > +    if (h == NULL) {
> > +        return;
> > +    }
> > +    h->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_Parameters;
> > +    h->b.hdr.dwLength = 0;
> > +    h->b.hdr.bSlot = recv->bSlot;
> > +    h->b.hdr.bSeq = recv->bSeq;
> > +    h->b.bStatus = ccid_calc_status(s);
> > +    h->b.bError = s->bError;
> > +    h->bProtocolNum = s->bProtocolNum;
> > +    memcpy(h->abProtocolDataStructure, s->abProtocolDataStructure, len);
> > +    ccid_reset_error_status(s);
> > +}
> > +
> > +static void ccid_write_data_block(
> > +    USBCCIDState* s, uint8_t slot, uint8_t seq,
> > +    const uint8_t* data, uint32_t len)
> > +{
> > +    CCID_DataBlock *p = ccid_reserve_recv_buf(s, sizeof(*p) + len);
> > +
> > +    if (p == NULL) {
> > +        return;
> > +    }
> > +    p->b.hdr.bMessageType = CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock;
> > +    p->b.hdr.dwLength = len;
> 
> This assumes that host endianess happens to match what USB uses.
Answered (with a question) above.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]