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