blob: 8b781bcf314c64d6afb2ba7e49c682f55daf6a6b [file] [log] [blame]
Harald Welte9d3e3822015-11-09 00:50:54 +01001#pragma once
2
3/* Smart Card Emulation USB protocol */
4
5/* (C) 2015 by Harald Welte <hwelte@hmw-consulting.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <stdint.h>
24
25/* DT = Device Terminated. DO = Device Originated */
26enum cardemu_usb_msg_type {
27 /* Bulk out pipe */
28 CEMU_USB_MSGT_DT_TX_DATA, /* TPDU Date */
29 CEMU_USB_MSGT_DT_SET_ATR, /* Set the ATR stored in simulator */
30 CEMU_USB_MSGT_DT_GET_STATS, /* request DO_STATS */
31 CEMU_USB_MSGT_DT_GET_STATUS, /* request DO_STATUS */
Harald Welte0eaa9922016-03-04 03:03:49 +010032 CEMU_USB_MSGT_DT_CARDINSERT, /* insert/remove card */
Harald Welte9d3e3822015-11-09 00:50:54 +010033
34 /* Bulk in pipe */
35 CEMU_USB_MSGT_DO_RX_DATA, /* TPDU data */
36 CEMU_USB_MSGT_DO_STATUS, /* Status information */
37 CEMU_USB_MSGT_DO_STATS, /* Statistics */
38 CEMU_USB_MSGT_DO_PTS, /* Information about PTS */
39 CEMU_USB_MSGT_DO_ERROR, /* Error message */
40};
41
42/* generic header, shared by all messages */
43struct cardemu_usb_msg_hdr {
44 uint8_t msg_type; /* enum cardemu_usb_msg_type */
45 uint8_t seq_nr; /* sequence number */
Harald Welted295b922016-03-18 21:01:36 +010046 uint16_t msg_len; /* length of message including hdr */
47 uint8_t data[0];
Harald Welte9d3e3822015-11-09 00:50:54 +010048} __attribute__ ((packed));
49
50/* indicates a TPDU header is present in this message */
51#define CEMU_DATA_F_TPDU_HDR 0x00000001
52/* indicates last part of transmission in this direction */
53#define CEMU_DATA_F_FINAL 0x00000002
54/* incdicates a PB is present and we should continue with TX */
55#define CEMU_DATA_F_PB_AND_TX 0x00000004
56/* incdicates a PB is present and we should continue with RX */
57#define CEMU_DATA_F_PB_AND_RX 0x00000008
58
Harald Welte0eaa9922016-03-04 03:03:49 +010059/* CEMU_USB_MSGT_DT_CARDINSERT */
60struct cardemu_usb_msg_cardinsert {
61 struct cardemu_usb_msg_hdr hdr;
62 uint8_t card_insert;
63} __attribute__ ((packed));
64
Harald Welte9d3e3822015-11-09 00:50:54 +010065/* CEMU_USB_MSGT_DT_SET_ATR */
66struct cardemu_usb_msg_set_atr {
67 struct cardemu_usb_msg_hdr hdr;
Harald Welted295b922016-03-18 21:01:36 +010068 uint8_t atr_len;
69 /* variable-length ATR data */
Harald Welte9d3e3822015-11-09 00:50:54 +010070 uint8_t atr[0];
71} __attribute__ ((packed));
72
73/* CEMU_USB_MSGT_DT_TX_DATA */
74struct cardemu_usb_msg_tx_data {
75 struct cardemu_usb_msg_hdr hdr;
76 uint32_t flags;
Harald Welted295b922016-03-18 21:01:36 +010077 uint16_t data_len;
78 /* variable-length TPDU data */
Harald Welte9d3e3822015-11-09 00:50:54 +010079 uint8_t data[0];
80} __attribute__ ((packed));
81
82/* CEMU_USB_MSGT_DO_RX_DATA */
83struct cardemu_usb_msg_rx_data {
84 struct cardemu_usb_msg_hdr hdr;
85 uint32_t flags;
Harald Welted295b922016-03-18 21:01:36 +010086 uint16_t data_len;
87 /* variable-length TPDU data */
Harald Welte9d3e3822015-11-09 00:50:54 +010088 uint8_t data[0];
89} __attribute__ ((packed));
90
91#define CEMU_STATUS_F_VCC_PRESENT 0x00000001
92#define CEMU_STATUS_F_CLK_ACTIVE 0x00000002
93#define CEMU_STATUS_F_RCEMU_ACTIVE 0x00000004
94#define CEMU_STATUS_F_CARD_INSERT 0x00000008
Harald Welteff160652016-03-19 21:59:06 +010095#define CEMU_STATUS_F_RESET_ACTIVE 0x00000010
Harald Welte9d3e3822015-11-09 00:50:54 +010096
97/* CEMU_USB_MSGT_DO_STATUS */
98struct cardemu_usb_msg_status {
99 struct cardemu_usb_msg_hdr hdr;
100 uint32_t flags;
101 /* phone-applied target voltage in mV */
102 uint16_t voltage_mv;
103 /* Fi/Di related information */
104 uint8_t fi;
105 uint8_t di;
106 uint8_t wi;
107 uint32_t waiting_time;
108} __attribute__ ((packed));
109
110/* CEMU_USB_MSGT_DO_PTS */
111struct cardemu_usb_msg_pts_info {
112 struct cardemu_usb_msg_hdr hdr;
Harald Welted295b922016-03-18 21:01:36 +0100113 uint8_t pts_len;
Harald Welte9d3e3822015-11-09 00:50:54 +0100114 /* PTS request as sent from reader */
115 uint8_t req[6];
116 /* PTS response as sent by card */
117 uint8_t resp[6];
118} __attribute__ ((packed));
119
120/* CEMU_USB_MSGT_DO_ERROR */
121struct cardemu_usb_msg_error {
122 struct cardemu_usb_msg_hdr hdr;
123 uint8_t severity;
124 uint8_t subsystem;
125 uint16_t code;
Harald Welted295b922016-03-18 21:01:36 +0100126 uint8_t msg_len;
Harald Welte9d3e3822015-11-09 00:50:54 +0100127 /* human-readable error message */
128 uint8_t msg[0];
129} __attribute__ ((packed));
130
131static inline void cardemu_hdr_set(struct cardemu_usb_msg_hdr *hdr, uint16_t msgt)
132{
133 memset(hdr, 0, sizeof(*hdr));
134 hdr->msg_type = msgt;
135}