2010-10-17 13:40:07 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* CCID Device emulation
|
|
|
|
*
|
|
|
|
* Written by Alon Levy, with contributions from Robert Relyea.
|
|
|
|
*
|
2012-02-26 21:46:12 +04:00
|
|
|
* Based on usb-serial.c, see its copyright and attributions below.
|
2010-10-17 13:40:07 +04:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2.1 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
* ------- (original copyright & attribution for usb-serial.c below) --------
|
|
|
|
* Copyright (c) 2006 CodeSourcery.
|
|
|
|
* Copyright (c) 2008 Samuel Thibault <samuel.thibault@ens-lyon.org>
|
|
|
|
* Written by Paul Brook, reused for FTDI by Samuel Thibault,
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
*
|
|
|
|
* CCID Specification Revision 1.1 April 22nd 2005
|
|
|
|
* "Universal Serial Bus, Device Class: Smart Card"
|
|
|
|
* Specification for Integrated Circuit(s) Cards Interface Devices
|
|
|
|
*
|
2011-04-05 01:54:04 +04:00
|
|
|
* Endianness note: from the spec (1.3)
|
2010-10-17 13:40:07 +04:00
|
|
|
* "Fields that are larger than a byte are stored in little endian"
|
|
|
|
*
|
|
|
|
* KNOWN BUGS
|
|
|
|
* 1. remove/insert can sometimes result in removed state instead of inserted.
|
|
|
|
* This is a result of the following:
|
|
|
|
* symptom: dmesg shows ERMOTEIO (-121), pcscd shows -99. This can happen
|
|
|
|
* when a short packet is sent, as seen in uhci-usb.c, resulting from a urb
|
|
|
|
* from the guest requesting SPD and us returning a smaller packet.
|
|
|
|
* Not sure which messages trigger this.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:12 +03:00
|
|
|
#include "qemu/osdep.h"
|
2018-06-25 15:42:27 +03:00
|
|
|
#include "qemu/units.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2010-10-17 13:40:07 +04:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/error-report.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2012-03-07 17:55:18 +04:00
|
|
|
#include "hw/usb.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2018-05-03 22:50:48 +03:00
|
|
|
#include "desc.h"
|
2010-10-17 13:40:07 +04:00
|
|
|
|
2013-03-18 20:36:02 +04:00
|
|
|
#include "ccid.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
#define DPRINTF(s, lvl, fmt, ...) \
|
|
|
|
do { \
|
|
|
|
if (lvl <= s->debug) { \
|
|
|
|
printf("usb-ccid: " fmt , ## __VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define D_WARN 1
|
|
|
|
#define D_INFO 2
|
|
|
|
#define D_MORE_INFO 3
|
|
|
|
#define D_VERBOSE 4
|
|
|
|
|
2020-09-03 01:42:14 +03:00
|
|
|
#define TYPE_USB_CCID_DEV "usb-ccid"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(USBCCIDState, USB_CCID_DEV)
|
2010-10-17 13:40:07 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-06-25 15:42:27 +03:00
|
|
|
#define BULK_OUT_DATA_SIZE (64 * KiB)
|
2010-10-17 13:40:07 +04:00
|
|
|
#define PENDING_ANSWERS_NUM 128
|
|
|
|
|
|
|
|
#define BULK_IN_BUF_SIZE 384
|
|
|
|
#define BULK_IN_PENDING_NUM 8
|
|
|
|
|
|
|
|
#define CCID_MAX_PACKET_SIZE 64
|
|
|
|
|
|
|
|
#define CCID_CONTROL_ABORT 0x1
|
|
|
|
#define CCID_CONTROL_GET_CLOCK_FREQUENCIES 0x2
|
|
|
|
#define CCID_CONTROL_GET_DATA_RATES 0x3
|
|
|
|
|
|
|
|
#define CCID_PRODUCT_DESCRIPTION "QEMU USB CCID"
|
2012-05-30 07:35:51 +04:00
|
|
|
#define CCID_VENDOR_DESCRIPTION "QEMU"
|
2010-10-17 13:40:07 +04:00
|
|
|
#define CCID_INTERFACE_NAME "CCID Interface"
|
|
|
|
#define CCID_SERIAL_NUMBER_STRING "1"
|
|
|
|
/*
|
|
|
|
* Using Gemplus Vendor and Product id
|
|
|
|
* Effect on various drivers:
|
|
|
|
* usbccid.sys (winxp, others untested) is a class driver so it doesn't care.
|
|
|
|
* linux has a number of class drivers, but openct filters based on
|
|
|
|
* vendor/product (/etc/openct.conf under fedora), hence Gemplus.
|
|
|
|
*/
|
|
|
|
#define CCID_VENDOR_ID 0x08e6
|
|
|
|
#define CCID_PRODUCT_ID 0x4433
|
|
|
|
#define CCID_DEVICE_VERSION 0x0000
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BULK_OUT messages from PC to Reader
|
|
|
|
* Defined in CCID Rev 1.1 6.1 (page 26)
|
|
|
|
*/
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn 0x62
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff 0x63
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus 0x65
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock 0x6f
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters 0x6c
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters 0x6d
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters 0x61
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_Escape 0x6b
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_IccClock 0x6e
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU 0x6a
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_Secure 0x69
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical 0x71
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_Abort 0x72
|
|
|
|
#define CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency 0x73
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BULK_IN messages from Reader to PC
|
|
|
|
* Defined in CCID Rev 1.1 6.2 (page 48)
|
|
|
|
*/
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock 0x80
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus 0x81
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_Parameters 0x82
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_Escape 0x83
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_DataRateAndClockFrequency 0x84
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INTERRUPT_IN messages from Reader to PC
|
|
|
|
* Defined in CCID Rev 1.1 6.3 (page 56)
|
|
|
|
*/
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange 0x50
|
|
|
|
#define CCID_MESSAGE_TYPE_RDR_to_PC_HardwareError 0x51
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Endpoints for CCID - addresses are up to us to decide.
|
|
|
|
* To support slot insertion and removal we must have an interrupt in ep
|
|
|
|
* in addition we need a bulk in and bulk out ep
|
|
|
|
* 5.2, page 20
|
|
|
|
*/
|
|
|
|
#define CCID_INT_IN_EP 1
|
|
|
|
#define CCID_BULK_IN_EP 2
|
|
|
|
#define CCID_BULK_OUT_EP 3
|
|
|
|
|
|
|
|
/* bmSlotICCState masks */
|
|
|
|
#define SLOT_0_STATE_MASK 1
|
|
|
|
#define SLOT_0_CHANGED_MASK 2
|
|
|
|
|
|
|
|
/* Status codes that go in bStatus (see 6.2.6) */
|
|
|
|
enum {
|
|
|
|
ICC_STATUS_PRESENT_ACTIVE = 0,
|
|
|
|
ICC_STATUS_PRESENT_INACTIVE,
|
|
|
|
ICC_STATUS_NOT_PRESENT
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
COMMAND_STATUS_NO_ERROR = 0,
|
|
|
|
COMMAND_STATUS_FAILED,
|
|
|
|
COMMAND_STATUS_TIME_EXTENSION_REQUIRED
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Error codes that go in bError (see 6.2.6) */
|
|
|
|
enum {
|
|
|
|
ERROR_CMD_NOT_SUPPORTED = 0,
|
|
|
|
ERROR_CMD_ABORTED = -1,
|
|
|
|
ERROR_ICC_MUTE = -2,
|
|
|
|
ERROR_XFR_PARITY_ERROR = -3,
|
|
|
|
ERROR_XFR_OVERRUN = -4,
|
|
|
|
ERROR_HW_ERROR = -5,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 6.2.6 RDR_to_PC_SlotStatus definitions */
|
|
|
|
enum {
|
|
|
|
CLOCK_STATUS_RUNNING = 0,
|
|
|
|
/*
|
|
|
|
* 0 - Clock Running, 1 - Clock stopped in State L, 2 - H,
|
2011-04-05 01:54:04 +04:00
|
|
|
* 3 - unknown state. rest are RFU
|
2010-10-17 13:40:07 +04:00
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_Header {
|
2010-10-17 13:40:07 +04:00
|
|
|
uint8_t bMessageType;
|
|
|
|
uint32_t dwLength;
|
|
|
|
uint8_t bSlot;
|
|
|
|
uint8_t bSeq;
|
|
|
|
} CCID_Header;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_BULK_IN {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_Header hdr;
|
|
|
|
uint8_t bStatus; /* Only used in BULK_IN */
|
|
|
|
uint8_t bError; /* Only used in BULK_IN */
|
|
|
|
} CCID_BULK_IN;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_SlotStatus {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_BULK_IN b;
|
|
|
|
uint8_t bClockStatus;
|
|
|
|
} CCID_SlotStatus;
|
|
|
|
|
2013-03-04 20:57:45 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_T0ProtocolDataStructure {
|
|
|
|
uint8_t bmFindexDindex;
|
|
|
|
uint8_t bmTCCKST0;
|
|
|
|
uint8_t bGuardTimeT0;
|
|
|
|
uint8_t bWaitingIntegerT0;
|
|
|
|
uint8_t bClockStop;
|
|
|
|
} CCID_T0ProtocolDataStructure;
|
|
|
|
|
|
|
|
typedef struct QEMU_PACKED CCID_T1ProtocolDataStructure {
|
|
|
|
uint8_t bmFindexDindex;
|
|
|
|
uint8_t bmTCCKST1;
|
|
|
|
uint8_t bGuardTimeT1;
|
|
|
|
uint8_t bWaitingIntegerT1;
|
|
|
|
uint8_t bClockStop;
|
|
|
|
uint8_t bIFSC;
|
|
|
|
uint8_t bNadValue;
|
|
|
|
} CCID_T1ProtocolDataStructure;
|
|
|
|
|
|
|
|
typedef union CCID_ProtocolDataStructure {
|
|
|
|
CCID_T0ProtocolDataStructure t0;
|
|
|
|
CCID_T1ProtocolDataStructure t1;
|
|
|
|
uint8_t data[7]; /* must be = max(sizeof(t0), sizeof(t1)) */
|
|
|
|
} CCID_ProtocolDataStructure;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_Parameter {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_BULK_IN b;
|
|
|
|
uint8_t bProtocolNum;
|
2013-03-04 20:57:45 +04:00
|
|
|
CCID_ProtocolDataStructure abProtocolDataStructure;
|
2010-10-17 13:40:07 +04:00
|
|
|
} CCID_Parameter;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_DataBlock {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_BULK_IN b;
|
|
|
|
uint8_t bChainParameter;
|
misc: Replace zero-length arrays with flexible array member (automatic)
Description copied from Linux kernel commit from Gustavo A. R. Silva
(see [3]):
--v-- description start --v--
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to
declare variable-length types such as these ones is a flexible
array member [1], introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler
warning in case the flexible array does not occur last in the
structure, which will help us prevent some kind of undefined
behavior bugs from being unadvertenly introduced [2] to the
Linux codebase from now on.
--^-- description end --^--
Do the similar housekeeping in the QEMU codebase (which uses
C99 since commit 7be41675f7cb).
All these instances of code were found with the help of the
following Coccinelle script:
@@
identifier s, m, a;
type t, T;
@@
struct s {
...
t m;
- T a[0];
+ T a[];
};
@@
identifier s, m, a;
type t, T;
@@
struct s {
...
t m;
- T a[0];
+ T a[];
} QEMU_PACKED;
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=76497732932f
[3] https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?id=17642a2fbd2c1
Inspired-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-03-04 18:38:15 +03:00
|
|
|
uint8_t abData[];
|
2010-10-17 13:40:07 +04:00
|
|
|
} CCID_DataBlock;
|
|
|
|
|
|
|
|
/* 6.1.4 PC_to_RDR_XfrBlock */
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_XferBlock {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_Header hdr;
|
|
|
|
uint8_t bBWI; /* Block Waiting Timeout */
|
|
|
|
uint16_t wLevelParameter; /* XXX currently unused */
|
misc: Replace zero-length arrays with flexible array member (automatic)
Description copied from Linux kernel commit from Gustavo A. R. Silva
(see [3]):
--v-- description start --v--
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to
declare variable-length types such as these ones is a flexible
array member [1], introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler
warning in case the flexible array does not occur last in the
structure, which will help us prevent some kind of undefined
behavior bugs from being unadvertenly introduced [2] to the
Linux codebase from now on.
--^-- description end --^--
Do the similar housekeeping in the QEMU codebase (which uses
C99 since commit 7be41675f7cb).
All these instances of code were found with the help of the
following Coccinelle script:
@@
identifier s, m, a;
type t, T;
@@
struct s {
...
t m;
- T a[0];
+ T a[];
};
@@
identifier s, m, a;
type t, T;
@@
struct s {
...
t m;
- T a[0];
+ T a[];
} QEMU_PACKED;
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=76497732932f
[3] https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?id=17642a2fbd2c1
Inspired-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-03-04 18:38:15 +03:00
|
|
|
uint8_t abData[];
|
2010-10-17 13:40:07 +04:00
|
|
|
} CCID_XferBlock;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_IccPowerOn {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_Header hdr;
|
|
|
|
uint8_t bPowerSelect;
|
|
|
|
uint16_t abRFU;
|
|
|
|
} CCID_IccPowerOn;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_IccPowerOff {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_Header hdr;
|
|
|
|
uint16_t abRFU;
|
|
|
|
} CCID_IccPowerOff;
|
|
|
|
|
2011-08-31 14:38:01 +04:00
|
|
|
typedef struct QEMU_PACKED CCID_SetParameters {
|
2010-10-17 13:40:07 +04:00
|
|
|
CCID_Header hdr;
|
|
|
|
uint8_t bProtocolNum;
|
|
|
|
uint16_t abRFU;
|
2013-03-04 20:57:45 +04:00
|
|
|
CCID_ProtocolDataStructure abProtocolDataStructure;
|
2010-10-17 13:40:07 +04:00
|
|
|
} CCID_SetParameters;
|
|
|
|
|
|
|
|
typedef struct CCID_Notify_Slot_Change {
|
|
|
|
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 {
|
|
|
|
uint8_t slot;
|
|
|
|
uint8_t seq;
|
|
|
|
} Answer;
|
|
|
|
|
|
|
|
/* pending BULK_IN messages */
|
|
|
|
typedef struct BulkIn {
|
|
|
|
uint8_t data[BULK_IN_BUF_SIZE];
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t pos;
|
|
|
|
} BulkIn;
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct CCIDBus {
|
2011-05-24 20:09:10 +04:00
|
|
|
BusState qbus;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
|
|
|
typedef struct CCIDBus CCIDBus;
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* powered - defaults to true, changed by PowerOn/PowerOff messages
|
|
|
|
*/
|
2020-09-03 23:43:22 +03:00
|
|
|
struct USBCCIDState {
|
2010-10-17 13:40:07 +04:00
|
|
|
USBDevice dev;
|
2012-01-17 16:25:46 +04:00
|
|
|
USBEndpoint *intr;
|
2015-07-16 17:33:07 +03:00
|
|
|
USBEndpoint *bulk;
|
2011-05-24 20:09:10 +04:00
|
|
|
CCIDBus bus;
|
2010-10-17 13:40:07 +04:00
|
|
|
CCIDCardState *card;
|
|
|
|
BulkIn 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;
|
|
|
|
BulkIn *current_bulk_in;
|
|
|
|
uint8_t bulk_out_data[BULK_OUT_DATA_SIZE];
|
|
|
|
uint32_t bulk_out_pos;
|
|
|
|
uint64_t last_answer_error;
|
|
|
|
Answer 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;
|
2013-03-04 20:57:45 +04:00
|
|
|
CCID_ProtocolDataStructure abProtocolDataStructure;
|
2010-10-17 13:40:07 +04:00
|
|
|
uint32_t ulProtocolDataStructureSize;
|
|
|
|
uint32_t state_vmstate;
|
|
|
|
uint8_t bmSlotICCState;
|
|
|
|
uint8_t powered;
|
|
|
|
uint8_t notify_slot_change;
|
|
|
|
uint8_t debug;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-08-30 13:49:05 +04:00
|
|
|
static const uint8_t qemu_ccid_descriptor[] = {
|
2010-10-17 13:40:07 +04:00
|
|
|
/* 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 */
|
|
|
|
|
2018-04-20 21:32:19 +03:00
|
|
|
0x01, 0x00, /* u32 dwProtocols; RRRR PPPP. RRRR = 0000h.*/
|
|
|
|
0x00, 0x00, /* PPPP: 0001h = Protocol T=0, 0002h = Protocol T=1 */
|
2010-10-17 13:40:07 +04:00
|
|
|
/* 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
|
|
|
|
*
|
2013-03-04 23:40:53 +04:00
|
|
|
* 100000 USB Wake up signaling supported on card
|
2010-10-17 13:40:07 +04:00
|
|
|
* insertion and removal. Must set bit 5 in bmAttributes
|
|
|
|
* in Configuration descriptor if 100000 is set.
|
|
|
|
*/
|
2013-03-04 23:40:53 +04:00
|
|
|
0xfe, 0x04, 0x01, 0x00,
|
2010-10-17 13:40:07 +04:00
|
|
|
/*
|
|
|
|
* 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; */
|
2011-08-30 13:49:05 +04:00
|
|
|
};
|
2010-10-17 13:40:07 +04:00
|
|
|
|
2011-08-30 13:49:05 +04:00
|
|
|
enum {
|
|
|
|
STR_MANUFACTURER = 1,
|
|
|
|
STR_PRODUCT,
|
|
|
|
STR_SERIALNUMBER,
|
|
|
|
STR_INTERFACE,
|
|
|
|
};
|
2010-10-17 13:40:07 +04:00
|
|
|
|
2011-08-30 13:49:05 +04:00
|
|
|
static const USBDescStrings desc_strings = {
|
2012-05-30 07:35:51 +04:00
|
|
|
[STR_MANUFACTURER] = "QEMU",
|
2011-08-30 13:49:05 +04:00
|
|
|
[STR_PRODUCT] = "QEMU USB CCID",
|
|
|
|
[STR_SERIALNUMBER] = "1",
|
|
|
|
[STR_INTERFACE] = "CCID Interface",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescIface desc_iface0 = {
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 3,
|
2013-03-05 17:31:26 +04:00
|
|
|
.bInterfaceClass = USB_CLASS_CSCID,
|
|
|
|
.bInterfaceSubClass = USB_SUBCLASS_UNDEFINED,
|
2011-08-30 13:49:05 +04:00
|
|
|
.bInterfaceProtocol = 0x00,
|
|
|
|
.iInterface = STR_INTERFACE,
|
|
|
|
.ndesc = 1,
|
|
|
|
.descs = (USBDescOther[]) {
|
|
|
|
{
|
|
|
|
/* smartcard descriptor */
|
|
|
|
.data = qemu_ccid_descriptor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
.eps = (USBDescEndpoint[]) {
|
|
|
|
{
|
|
|
|
.bEndpointAddress = USB_DIR_IN | CCID_INT_IN_EP,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
|
|
|
.bInterval = 255,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},{
|
|
|
|
.bEndpointAddress = USB_DIR_IN | CCID_BULK_IN_EP,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},{
|
|
|
|
.bEndpointAddress = USB_DIR_OUT | CCID_BULK_OUT_EP,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDescDevice desc_device = {
|
|
|
|
.bcdUSB = 0x0110,
|
|
|
|
.bMaxPacketSize0 = 64,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
.confs = (USBDescConfig[]) {
|
|
|
|
{
|
|
|
|
.bNumInterfaces = 1,
|
|
|
|
.bConfigurationValue = 1,
|
2013-12-16 11:42:49 +04:00
|
|
|
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
|
|
|
|
USB_CFG_ATT_WAKEUP,
|
2011-08-30 13:49:05 +04:00
|
|
|
.bMaxPower = 50,
|
|
|
|
.nif = 1,
|
|
|
|
.ifs = &desc_iface0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const USBDesc desc_ccid = {
|
|
|
|
.id = {
|
|
|
|
.idVendor = CCID_VENDOR_ID,
|
|
|
|
.idProduct = CCID_PRODUCT_ID,
|
|
|
|
.bcdDevice = CCID_DEVICE_VERSION,
|
|
|
|
.iManufacturer = STR_MANUFACTURER,
|
|
|
|
.iProduct = STR_PRODUCT,
|
|
|
|
.iSerialNumber = STR_SERIALNUMBER,
|
|
|
|
},
|
|
|
|
.full = &desc_device,
|
|
|
|
.str = desc_strings,
|
2010-10-17 13:40:07 +04:00
|
|
|
};
|
|
|
|
|
2011-12-04 22:34:10 +04:00
|
|
|
static const uint8_t *ccid_card_get_atr(CCIDCardState *card, uint32_t *len)
|
|
|
|
{
|
|
|
|
CCIDCardClass *cc = CCID_CARD_GET_CLASS(card);
|
2013-03-04 20:45:49 +04:00
|
|
|
|
2011-12-04 22:34:10 +04:00
|
|
|
if (cc->get_atr) {
|
|
|
|
return cc->get_atr(card, len);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_card_apdu_from_guest(CCIDCardState *card,
|
|
|
|
const uint8_t *apdu,
|
|
|
|
uint32_t len)
|
|
|
|
{
|
|
|
|
CCIDCardClass *cc = CCID_CARD_GET_CLASS(card);
|
2013-03-04 20:45:49 +04:00
|
|
|
|
2011-12-04 22:34:10 +04:00
|
|
|
if (cc->apdu_from_guest) {
|
|
|
|
cc->apdu_from_guest(card, apdu, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 13:40:07 +04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
Answer *answer;
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
DPRINTF(s, D_VERBOSE, "usb-ccid: pending answers:");
|
|
|
|
if (!ccid_has_pending_answers(s)) {
|
|
|
|
DPRINTF(s, D_VERBOSE, " 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) {
|
|
|
|
DPRINTF(s, D_VERBOSE, "%d:%d\n", answer->slot, answer->seq);
|
|
|
|
} else {
|
|
|
|
DPRINTF(s, D_VERBOSE, "%d:%d,", answer->slot, answer->seq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_add_pending_answer(USBCCIDState *s, CCID_Header *hdr)
|
|
|
|
{
|
|
|
|
Answer *answer;
|
|
|
|
|
|
|
|
assert(s->pending_answers_num < PENDING_ANSWERS_NUM);
|
|
|
|
s->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 *answer;
|
|
|
|
|
|
|
|
assert(s->pending_answers_num > 0);
|
|
|
|
s->pending_answers_num--;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
BulkIn *bulk_in;
|
|
|
|
|
|
|
|
DPRINTF(s, D_VERBOSE, "%s: QUEUE: reserve %d bytes\n", __func__, len);
|
|
|
|
|
|
|
|
/* look for an existing element */
|
|
|
|
if (len > BULK_IN_BUF_SIZE) {
|
|
|
|
DPRINTF(s, D_WARN, "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) {
|
|
|
|
DPRINTF(s, D_WARN, "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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_handle_reset(USBDevice *dev)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
DPRINTF(s, 1, "Reset\n");
|
|
|
|
|
|
|
|
ccid_reset(s);
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:50:33 +04:00
|
|
|
static const char *ccid_control_to_str(USBCCIDState *s, int request)
|
|
|
|
{
|
|
|
|
switch (request) {
|
|
|
|
/* generic - should be factored out if there are other debugees */
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
|
|
|
return "(generic) set address";
|
|
|
|
case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
|
|
|
return "(generic) get descriptor";
|
|
|
|
case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
|
|
|
return "(generic) get configuration";
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
|
|
|
return "(generic) set configuration";
|
|
|
|
case DeviceRequest | USB_REQ_GET_STATUS:
|
|
|
|
return "(generic) get status";
|
|
|
|
case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
|
|
|
|
return "(generic) clear feature";
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_FEATURE:
|
|
|
|
return "(generic) set_feature";
|
|
|
|
case InterfaceRequest | USB_REQ_GET_INTERFACE:
|
|
|
|
return "(generic) get interface";
|
|
|
|
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
|
|
|
|
return "(generic) set interface";
|
|
|
|
/* class requests */
|
|
|
|
case ClassInterfaceOutRequest | CCID_CONTROL_ABORT:
|
|
|
|
return "ABORT";
|
|
|
|
case ClassInterfaceRequest | CCID_CONTROL_GET_CLOCK_FREQUENCIES:
|
|
|
|
return "GET_CLOCK_FREQUENCIES";
|
|
|
|
case ClassInterfaceRequest | CCID_CONTROL_GET_DATA_RATES:
|
|
|
|
return "GET_DATA_RATES";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
static void ccid_handle_control(USBDevice *dev, USBPacket *p, int request,
|
2011-02-02 18:33:13 +03:00
|
|
|
int value, int index, int length, uint8_t *data)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
int ret;
|
2010-10-17 13:40:07 +04:00
|
|
|
|
2013-03-04 20:50:33 +04:00
|
|
|
DPRINTF(s, 1, "%s: got control %s (%x), value %x\n", __func__,
|
|
|
|
ccid_control_to_str(s, request), request, value);
|
2011-08-30 13:49:05 +04:00
|
|
|
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
|
|
|
|
if (ret >= 0) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
return;
|
2011-08-30 13:49:05 +04:00
|
|
|
}
|
|
|
|
|
2010-10-17 13:40:07 +04:00
|
|
|
switch (request) {
|
|
|
|
/* Class specific requests. */
|
2013-03-05 17:31:26 +04:00
|
|
|
case ClassInterfaceOutRequest | CCID_CONTROL_ABORT:
|
2010-10-17 13:40:07 +04:00
|
|
|
DPRINTF(s, 1, "ccid_control abort UNIMPLEMENTED\n");
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
2013-03-05 17:31:26 +04:00
|
|
|
case ClassInterfaceRequest | CCID_CONTROL_GET_CLOCK_FREQUENCIES:
|
2010-10-17 13:40:07 +04:00
|
|
|
DPRINTF(s, 1, "ccid_control get clock frequencies UNIMPLEMENTED\n");
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
2013-03-05 17:31:26 +04:00
|
|
|
case ClassInterfaceRequest | CCID_CONTROL_GET_DATA_RATES:
|
2010-10-17 13:40:07 +04:00
|
|
|
DPRINTF(s, 1, "ccid_control get data rates UNIMPLEMENTED\n");
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(s, 1, "got unsupported/bogus control %x, value %x\n",
|
|
|
|
request, value);
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-03-04 20:50:33 +04:00
|
|
|
DPRINTF(s, D_VERBOSE, "%s: status = %d\n", __func__, ret);
|
2010-10-17 13:40:07 +04:00
|
|
|
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);
|
2015-07-16 17:33:07 +03:00
|
|
|
usb_wakeup(s->bulk, 0);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-03-04 20:57:45 +04:00
|
|
|
h->abProtocolDataStructure = s->abProtocolDataStructure;
|
2010-10-17 13:40:07 +04:00
|
|
|
ccid_reset_error_status(s);
|
2015-07-16 17:33:07 +03:00
|
|
|
usb_wakeup(s->bulk, 0);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = cpu_to_le32(len);
|
|
|
|
p->b.hdr.bSlot = slot;
|
|
|
|
p->b.hdr.bSeq = seq;
|
|
|
|
p->b.bStatus = ccid_calc_status(s);
|
|
|
|
p->b.bError = s->bError;
|
|
|
|
if (p->b.bError) {
|
2013-03-04 20:50:33 +04:00
|
|
|
DPRINTF(s, D_VERBOSE, "error %d\n", p->b.bError);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2017-04-08 01:20:13 +03:00
|
|
|
if (len) {
|
2018-06-08 20:02:31 +03:00
|
|
|
assert(data);
|
2017-04-08 01:20:13 +03:00
|
|
|
memcpy(p->abData, data, len);
|
|
|
|
}
|
2010-10-17 13:40:07 +04:00
|
|
|
ccid_reset_error_status(s);
|
2015-07-16 17:33:07 +03:00
|
|
|
usb_wakeup(s->bulk, 0);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
2013-03-04 20:55:07 +04:00
|
|
|
static void ccid_report_error_failed(USBCCIDState *s, uint8_t error)
|
|
|
|
{
|
|
|
|
s->bmCommandStatus = COMMAND_STATUS_FAILED;
|
|
|
|
s->bError = error;
|
|
|
|
}
|
|
|
|
|
2010-10-17 13:40:07 +04:00
|
|
|
static void ccid_write_data_block_answer(USBCCIDState *s,
|
|
|
|
const uint8_t *data, uint32_t len)
|
|
|
|
{
|
|
|
|
uint8_t seq;
|
|
|
|
uint8_t slot;
|
|
|
|
|
|
|
|
if (!ccid_has_pending_answers(s)) {
|
2013-03-04 20:55:07 +04:00
|
|
|
DPRINTF(s, D_WARN, "error: no pending answer to return to guest\n");
|
|
|
|
ccid_report_error_failed(s, ERROR_ICC_MUTE);
|
|
|
|
return;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
ccid_remove_pending_answer(s, &slot, &seq);
|
|
|
|
ccid_write_data_block(s, slot, seq, data, len);
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:58:29 +04:00
|
|
|
static uint8_t atr_get_protocol_num(const uint8_t *atr, uint32_t len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (len < 2 || !(atr[1] & 0x80)) {
|
|
|
|
/* too short or TD1 not included */
|
|
|
|
return 0; /* T=0, default */
|
|
|
|
}
|
|
|
|
i = 1 + !!(atr[1] & 0x10) + !!(atr[1] & 0x20) + !!(atr[1] & 0x40);
|
|
|
|
i += !!(atr[1] & 0x80);
|
|
|
|
return atr[i] & 0x0f;
|
|
|
|
}
|
|
|
|
|
2010-10-17 13:40:07 +04:00
|
|
|
static void ccid_write_data_block_atr(USBCCIDState *s, CCID_Header *recv)
|
|
|
|
{
|
|
|
|
const uint8_t *atr = NULL;
|
|
|
|
uint32_t len = 0;
|
2013-03-04 20:58:29 +04:00
|
|
|
uint8_t atr_protocol_num;
|
|
|
|
CCID_T0ProtocolDataStructure *t0 = &s->abProtocolDataStructure.t0;
|
|
|
|
CCID_T1ProtocolDataStructure *t1 = &s->abProtocolDataStructure.t1;
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
if (s->card) {
|
2011-12-04 22:34:10 +04:00
|
|
|
atr = ccid_card_get_atr(s->card, &len);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2013-03-04 20:58:29 +04:00
|
|
|
atr_protocol_num = atr_get_protocol_num(atr, len);
|
|
|
|
DPRINTF(s, D_VERBOSE, "%s: atr contains protocol=%d\n", __func__,
|
|
|
|
atr_protocol_num);
|
|
|
|
/* set parameters from ATR - see spec page 109 */
|
|
|
|
s->bProtocolNum = (atr_protocol_num <= 1 ? atr_protocol_num
|
|
|
|
: s->bProtocolNum);
|
|
|
|
switch (atr_protocol_num) {
|
|
|
|
case 0:
|
|
|
|
/* TODO: unimplemented ATR T0 parameters */
|
|
|
|
t0->bmFindexDindex = 0;
|
|
|
|
t0->bmTCCKST0 = 0;
|
|
|
|
t0->bGuardTimeT0 = 0;
|
|
|
|
t0->bWaitingIntegerT0 = 0;
|
|
|
|
t0->bClockStop = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* TODO: unimplemented ATR T1 parameters */
|
|
|
|
t1->bmFindexDindex = 0;
|
|
|
|
t1->bmTCCKST1 = 0;
|
|
|
|
t1->bGuardTimeT1 = 0;
|
|
|
|
t1->bWaitingIntegerT1 = 0;
|
|
|
|
t1->bClockStop = 0;
|
|
|
|
t1->bIFSC = 0;
|
|
|
|
t1->bNadValue = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(s, D_WARN, "%s: error: unsupported ATR protocol %d\n",
|
|
|
|
__func__, atr_protocol_num);
|
|
|
|
}
|
2010-10-17 13:40:07 +04:00
|
|
|
ccid_write_data_block(s, recv->bSlot, recv->bSeq, atr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_set_parameters(USBCCIDState *s, CCID_Header *recv)
|
|
|
|
{
|
|
|
|
CCID_SetParameters *ph = (CCID_SetParameters *) recv;
|
2013-03-04 20:57:45 +04:00
|
|
|
uint32_t protocol_num = ph->bProtocolNum & 3;
|
|
|
|
|
|
|
|
if (protocol_num != 0 && protocol_num != 1) {
|
|
|
|
ccid_report_error_failed(s, ERROR_CMD_NOT_SUPPORTED);
|
2010-10-17 13:40:07 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-03-04 20:57:45 +04:00
|
|
|
s->bProtocolNum = protocol_num;
|
|
|
|
s->abProtocolDataStructure = ph->abProtocolDataStructure;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* must be 5 bytes for T=0, 7 bytes for T=1
|
|
|
|
* See page 52
|
|
|
|
*/
|
2013-03-04 20:57:45 +04:00
|
|
|
static const CCID_ProtocolDataStructure defaultProtocolDataStructure = {
|
|
|
|
.t1 = {
|
|
|
|
.bmFindexDindex = 0x77,
|
|
|
|
.bmTCCKST1 = 0x00,
|
|
|
|
.bGuardTimeT1 = 0x00,
|
|
|
|
.bWaitingIntegerT1 = 0x00,
|
|
|
|
.bClockStop = 0x00,
|
|
|
|
.bIFSC = 0xfe,
|
|
|
|
.bNadValue = 0x00,
|
|
|
|
}
|
|
|
|
};
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
static void ccid_reset_parameters(USBCCIDState *s)
|
|
|
|
{
|
2013-03-27 12:14:15 +04:00
|
|
|
s->bProtocolNum = 0; /* T=0 */
|
2013-03-04 20:57:45 +04:00
|
|
|
s->abProtocolDataStructure = defaultProtocolDataStructure;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: only a single slot is supported (SLOT_0) */
|
|
|
|
static void ccid_on_slot_change(USBCCIDState *s, bool full)
|
|
|
|
{
|
|
|
|
/* RDR_to_PC_NotifySlotChange, 6.3.1 page 56 */
|
|
|
|
uint8_t current = s->bmSlotICCState;
|
|
|
|
if (full) {
|
|
|
|
s->bmSlotICCState |= SLOT_0_STATE_MASK;
|
|
|
|
} else {
|
|
|
|
s->bmSlotICCState &= ~SLOT_0_STATE_MASK;
|
|
|
|
}
|
|
|
|
if (current != s->bmSlotICCState) {
|
|
|
|
s->bmSlotICCState |= SLOT_0_CHANGED_MASK;
|
|
|
|
}
|
|
|
|
s->notify_slot_change = true;
|
2013-01-29 15:44:35 +04:00
|
|
|
usb_wakeup(s->intr, 0);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_write_data_block_error(
|
|
|
|
USBCCIDState *s, uint8_t slot, uint8_t seq)
|
|
|
|
{
|
|
|
|
ccid_write_data_block(s, slot, seq, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_on_apdu_from_guest(USBCCIDState *s, CCID_XferBlock *recv)
|
|
|
|
{
|
|
|
|
uint32_t len;
|
|
|
|
|
|
|
|
if (ccid_card_status(s) != ICC_STATUS_PRESENT_ACTIVE) {
|
|
|
|
DPRINTF(s, 1,
|
|
|
|
"usb-ccid: not sending apdu to client, no card connected\n");
|
|
|
|
ccid_write_data_block_error(s, recv->hdr.bSlot, recv->hdr.bSeq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
len = le32_to_cpu(recv->hdr.dwLength);
|
2020-11-19 05:57:51 +03:00
|
|
|
DPRINTF(s, 1, "%s: seq %d, len %u\n", __func__,
|
2010-10-17 13:40:07 +04:00
|
|
|
recv->hdr.bSeq, len);
|
|
|
|
ccid_add_pending_answer(s, (CCID_Header *)recv);
|
2017-02-02 22:22:28 +03:00
|
|
|
if (s->card && len <= BULK_OUT_DATA_SIZE) {
|
2011-12-04 22:34:10 +04:00
|
|
|
ccid_card_apdu_from_guest(s->card, recv->abData, len);
|
2010-10-17 13:40:07 +04:00
|
|
|
} else {
|
|
|
|
DPRINTF(s, D_WARN, "warning: discarded apdu\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:50:33 +04:00
|
|
|
static const char *ccid_message_type_to_str(uint8_t type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn: return "IccPowerOn";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff: return "IccPowerOff";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus: return "GetSlotStatus";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock: return "XfrBlock";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters: return "GetParameters";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters: return "ResetParameters";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters: return "SetParameters";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_Escape: return "Escape";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_IccClock: return "IccClock";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU: return "T0APDU";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_Secure: return "Secure";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical: return "Mechanical";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_Abort: return "Abort";
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency:
|
|
|
|
return "SetDataRateAndClockFrequency";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
static void ccid_handle_bulk_out(USBCCIDState *s, USBPacket *p)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
|
|
|
CCID_Header *ccid_header;
|
|
|
|
|
2011-07-12 17:22:25 +04:00
|
|
|
if (p->iov.size + s->bulk_out_pos > BULK_OUT_DATA_SIZE) {
|
2017-02-16 16:13:37 +03:00
|
|
|
goto err;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2011-07-12 17:22:25 +04:00
|
|
|
usb_packet_copy(p, s->bulk_out_data + s->bulk_out_pos, p->iov.size);
|
|
|
|
s->bulk_out_pos += p->iov.size;
|
2017-02-16 16:13:38 +03:00
|
|
|
if (s->bulk_out_pos < 10) {
|
|
|
|
DPRINTF(s, 1, "%s: header incomplete\n", __func__);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccid_header = (CCID_Header *)s->bulk_out_data;
|
2017-02-16 16:13:39 +03:00
|
|
|
if ((s->bulk_out_pos - 10 < ccid_header->dwLength) &&
|
|
|
|
(p->iov.size == CCID_MAX_PACKET_SIZE)) {
|
2010-10-17 13:40:07 +04:00
|
|
|
DPRINTF(s, D_VERBOSE,
|
2020-11-19 05:57:51 +03:00
|
|
|
"usb-ccid: bulk_in: expecting more packets (%u/%u)\n",
|
2017-02-16 16:13:39 +03:00
|
|
|
s->bulk_out_pos - 10, ccid_header->dwLength);
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
return;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2017-02-16 16:13:39 +03:00
|
|
|
if (s->bulk_out_pos - 10 != ccid_header->dwLength) {
|
|
|
|
DPRINTF(s, 1,
|
2020-11-19 05:57:51 +03:00
|
|
|
"usb-ccid: bulk_in: message size mismatch (got %u, expected %u)\n",
|
2017-02-16 16:13:39 +03:00
|
|
|
s->bulk_out_pos - 10, ccid_header->dwLength);
|
|
|
|
goto err;
|
|
|
|
}
|
2017-02-16 16:13:37 +03:00
|
|
|
|
|
|
|
DPRINTF(s, D_MORE_INFO, "%s %x %s\n", __func__,
|
|
|
|
ccid_header->bMessageType,
|
|
|
|
ccid_message_type_to_str(ccid_header->bMessageType));
|
|
|
|
switch (ccid_header->bMessageType) {
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus:
|
|
|
|
ccid_write_slot_status(s, ccid_header);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn:
|
|
|
|
DPRINTF(s, 1, "%s: PowerOn: %d\n", __func__,
|
2010-10-17 13:40:07 +04:00
|
|
|
((CCID_IccPowerOn *)(ccid_header))->bPowerSelect);
|
2017-02-16 16:13:37 +03:00
|
|
|
s->powered = true;
|
|
|
|
if (!ccid_card_inserted(s)) {
|
|
|
|
ccid_report_error_failed(s, ERROR_ICC_MUTE);
|
|
|
|
}
|
|
|
|
/* atr is written regardless of error. */
|
|
|
|
ccid_write_data_block_atr(s, ccid_header);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff:
|
|
|
|
ccid_reset_error_status(s);
|
|
|
|
s->powered = false;
|
|
|
|
ccid_write_slot_status(s, ccid_header);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock:
|
|
|
|
ccid_on_apdu_from_guest(s, (CCID_XferBlock *)s->bulk_out_data);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters:
|
|
|
|
ccid_reset_error_status(s);
|
|
|
|
ccid_set_parameters(s, ccid_header);
|
|
|
|
ccid_write_parameters(s, ccid_header);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters:
|
|
|
|
ccid_reset_error_status(s);
|
|
|
|
ccid_reset_parameters(s);
|
|
|
|
ccid_write_parameters(s, ccid_header);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters:
|
|
|
|
ccid_reset_error_status(s);
|
|
|
|
ccid_write_parameters(s, ccid_header);
|
|
|
|
break;
|
|
|
|
case CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical:
|
|
|
|
ccid_report_error_failed(s, 0);
|
|
|
|
ccid_write_slot_status(s, ccid_header);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(s, 1,
|
2010-10-17 13:40:07 +04:00
|
|
|
"handle_data: ERROR: unhandled message type %Xh\n",
|
|
|
|
ccid_header->bMessageType);
|
2017-02-16 16:13:37 +03:00
|
|
|
/*
|
|
|
|
* The caller is expecting the device to respond, tell it we
|
|
|
|
* don't support the operation.
|
|
|
|
*/
|
|
|
|
ccid_report_error_failed(s, ERROR_CMD_NOT_SUPPORTED);
|
|
|
|
ccid_write_slot_status(s, ccid_header);
|
|
|
|
break;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
s->bulk_out_pos = 0;
|
2017-02-16 16:13:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
p->status = USB_RET_STALL;
|
|
|
|
s->bulk_out_pos = 0;
|
|
|
|
return;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:55:44 +03:00
|
|
|
static void ccid_bulk_in_copy_to_guest(USBCCIDState *s, USBPacket *p,
|
|
|
|
unsigned int max_packet_size)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
int len = 0;
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
ccid_bulk_in_get(s);
|
|
|
|
if (s->current_bulk_in != NULL) {
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
len = MIN(s->current_bulk_in->len - s->current_bulk_in->pos,
|
2011-07-12 17:22:25 +04:00
|
|
|
p->iov.size);
|
2018-05-16 14:55:44 +03:00
|
|
|
if (len) {
|
|
|
|
usb_packet_copy(p, s->current_bulk_in->data +
|
|
|
|
s->current_bulk_in->pos, len);
|
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
s->current_bulk_in->pos += len;
|
2018-05-16 14:55:44 +03:00
|
|
|
if (s->current_bulk_in->pos == s->current_bulk_in->len
|
|
|
|
&& len != max_packet_size) {
|
2010-10-17 13:40:07 +04:00
|
|
|
ccid_bulk_in_release(s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* return when device has no data - usb 2.0 spec Table 8-4 */
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_NAK;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
if (len) {
|
2010-10-17 13:40:07 +04:00
|
|
|
DPRINTF(s, D_MORE_INFO,
|
2011-07-12 17:22:25 +04:00
|
|
|
"%s: %zd/%d req/act to guest (BULK_IN)\n",
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
__func__, p->iov.size, len);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
if (len < p->iov.size) {
|
2010-10-17 13:40:07 +04:00
|
|
|
DPRINTF(s, 1,
|
2011-07-12 17:22:25 +04:00
|
|
|
"%s: returning short (EREMOTEIO) %d < %zd\n",
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
__func__, len, p->iov.size);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
static void ccid_handle_data(USBDevice *dev, USBPacket *p)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2011-07-12 17:22:25 +04:00
|
|
|
uint8_t buf[2];
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
switch (p->pid) {
|
|
|
|
case USB_TOKEN_OUT:
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
ccid_handle_bulk_out(s, p);
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_TOKEN_IN:
|
2012-01-12 16:23:01 +04:00
|
|
|
switch (p->ep->nr) {
|
2010-10-17 13:40:07 +04:00
|
|
|
case CCID_BULK_IN_EP:
|
2018-05-16 14:55:44 +03:00
|
|
|
ccid_bulk_in_copy_to_guest(s, p, dev->ep_ctl.max_packet_size);
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
|
|
|
case CCID_INT_IN_EP:
|
|
|
|
if (s->notify_slot_change) {
|
|
|
|
/* page 56, RDR_to_PC_NotifySlotChange */
|
2011-07-12 17:22:25 +04:00
|
|
|
buf[0] = CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange;
|
|
|
|
buf[1] = s->bmSlotICCState;
|
|
|
|
usb_packet_copy(p, buf, 2);
|
2010-10-17 13:40:07 +04:00
|
|
|
s->notify_slot_change = false;
|
|
|
|
s->bmSlotICCState &= ~SLOT_0_CHANGED_MASK;
|
|
|
|
DPRINTF(s, D_INFO,
|
|
|
|
"handle_data: int_in: notify_slot_change %X, "
|
2011-07-12 17:22:25 +04:00
|
|
|
"requested len %zd\n",
|
|
|
|
s->bmSlotICCState, p->iov.size);
|
2012-11-17 15:15:02 +04:00
|
|
|
} else {
|
|
|
|
p->status = USB_RET_NAK;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(s, 1, "Bad endpoint\n");
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPRINTF(s, 1, "Bad token\n");
|
usb: split packet result into actual_length + status
Since with the ehci and xhci controllers a single packet can be larger
then maxpacketsize, it is possible for the result of a single packet
to be both having transferred some data as well as the transfer to have
an error.
An example would be an input transfer from a bulk endpoint successfully
receiving 1 or more maxpacketsize packets from the device, followed
by a packet signalling halt.
While already touching all the devices and controllers handle_packet /
handle_data / handle_control code, also change the return type of
these functions to void, solely storing the status in the packet. To
make the code paths for regular versus async packet handling more
uniform.
This patch unfortunately is somewhat invasive, since makeing the qemu
usb core deal with this requires changes everywhere. This patch only
prepares the usb core for this, all the hcd / device changes are done
in such a way that there are no functional changes.
This patch has been tested with uhci and ehci hcds, together with usb-audio,
usb-hid and usb-storage devices, as well as with usb-redir redirection
with a wide variety of real devices.
Note that there is usually no need to directly set packet->actual_length
form devices handle_data callback, as that is done by usb_packet_copy()
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
2012-11-01 20:15:01 +04:00
|
|
|
p->status = USB_RET_STALL;
|
2010-10-17 13:40:07 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
static void ccid_unrealize(USBDevice *dev)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
ccid_bulk_in_clear(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ccid_flush_pending_answers(USBCCIDState *s)
|
|
|
|
{
|
|
|
|
while (ccid_has_pending_answers(s)) {
|
|
|
|
ccid_write_data_block_answer(s, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Answer *ccid_peek_next_answer(USBCCIDState *s)
|
|
|
|
{
|
|
|
|
return s->pending_answers_num == 0
|
|
|
|
? NULL
|
|
|
|
: &s->pending_answers[s->pending_answers_start % PENDING_ANSWERS_NUM];
|
|
|
|
}
|
|
|
|
|
2012-03-28 20:01:36 +04:00
|
|
|
static Property ccid_props[] = {
|
|
|
|
DEFINE_PROP_UINT32("slot", struct CCIDCardState, slot, 0),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2012-05-02 11:00:20 +04:00
|
|
|
#define TYPE_CCID_BUS "ccid-bus"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(CCIDBus, CCID_BUS)
|
2012-05-02 11:00:20 +04:00
|
|
|
|
|
|
|
static const TypeInfo ccid_bus_info = {
|
|
|
|
.name = TYPE_CCID_BUS,
|
|
|
|
.parent = TYPE_BUS,
|
|
|
|
.instance_size = sizeof(CCIDBus),
|
2010-10-17 13:40:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void ccid_card_send_apdu_to_guest(CCIDCardState *card,
|
|
|
|
uint8_t *apdu, uint32_t len)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
DeviceState *qdev = DEVICE(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
Answer *answer;
|
|
|
|
|
|
|
|
if (!ccid_has_pending_answers(s)) {
|
|
|
|
DPRINTF(s, 1, "CCID ERROR: got an APDU without pending answers\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->bmCommandStatus = COMMAND_STATUS_NO_ERROR;
|
|
|
|
answer = ccid_peek_next_answer(s);
|
|
|
|
if (answer == NULL) {
|
2013-03-04 20:55:07 +04:00
|
|
|
DPRINTF(s, D_WARN, "%s: error: unexpected lack of answer\n", __func__);
|
|
|
|
ccid_report_error_failed(s, ERROR_HW_ERROR);
|
|
|
|
return;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2020-11-19 05:57:51 +03:00
|
|
|
DPRINTF(s, 1, "APDU returned to guest %u (answer seq %d, slot %d)\n",
|
2010-10-17 13:40:07 +04:00
|
|
|
len, answer->seq, answer->slot);
|
|
|
|
ccid_write_data_block_answer(s, apdu, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccid_card_card_removed(CCIDCardState *card)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
DeviceState *qdev = DEVICE(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
ccid_on_slot_change(s, false);
|
|
|
|
ccid_flush_pending_answers(s);
|
|
|
|
ccid_reset(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccid_card_ccid_attach(CCIDCardState *card)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
DeviceState *qdev = DEVICE(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
DPRINTF(s, 1, "CCID Attach\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccid_card_ccid_detach(CCIDCardState *card)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
DeviceState *qdev = DEVICE(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
DPRINTF(s, 1, "CCID Detach\n");
|
|
|
|
if (ccid_card_inserted(s)) {
|
|
|
|
ccid_on_slot_change(s, false);
|
|
|
|
}
|
|
|
|
ccid_detach(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccid_card_card_error(CCIDCardState *card, uint64_t error)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
DeviceState *qdev = DEVICE(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
s->bmCommandStatus = COMMAND_STATUS_FAILED;
|
|
|
|
s->last_answer_error = error;
|
2011-04-04 08:48:08 +04:00
|
|
|
DPRINTF(s, 1, "VSC_Error: %" PRIX64 "\n", s->last_answer_error);
|
2011-04-05 01:54:04 +04:00
|
|
|
/* TODO: these errors should be more verbose and propagated to the guest.*/
|
2010-10-17 13:40:07 +04:00
|
|
|
/*
|
|
|
|
* We flush all pending answers on CardRemove message in ccid-card-passthru,
|
|
|
|
* so check that first to not trigger abort
|
|
|
|
*/
|
|
|
|
if (ccid_has_pending_answers(s)) {
|
|
|
|
ccid_write_data_block_answer(s, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ccid_card_card_inserted(CCIDCardState *card)
|
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
DeviceState *qdev = DEVICE(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
s->bmCommandStatus = COMMAND_STATUS_NO_ERROR;
|
|
|
|
ccid_flush_pending_answers(s);
|
|
|
|
ccid_on_slot_change(s, true);
|
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
static void ccid_card_unrealize(DeviceState *qdev)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2011-12-04 22:34:10 +04:00
|
|
|
CCIDCardState *card = CCID_CARD(qdev);
|
2018-01-25 20:14:32 +03:00
|
|
|
CCIDCardClass *cc = CCID_CARD_GET_CLASS(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
if (ccid_card_inserted(s)) {
|
|
|
|
ccid_card_card_removed(card);
|
|
|
|
}
|
2018-01-25 20:14:32 +03:00
|
|
|
if (cc->unrealize) {
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
cc->unrealize(card);
|
2018-01-25 20:14:32 +03:00
|
|
|
}
|
2010-10-17 13:40:07 +04:00
|
|
|
s->card = NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:14:30 +03:00
|
|
|
static void ccid_card_realize(DeviceState *qdev, Error **errp)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2011-12-04 22:34:10 +04:00
|
|
|
CCIDCardState *card = CCID_CARD(qdev);
|
2018-01-25 20:14:31 +03:00
|
|
|
CCIDCardClass *cc = CCID_CARD_GET_CLASS(card);
|
2015-07-17 12:34:11 +03:00
|
|
|
USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent);
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2018-01-25 20:14:30 +03:00
|
|
|
Error *local_err = NULL;
|
2010-10-17 13:40:07 +04:00
|
|
|
|
|
|
|
if (card->slot != 0) {
|
2018-01-25 20:14:30 +03:00
|
|
|
error_setg(errp, "usb-ccid supports one slot, can't add %d",
|
|
|
|
card->slot);
|
|
|
|
return;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
if (s->card != NULL) {
|
2018-01-25 20:14:30 +03:00
|
|
|
error_setg(errp, "usb-ccid card already full, not adding");
|
|
|
|
return;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2018-01-25 20:14:31 +03:00
|
|
|
if (cc->realize) {
|
|
|
|
cc->realize(card, &local_err);
|
|
|
|
if (local_err != NULL) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2018-01-25 20:14:30 +03:00
|
|
|
s->card = card;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
2014-09-19 10:48:35 +04:00
|
|
|
static void ccid_realize(USBDevice *dev, Error **errp)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2015-05-06 15:55:31 +03:00
|
|
|
USBCCIDState *s = USB_CCID_DEV(dev);
|
2010-10-17 13:40:07 +04:00
|
|
|
|
2012-04-20 14:33:30 +04:00
|
|
|
usb_desc_create_serial(dev);
|
2011-08-30 13:49:05 +04:00
|
|
|
usb_desc_init(dev);
|
2021-09-23 15:11:51 +03:00
|
|
|
qbus_init(&s->bus, sizeof(s->bus), TYPE_CCID_BUS, DEVICE(dev), NULL);
|
qdev: Drop qbus_set_hotplug_handler() parameter @errp
qbus_set_hotplug_handler() is a simple wrapper around
object_property_set_link().
object_property_set_link() fails when the property doesn't exist, is
not settable, or its .check() method fails. These are all programming
errors here, so passing &error_abort to qbus_set_hotplug_handler() is
appropriate.
Most of its callers do. Exceptions:
* pcie_cap_slot_init(), shpc_init(), spapr_phb_realize() pass NULL,
i.e. they ignore errors.
* spapr_machine_init() passes &error_fatal.
* s390_pcihost_realize(), virtio_serial_device_realize(),
s390_pcihost_plug() pass the error to their callers. The latter two
keep going after the error, which looks wrong.
Drop the @errp parameter, and instead pass &error_abort to
object_property_set_link().
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200630090351.1247703-15-armbru@redhat.com>
2020-06-30 12:03:39 +03:00
|
|
|
qbus_set_hotplug_handler(BUS(&s->bus), OBJECT(dev));
|
2012-01-17 16:25:46 +04:00
|
|
|
s->intr = usb_ep_get(dev, USB_TOKEN_IN, CCID_INT_IN_EP);
|
2015-07-16 17:33:07 +03:00
|
|
|
s->bulk = usb_ep_get(dev, USB_TOKEN_IN, CCID_BULK_IN_EP);
|
2010-10-17 13:40:07 +04:00
|
|
|
s->card = NULL;
|
|
|
|
s->dev.speed = USB_SPEED_FULL;
|
2011-05-27 16:27:18 +04:00
|
|
|
s->dev.speedmask = USB_SPEED_MASK_FULL;
|
2010-10-17 13:40:07 +04:00
|
|
|
s->notify_slot_change = false;
|
|
|
|
s->powered = true;
|
|
|
|
s->pending_answers_num = 0;
|
|
|
|
s->last_answer_error = 0;
|
|
|
|
s->bulk_in_pending_start = 0;
|
|
|
|
s->bulk_in_pending_end = 0;
|
|
|
|
s->current_bulk_in = NULL;
|
|
|
|
ccid_reset_error_status(s);
|
|
|
|
s->bulk_out_pos = 0;
|
|
|
|
ccid_reset_parameters(s);
|
|
|
|
ccid_reset(s);
|
2013-03-04 20:41:28 +04:00
|
|
|
s->debug = parse_debug_env("QEMU_CCID_DEBUG", D_VERBOSE, s->debug);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ccid_post_load(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
USBCCIDState *s = opaque;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be done after usb_device_attach, which sets state to ATTACHED,
|
|
|
|
* while it must be DEFAULT in order to accept packets (like it is after
|
|
|
|
* reset, but reset will reset our addr and call our reset handler which
|
|
|
|
* may change state, and we don't want to do that when migrating).
|
|
|
|
*/
|
|
|
|
s->dev.state = s->state_vmstate;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-25 14:29:12 +03:00
|
|
|
static int ccid_pre_save(void *opaque)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
|
|
|
USBCCIDState *s = opaque;
|
|
|
|
|
|
|
|
s->state_vmstate = s->dev.state;
|
2017-09-25 14:29:12 +03:00
|
|
|
|
|
|
|
return 0;
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
|
|
|
|
2021-03-13 20:11:50 +03:00
|
|
|
static const VMStateDescription bulk_in_vmstate = {
|
2010-10-17 13:40:07 +04:00
|
|
|
.name = "CCID BulkIn state",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_BUFFER(data, BulkIn),
|
|
|
|
VMSTATE_UINT32(len, BulkIn),
|
|
|
|
VMSTATE_UINT32(pos, BulkIn),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-13 20:11:50 +03:00
|
|
|
static const VMStateDescription answer_vmstate = {
|
2010-10-17 13:40:07 +04:00
|
|
|
.name = "CCID Answer state",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT8(slot, Answer),
|
|
|
|
VMSTATE_UINT8(seq, Answer),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-13 20:11:50 +03:00
|
|
|
static const VMStateDescription usb_device_vmstate = {
|
2010-10-17 13:40:07 +04:00
|
|
|
.name = "usb_device",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT8(addr, USBDevice),
|
|
|
|
VMSTATE_BUFFER(setup_buf, USBDevice),
|
|
|
|
VMSTATE_BUFFER(data_buf, USBDevice),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-13 20:11:50 +03:00
|
|
|
static const VMStateDescription ccid_vmstate = {
|
2013-06-27 15:03:44 +04:00
|
|
|
.name = "usb-ccid",
|
2010-10-17 13:40:07 +04:00
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.post_load = ccid_post_load,
|
|
|
|
.pre_save = ccid_pre_save,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_STRUCT(dev, USBCCIDState, 1, usb_device_vmstate, USBDevice),
|
|
|
|
VMSTATE_UINT8(debug, USBCCIDState),
|
|
|
|
VMSTATE_BUFFER(bulk_out_data, USBCCIDState),
|
|
|
|
VMSTATE_UINT32(bulk_out_pos, USBCCIDState),
|
|
|
|
VMSTATE_UINT8(bmSlotICCState, USBCCIDState),
|
|
|
|
VMSTATE_UINT8(powered, USBCCIDState),
|
|
|
|
VMSTATE_UINT8(notify_slot_change, USBCCIDState),
|
|
|
|
VMSTATE_UINT64(last_answer_error, USBCCIDState),
|
|
|
|
VMSTATE_UINT8(bError, USBCCIDState),
|
|
|
|
VMSTATE_UINT8(bmCommandStatus, USBCCIDState),
|
|
|
|
VMSTATE_UINT8(bProtocolNum, USBCCIDState),
|
2013-03-04 20:57:45 +04:00
|
|
|
VMSTATE_BUFFER(abProtocolDataStructure.data, USBCCIDState),
|
2010-10-17 13:40:07 +04:00
|
|
|
VMSTATE_UINT32(ulProtocolDataStructureSize, USBCCIDState),
|
|
|
|
VMSTATE_STRUCT_ARRAY(bulk_in_pending, USBCCIDState,
|
|
|
|
BULK_IN_PENDING_NUM, 1, bulk_in_vmstate, BulkIn),
|
|
|
|
VMSTATE_UINT32(bulk_in_pending_start, USBCCIDState),
|
|
|
|
VMSTATE_UINT32(bulk_in_pending_end, USBCCIDState),
|
|
|
|
VMSTATE_STRUCT_ARRAY(pending_answers, USBCCIDState,
|
|
|
|
PENDING_ANSWERS_NUM, 1, answer_vmstate, Answer),
|
|
|
|
VMSTATE_UINT32(pending_answers_num, USBCCIDState),
|
2017-10-13 15:55:33 +03:00
|
|
|
VMSTATE_UNUSED(1), /* was migration_state */
|
2010-10-17 13:40:07 +04:00
|
|
|
VMSTATE_UINT32(state_vmstate, USBCCIDState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static Property ccid_properties[] = {
|
|
|
|
DEFINE_PROP_UINT8("debug", USBCCIDState, debug, 0),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2011-12-16 00:53:10 +04:00
|
|
|
static void ccid_class_initfn(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-16 00:53:10 +04:00
|
|
|
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
|
2014-09-26 13:28:38 +04:00
|
|
|
HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
|
2011-12-16 00:53:10 +04:00
|
|
|
|
2014-09-19 10:48:35 +04:00
|
|
|
uc->realize = ccid_realize;
|
2011-12-16 00:53:10 +04:00
|
|
|
uc->product_desc = "QEMU USB CCID";
|
|
|
|
uc->usb_desc = &desc_ccid;
|
|
|
|
uc->handle_reset = ccid_handle_reset;
|
|
|
|
uc->handle_control = ccid_handle_control;
|
|
|
|
uc->handle_data = ccid_handle_data;
|
2017-02-21 17:14:45 +03:00
|
|
|
uc->unrealize = ccid_unrealize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->desc = "CCID Rev 1.1 smartcard reader";
|
|
|
|
dc->vmsd = &ccid_vmstate;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, ccid_properties);
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
|
2014-09-26 13:28:38 +04:00
|
|
|
hc->unplug = qdev_simple_device_unplug_cb;
|
2011-12-16 00:53:10 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo ccid_info = {
|
2020-09-03 01:42:14 +03:00
|
|
|
.name = TYPE_USB_CCID_DEV,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_USB_DEVICE,
|
|
|
|
.instance_size = sizeof(USBCCIDState),
|
|
|
|
.class_init = ccid_class_initfn,
|
2014-09-26 13:28:38 +04:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ TYPE_HOTPLUG_HANDLER },
|
|
|
|
{ }
|
|
|
|
}
|
2010-10-17 13:40:07 +04:00
|
|
|
};
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static void ccid_card_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *k = DEVICE_CLASS(klass);
|
2012-05-02 11:00:20 +04:00
|
|
|
k->bus_type = TYPE_CCID_BUS;
|
2018-01-25 20:14:30 +03:00
|
|
|
k->realize = ccid_card_realize;
|
2018-01-25 20:14:32 +03:00
|
|
|
k->unrealize = ccid_card_unrealize;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(k, ccid_props);
|
2011-12-08 07:34:16 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo ccid_card_type_info = {
|
2011-12-04 22:34:10 +04:00
|
|
|
.name = TYPE_CCID_CARD,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(CCIDCardState),
|
|
|
|
.abstract = true,
|
|
|
|
.class_size = sizeof(CCIDCardClass),
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = ccid_card_class_init,
|
2011-12-04 22:34:10 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void ccid_register_types(void)
|
2010-10-17 13:40:07 +04:00
|
|
|
{
|
2012-05-02 11:00:20 +04:00
|
|
|
type_register_static(&ccid_bus_info);
|
2011-12-04 22:34:10 +04:00
|
|
|
type_register_static(&ccid_card_type_info);
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&ccid_info);
|
2010-10-17 13:40:07 +04:00
|
|
|
}
|
2012-02-09 18:20:55 +04:00
|
|
|
|
|
|
|
type_init(ccid_register_types)
|