blob: 2d41c01b45ab58514035759dd08a6836422c2e4f [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 */
32
33 /* Bulk in pipe */
34 CEMU_USB_MSGT_DO_RX_DATA, /* TPDU data */
35 CEMU_USB_MSGT_DO_STATUS, /* Status information */
36 CEMU_USB_MSGT_DO_STATS, /* Statistics */
37 CEMU_USB_MSGT_DO_PTS, /* Information about PTS */
38 CEMU_USB_MSGT_DO_ERROR, /* Error message */
39};
40
41/* generic header, shared by all messages */
42struct cardemu_usb_msg_hdr {
43 uint8_t msg_type; /* enum cardemu_usb_msg_type */
44 uint8_t seq_nr; /* sequence number */
45 uint16_t data_len; /* length of optional data field */
46} __attribute__ ((packed));
47
48/* indicates a TPDU header is present in this message */
49#define CEMU_DATA_F_TPDU_HDR 0x00000001
50/* indicates last part of transmission in this direction */
51#define CEMU_DATA_F_FINAL 0x00000002
52/* incdicates a PB is present and we should continue with TX */
53#define CEMU_DATA_F_PB_AND_TX 0x00000004
54/* incdicates a PB is present and we should continue with RX */
55#define CEMU_DATA_F_PB_AND_RX 0x00000008
56
57/* CEMU_USB_MSGT_DT_SET_ATR */
58struct cardemu_usb_msg_set_atr {
59 struct cardemu_usb_msg_hdr hdr;
60 /* variable-length ATR data (hdr.data_len) */
61 uint8_t atr[0];
62} __attribute__ ((packed));
63
64/* CEMU_USB_MSGT_DT_TX_DATA */
65struct cardemu_usb_msg_tx_data {
66 struct cardemu_usb_msg_hdr hdr;
67 uint32_t flags;
68 /* variable-length TPDU data (hdr.data_len) */
69 uint8_t data[0];
70} __attribute__ ((packed));
71
72/* CEMU_USB_MSGT_DO_RX_DATA */
73struct cardemu_usb_msg_rx_data {
74 struct cardemu_usb_msg_hdr hdr;
75 uint32_t flags;
76 /* variable-length TPDU data (hdr.data_len) */
77 uint8_t data[0];
78} __attribute__ ((packed));
79
80#define CEMU_STATUS_F_VCC_PRESENT 0x00000001
81#define CEMU_STATUS_F_CLK_ACTIVE 0x00000002
82#define CEMU_STATUS_F_RCEMU_ACTIVE 0x00000004
83#define CEMU_STATUS_F_CARD_INSERT 0x00000008
84
85/* CEMU_USB_MSGT_DO_STATUS */
86struct cardemu_usb_msg_status {
87 struct cardemu_usb_msg_hdr hdr;
88 uint32_t flags;
89 /* phone-applied target voltage in mV */
90 uint16_t voltage_mv;
91 /* Fi/Di related information */
92 uint8_t fi;
93 uint8_t di;
94 uint8_t wi;
95 uint32_t waiting_time;
96} __attribute__ ((packed));
97
98/* CEMU_USB_MSGT_DO_PTS */
99struct cardemu_usb_msg_pts_info {
100 struct cardemu_usb_msg_hdr hdr;
101 /* PTS request as sent from reader */
102 uint8_t req[6];
103 /* PTS response as sent by card */
104 uint8_t resp[6];
105} __attribute__ ((packed));
106
107/* CEMU_USB_MSGT_DO_ERROR */
108struct cardemu_usb_msg_error {
109 struct cardemu_usb_msg_hdr hdr;
110 uint8_t severity;
111 uint8_t subsystem;
112 uint16_t code;
113 /* human-readable error message */
114 uint8_t msg[0];
115} __attribute__ ((packed));
116
117static inline void cardemu_hdr_set(struct cardemu_usb_msg_hdr *hdr, uint16_t msgt)
118{
119 memset(hdr, 0, sizeof(*hdr));
120 hdr->msg_type = msgt;
121}