Received: from [140.186.70.92] (port=44728 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PMetV-0004HV-Ts for qemu-devel@nongnu.org; Sun, 28 Nov 2010 05:54:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PMetU-0000ee-5e for qemu-devel@nongnu.org; Sun, 28 Nov 2010 05:54:09 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55929) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PMetT-0000eX-Bw for qemu-devel@nongnu.org; Sun, 28 Nov 2010 05:54:08 -0500 Date: Sun, 28 Nov 2010 12:53:59 +0200 From: Alon Levy Subject: Re: [Qemu-devel] [PATCH 1/6] usb-ccid: add CCID bus Message-ID: <20101128105359.GD6599@playa.tlv.redhat.com> References: <1290702170-3775-1-git-send-email-alevy@redhat.com> <1290702170-3775-2-git-send-email-alevy@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Blue Swirl Cc: qemu-devel@nongnu.org On Fri, Nov 26, 2010 at 07:05:02PM +0000, Blue Swirl wrote: > On Thu, Nov 25, 2010 at 4:22 PM, Alon Levy wrote: > > A CCID device is a smart card reader. It is a USB device, defined at = [1]. > > This patch introduces the usb-ccid device that is a ccid bus. Next pa= tches will > > introduce two card types to use it, a passthru card and an emulated c= ard. > > > > =A0[1] http://www.usb.org/developers/devclass_docs/DWG_Smart-Card_CCI= D_Rev110. > > > > Signed-off-by: Alon Levy > > --- > > =A0Makefile.objs | =A0 =A01 + > > =A0configure =A0 =A0 | =A0 12 + > > =A0hw/ccid.h =A0 =A0 | =A0 34 ++ > > =A0hw/usb-ccid.c | 1342 +++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++ > > =A04 files changed, 1389 insertions(+), 0 deletions(-) > > =A0create mode 100644 hw/ccid.h > > =A0create mode 100644 hw/usb-ccid.c > > > > diff --git a/Makefile.objs b/Makefile.objs > > index 23b17ce..713131f 100644 > > --- a/Makefile.objs > > +++ b/Makefile.objs > > @@ -183,6 +183,7 @@ hw-obj-$(CONFIG_FDC) +=3D fdc.o > > =A0hw-obj-$(CONFIG_ACPI) +=3D acpi.o acpi_piix4.o > > =A0hw-obj-$(CONFIG_APM) +=3D pm_smbus.o apm.o > > =A0hw-obj-$(CONFIG_DMA) +=3D dma.o > > +hw-obj-$(CONFIG_SMARTCARD) +=3D usb-ccid.o > > > > =A0# PPC devices > > =A0hw-obj-$(CONFIG_OPENPIC) +=3D openpic.o > > diff --git a/configure b/configure > > index 2917874..fb9eac2 100755 > > --- a/configure > > +++ b/configure > > @@ -332,6 +332,7 @@ zero_malloc=3D"" > > =A0trace_backend=3D"nop" > > =A0trace_file=3D"trace" > > =A0spice=3D"" > > +smartcard=3D"no" > > > > =A0# OS specific > > =A0if check_define __linux__ ; then > > @@ -739,6 +740,10 @@ for opt do > > =A0 ;; > > =A0 --enable-vhost-net) vhost_net=3D"yes" > > =A0 ;; > > + =A0--disable-smartcard) smartcard=3D"no" > > + =A0;; > > + =A0--enable-smartcard) smartcard=3D"yes" > > + =A0;; > > =A0 --*dir) > > =A0 ;; > > =A0 *) echo "ERROR: unknown option $opt"; show_help=3D"yes" > > @@ -934,6 +939,8 @@ echo " =A0--trace-file=3DNAME =A0 =A0 =A0 =A0Full= PATH,NAME of file to store traces" > > =A0echo " =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Default= :trace-" > > =A0echo " =A0--disable-spice =A0 =A0 =A0 =A0 =A0disable spice" > > =A0echo " =A0--enable-spice =A0 =A0 =A0 =A0 =A0 enable spice" > > +echo " =A0--disable-smartcard =A0 =A0 =A0disable smartcard support" > > +echo " =A0--enable-smartcard =A0 =A0 =A0 enable smartcard support" > > =A0echo "" > > =A0echo "NOTE: The object files are built at the place where configur= e is launched" > > =A0exit 1 > > @@ -2354,6 +2361,7 @@ echo "vhost-net support $vhost_net" > > =A0echo "Trace backend =A0 =A0 $trace_backend" > > =A0echo "Trace output file $trace_file-" > > =A0echo "spice support =A0 =A0 $spice" > > +echo "smartcard support $smartcard" > > > > =A0if test $sdl_too_old =3D "yes"; then > > =A0echo "-> Your SDL version is too old - please upgrade to have SDL = support" > > @@ -2617,6 +2625,10 @@ if test "$spice" =3D "yes" ; then > > =A0 echo "CONFIG_SPICE=3Dy" >> $config_host_mak > > =A0fi > > > > +if test "$smartcard" =3D "yes" ; then > > + =A0echo "CONFIG_SMARTCARD=3Dy" >> $config_host_mak > > +fi > > + > > =A0# XXX: suppress that > > =A0if [ "$bsd" =3D "yes" ] ; then > > =A0 echo "CONFIG_BSD=3Dy" >> $config_host_mak > > diff --git a/hw/ccid.h b/hw/ccid.h > > new file mode 100644 > > index 0000000..a38f971 > > --- /dev/null > > +++ b/hw/ccid.h > > @@ -0,0 +1,34 @@ > > +#ifndef __CCID_H__ > > +#define __CCID_H__ > > + > > +#include "qdev.h" > > + > > +typedef struct CCIDCardState CCIDCardState; > > +typedef struct CCIDCardInfo CCIDCardInfo; > > + > > +struct CCIDCardState { > > + =A0 =A0DeviceState qdev; > > +}; > > + > > +struct CCIDCardInfo { > > + =A0 =A0DeviceInfo qdev; > > + =A0 =A0void (*print)(Monitor *mon, CCIDCardState *card, int indent)= ; > > + =A0 =A0const uint8_t *(*get_atr)(CCIDCardState *card, uint32_t *len= ); > > + =A0 =A0void (*apdu_from_guest)(CCIDCardState *card, const uint8_t *= apdu, uint32_t len); > > + =A0 =A0int (*exitfn)(CCIDCardState *card); > > + =A0 =A0int (*initfn)(CCIDCardState *card); > > +}; > > + > > +void ccid_card_send_apdu_to_guest(CCIDCardState *card, uint8_t* apdu= , uint32_t len); > > +void ccid_card_card_removed(CCIDCardState *card); > > +void ccid_card_card_inserted(CCIDCardState *card); > > +void ccid_card_card_error(CCIDCardState *card, uint64_t error); > > +void ccid_card_qdev_register(CCIDCardInfo *card); > > + > > +/* support guest visible insertion/removal of ccid devices based on = actual > > + * devices connected/removed. Called by card implementation (passthr= u, local) */ > > +int ccid_card_ccid_attach(CCIDCardState *card); > > +void ccid_card_ccid_detach(CCIDCardState *card); > > + > > +#endif // __CCID_H__ > > + > > diff --git a/hw/usb-ccid.c b/hw/usb-ccid.c > > new file mode 100644 > > index 0000000..66c1dba > > --- /dev/null > > +++ b/hw/usb-ccid.c > > @@ -0,0 +1,1342 @@ > > +/* > > + * CCID Device emulation > > + * > > + * Based on usb-serial.c: > > + * Copyright (c) 2006 CodeSourcery. > > + * Copyright (c) 2008 Samuel Thibault > > + * Written by Paul Brook, reused for FTDI by Samuel Thibault, > > + * Reused for CCID by Alon Levy. > > + * Contributed to by Robert Relyea > > + * Copyright (c) 2010 Red Hat. > > + * > > + * This code is licenced under the LGPL. > > + */ > > + > > +/* References: > > + * > > + * CCID Specification Revision 1.1 April 22nd 2005 > > + * =A0"Universal Serial Bus, Device Class: Smart Card" > > + * =A0Specification for Integrated Circuit(s) Cards Interface Device= s > > + * > > + * KNOWN BUGS > > + * 1. remove/insert can sometimes result in removed state instead of= inserted. > > + * This is a result of the following: > > + * =A0symptom: dmesg shows ERMOTEIO (-121), pcscd shows -99. This ha= ppens > > + * =A0when we send a too short packet, seen in uhci-usb.c, resulting= from > > + * =A0a urb requesting SPD and us returning a smaller packet. > > + * =A0Not sure which messages trigger this. > > + * > > + */ > > + > > +#include "qemu-common.h" > > +#include "qemu-error.h" > > +#include "usb.h" > > +#include "monitor.h" > > + > > +#include "hw/ccid.h" > > + > > +//#define DEBUG_CCID > > + > > +#define DPRINTF(s, lvl, fmt, ...) \ > > +do { if (lvl <=3D s->debug) { printf("usb-ccid: " fmt , ## __VA_ARGS= __); } } while (0) > > + > > +#define CCID_DEV_NAME "usb-ccid" > > + > > +/* The two options for variable sized buffers: > > + * make them constant size, for large enough constant, > > + * or handle the migration complexity - VMState doesn't handle this = case. > > + * sizes are expected never to be exceeded, unless guest misbehaves.= */ > > +#define BULK_OUT_DATA_SIZE 65536 > > +#define PENDING_ANSWERS_NUM 128 > > + > > +#define BULK_IN_BUF_SIZE 384 > > +#define BULK_IN_PENDING_NUM 8 > > + > > +#define InterfaceOutClass =A0 =A0((USB_DIR_OUT|USB_TYPE_CLASS|USB_RE= CIP_INTERFACE)<<8) > > +#define InterfaceInClass =A0 =A0 ((USB_DIR_IN |USB_TYPE_CLASS|USB_RE= CIP_INTERFACE)<<8) > > + > > +#define CCID_CONTROL_ABORT =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A00x1 > > +#define CCID_CONTROL_GET_CLOCK_FREQUENCIES =A00x2 > > +#define CCID_CONTROL_GET_DATA_RATES =A0 =A0 =A0 =A0 0x3 > > + > > +#define CCID_PRODUCT_DESCRIPTION =A0 =A0 =A0 =A0"QEMU USB CCID" > > +#define CCID_VENDOR_DESCRIPTION =A0 =A0 =A0 =A0 "QEMU " QEMU_VERSION > > +#define CCID_INTERFACE_NAME =A0 =A0 =A0 =A0 =A0 =A0 "CCID Interface" > > +#define CCID_SERIAL_NUMBER_STRING =A0 =A0 =A0 "1" > > +/* Using Gemplus Vendor and Product id > > + =A0Effect on various drivers: > > + =A0* usbccid.sys (winxp, others untested) is a class driver so it d= oesn't care. > > + =A0* linux has a number of class drivers, but openct filters based = on > > + =A0 =A0vendor/product (/etc/openct.conf under fedora), hence Gemplu= s. > > + */ > > +#define CCID_VENDOR_ID =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A00x08e6 > > +#define CCID_PRODUCT_ID =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 0x4433 > > +#define CCID_DEVICE_VERSION =A0 =A0 =A0 =A0 =A0 =A0 0x0000 > > + > > +/* BULK_OUT messages from PC to Reader > > + =A0 Defined in CCID Rev 1.1 6.1 (page 26) > > + */ > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn =A0 =A0 =A0 =A0 =A0 =A0= =A00x62 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff =A0 =A0 =A0 =A0 =A0 = =A0 0x63 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus =A0 =A0 =A0 =A0 =A0= 0x65 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A00x6f > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters =A0 =A0 =A0 =A0 =A0= 0x6c > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters =A0 =A0 =A0 =A0 = 0x6d > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters =A0 =A0 =A0 =A0 =A0= 0x61 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_Escape =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A00x6b > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_IccClock =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A00x6e > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A00x6a > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_Secure =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A00x69 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical =A0 =A0 =A0 =A0 =A0 =A0= =A00x71 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_Abort =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 0x72 > > +#define CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency 0x7= 3 > > + > > +/* BULK_IN messages from Reader to PC > > + =A0 Defined in CCID Rev 1.1 6.2 (page 48) > > + */ > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock =A0 =A0 =A0 =A0 =A0 =A0= =A0 0x80 > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus =A0 =A0 =A0 =A0 =A0 =A0= =A00x81 > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_Parameters =A0 =A0 =A0 =A0 =A0 =A0= =A00x82 > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_Escape =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A00x83 > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_DataRateAndClockFrequency 0x84 > > + > > +/* INTERRUPT_IN messages from Reader to PC > > + =A0 Defined in CCID Rev 1.1 6.3 (page 56) > > + */ > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange =A0 =A0 =A0 =A0= 0x50 > > +#define CCID_MESSAGE_TYPE_RDR_to_PC_HardwareError =A0 =A0 =A0 =A0 =A0= 0x51 > > + > > +/* Endpoints for CCID - addresses are up to us to decide. > > + =A0 To support slot insertion and removal we must have an interrupt= in ep > > + =A0 in addition we need a bulk in and bulk out ep > > + =A0 5.2, page 20 > > + */ > > +#define CCID_INT_IN_EP =A0 =A0 =A0 1 > > +#define CCID_BULK_IN_EP =A0 =A0 =A02 > > +#define CCID_BULK_OUT_EP =A0 =A0 3 > > + > > +/* bmSlotICCState masks */ > > +#define SLOT_0_STATE_MASK =A0 =A01 > > +#define SLOT_0_CHANGED_MASK =A02 > > + > > +/* Status codes that go in bStatus (see 6.2.6) */ > > +enum { > > + =A0 =A0ICC_STATUS_PRESENT_ACTIVE =3D 0, > > + =A0 =A0ICC_STATUS_PRESENT_INACTIVE, > > + =A0 =A0ICC_STATUS_NOT_PRESENT > > +}; > > + > > +enum { > > + =A0 =A0COMMAND_STATUS_NO_ERROR =3D 0, > > + =A0 =A0COMMAND_STATUS_FAILED, > > + =A0 =A0COMMAND_STATUS_TIME_EXTENSION_REQUIRED > > +}; > > + > > +/* Error codes that go in bError (see 6.2.6) > > + */ > > +enum { > > + =A0 =A0ERROR_CMD_NOT_SUPPORTED =3D 0, > > + =A0 =A0ERROR_CMD_ABORTED =A0 =A0 =A0 =3D -1, > > + =A0 =A0ERROR_ICC_MUTE =A0 =A0 =A0 =A0 =A0=3D -2, > > + =A0 =A0ERROR_XFR_PARITY_ERROR =A0=3D -3, > > + =A0 =A0ERROR_XFR_OVERRUN =A0 =A0 =A0 =3D -4, > > + =A0 =A0ERROR_HW_ERROR =A0 =A0 =A0 =A0 =A0=3D -5, > > +}; > > + > > +/* 6.2.6 RDR_to_PC_SlotStatus definitions */ > > +enum { > > + =A0 =A0CLOCK_STATUS_RUNNING =3D 0, > > + =A0 =A0/* 0 - Clock Running, 1 - Clock stopped in State L, 2 - H, > > + =A0 =A0 =A0 3 - unkonwn state. rest are RFU > > + =A0 =A0 */ > > +}; > > + > > +typedef struct __attribute__ ((__packed__)) { >=20 > Please don't use anonymous structs. >=20 > > + =A0 =A0uint8_t =A0 =A0 bMessageType; >=20 > Why aHungarian nNotation? It's not QEMU style. >=20 > > + =A0 =A0uint32_t =A0 =A0dwLength; > > + =A0 =A0uint8_t =A0 =A0 bSlot; > > + =A0 =A0uint8_t =A0 =A0 bSeq; > > +} CCID_Header; >=20 > Packed structure with unaligned fields. Will this work? >=20 So by that you mean it could produce incorrect code on some cpu's? I'm as= king because adding endianess correcting code is much simpler then removi= ng the packed attribute, which requires computing the size of message in = a way other then sizeof (which will be bigger) and copying each field sep= arately (inducing a much larger packing/unpacking code, error prone of co= urse). I assumed gcc knows to generate aligned accesses where required, a= nd do bit twiddling to retrieve the specific field (even if it is 4 byte = word at a 1 byte offset from a 4 byte multiple).