blob: e550616b5e7ae2e461427700c700f7e130a42e41 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace2 USB protocol
Harald Welte25a9a802017-05-08 13:30:09 +02002 *
Kévin Redon9a12d682018-07-08 13:21:16 +02003 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
4 * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Harald Welte25a9a802017-05-08 13:30:09 +02005 *
Kévin Redon9a12d682018-07-08 13:21:16 +02006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
Harald Welte25a9a802017-05-08 13:30:09 +020010 *
Kévin Redon9a12d682018-07-08 13:21:16 +020011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Harald Welte25a9a802017-05-08 13:30:09 +020015 *
Kévin Redon9a12d682018-07-08 13:21:16 +020016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
Harald Welte25a9a802017-05-08 13:30:09 +020019 */
Kévin Redon012940f2018-07-01 18:21:30 +020020#pragma once
21
22#include <stdint.h>
23#include <stdbool.h>
Harald Welte25a9a802017-05-08 13:30:09 +020024
25/***********************************************************************
26 * COMMON HEADER
27 ***********************************************************************/
28
29enum simtrace_msg_class {
30 SIMTRACE_MSGC_GENERIC = 0,
31 /* Card Emulation / Forwarding */
32 SIMTRACE_MSGC_CARDEM,
Kévin Redon42bd0262018-06-27 16:43:23 +020033 /* Modem Control (if modem is attached next to device) */
Harald Welte25a9a802017-05-08 13:30:09 +020034 SIMTRACE_MSGC_MODEM,
Kévin Redon012940f2018-07-01 18:21:30 +020035 /* Reader/phone-car/SIM communication sniff */
36 SIMTRACE_MSGC_SNIFF,
Harald Welte25a9a802017-05-08 13:30:09 +020037
38 /* first vendor-specific request */
39 _SIMTRACE_MGSC_VENDOR_FIRST = 127,
40};
41
42enum simtrace_msg_type_generic {
43 /* Generic Error Message */
44 SIMTRACE_CMD_DO_ERROR = 0,
45 /* Request/Response for simtrace_board_info */
46 SIMTRACE_CMD_BD_BOARD_INFO,
47};
48
49/* SIMTRACE_MSGC_CARDEM */
50enum simtrace_msg_type_cardem {
51 /* TPDU Data to be transmitted to phone */
52 SIMTRACE_MSGT_DT_CEMU_TX_DATA = 1,
53 /* Set the ATR to be returned at phone-SIM reset */
54 SIMTRACE_MSGT_DT_CEMU_SET_ATR,
55 /* Get Statistics Request / Response */
56 SIMTRACE_MSGT_BD_CEMU_STATS,
57 /* Get Status Request / Response */
58 SIMTRACE_MSGT_BD_CEMU_STATUS,
59 /* Request / Confirm emulated card insert */
60 SIMTRACE_MSGT_DT_CEMU_CARDINSERT,
61 /* TPDU Data received from phomne */
62 SIMTRACE_MSGT_DO_CEMU_RX_DATA,
63 /* Indicate PTS request from phone */
64 SIMTRACE_MSGT_DO_CEMU_PTS,
Harald Weltea5bbe782019-12-16 10:39:55 +010065 /* Set configurable parameters */
66 SIMTRACE_MSGT_BD_CEMU_CONFIG,
Harald Welte25a9a802017-05-08 13:30:09 +020067};
68
69/* SIMTRACE_MSGC_MODEM */
70enum simtrace_msg_type_modem {
71 /* Modem Control: Reset an attached modem */
72 SIMTRACE_MSGT_DT_MODEM_RESET = 1,
73 /* Modem Control: Select local / remote SIM */
74 SIMTRACE_MSGT_DT_MODEM_SIM_SELECT,
75 /* Modem Control: Status (WWAN LED, SIM Presence) */
76 SIMTRACE_MSGT_BD_MODEM_STATUS,
77};
78
Kévin Redon012940f2018-07-01 18:21:30 +020079/* SIMTRACE_MSGC_SNIFF */
80enum simtrace_msg_type_sniff {
81 /* Status change (card inserted, reset, ...) */
82 SIMTRACE_MSGT_SNIFF_CHANGE = 0,
83 /* Fi/Di baudrate change */
84 SIMTRACE_MSGT_SNIFF_FIDI,
85 /* ATR data */
86 SIMTRACE_MSGT_SNIFF_ATR,
87 /* PPS (request or response) data */
88 SIMTRACE_MSGT_SNIFF_PPS,
89 /* TPDU data */
90 SIMTRACE_MSGT_SNIFF_TPDU,
Harald Welte25a9a802017-05-08 13:30:09 +020091};
92
93/* common message header */
94struct simtrace_msg_hdr {
95 uint8_t msg_class; /* simtrace_msg_class */
96 uint8_t msg_type; /* simtrace_msg_type_xxx */
97 uint8_t seq_nr;
98 uint8_t slot_nr; /* SIM slot number */
99 uint16_t _reserved;
100 uint16_t msg_len; /* length including header */
101 uint8_t payload[0];
102} __attribute__ ((packed));
103
104/***********************************************************************
Kévin Redon012940f2018-07-01 18:21:30 +0200105 * Capabilities
Harald Welte25a9a802017-05-08 13:30:09 +0200106 ***********************************************************************/
107
108/* generic capabilities */
109enum simtrace_capability_generic {
110 /* compatible with 5V SIM card interface */
111 SIMTRACE_CAP_VOLT_5V,
112 /* compatible with 3.3V SIM card interface */
113 SIMTRACE_CAP_VOLT_3V3,
114 /* compatible with 1.8V SIM card interface */
115 SIMTRACE_CAP_VOLT_1V8,
116 /* Has LED1 */
117 SIMTRACE_CAP_LED_1,
118 /* Has LED2 */
119 SIMTRACE_CAP_LED_2,
Kévin Redon42bd0262018-06-27 16:43:23 +0200120 /* Has Single-Pole Dual-Throw (local/remote SIM) */
Harald Welte25a9a802017-05-08 13:30:09 +0200121 SIMTRACE_CAP_SPDT,
122 /* Has Bus-Switch (trace / MITM) */
123 SIMTRACE_CAP_BUS_SWITCH,
124 /* Can read VSIM via ADC */
125 SIMTRACE_CAP_VSIM_ADC,
126 /* Can read temperature via ADC */
127 SIMTRACE_CAP_TEMP_ADC,
128 /* Supports DFU for firmware update */
129 SIMTRACE_CAP_DFU,
130 /* Supports Ctrl EP command for erasing flash / return to SAM-BA */
131 SIMTRACE_CAP_ERASE_FLASH,
132 /* Can read the status of card insert contact */
133 SIMTRACE_CAP_READ_CARD_DET,
134 /* Can control the status of a simulated card insert */
135 SIMTRACE_CAP_ASSERT_CARD_DET,
136 /* Can toggle the hardware reset of an attached modem */
137 SIMTRACE_CAP_ASSERT_MODEM_RST,
138};
139
Kévin Redon42bd0262018-06-27 16:43:23 +0200140/* vendor-specific capabilities of sysmocom devices */
Harald Welte25a9a802017-05-08 13:30:09 +0200141enum simtrace_capability_vendor {
142 /* Can erase a peer SAM3 controller */
143 SIMTRACE_CAP_SYSMO_QMOD_ERASE_PEER,
144 /* Can read/write an attached EEPROM */
145 SIMTRACE_CAP_SYSMO_QMOD_RW_EEPROM,
146 /* can reset an attached USB hub */
147 SIMTRACE_CAP_SYSMO_QMOD_RESET_HUB,
148};
149
Harald Welte25a9a802017-05-08 13:30:09 +0200150/* SIMTRACE_CMD_BD_BOARD_INFO */
151struct simtrace_board_info {
152 struct {
153 char manufacturer[32];
154 char model[32];
155 char version[32];
156 } hardware;
157 struct {
158 /* who provided this software? */
159 char provider[32];
160 /* name of software image */
161 char name[32];
162 /* (git) version at build time */
163 char version[32];
164 /* built on which machine? */
165 char buildhost[32];
166 /* CRC-32 over software image */
167 uint32_t crc;
168 } software;
169 struct {
170 /* Maximum baud rate supported */
171 uint32_t max_baud_rate;
172 } speed;
173 /* number of bytes of generic capability bit-mask */
174 uint8_t cap_generic_bytes;
175 /* number of bytes of vendor capability bit-mask */
176 uint8_t cap_vendor_bytes;
177 uint8_t data[0];
178 /* cap_generic + cap_vendor */
179} __attribute__ ((packed));
180
181/***********************************************************************
182 * CARD EMULATOR / FORWARDER
183 ***********************************************************************/
184
185/* indicates a TPDU header is present in this message */
186#define CEMU_DATA_F_TPDU_HDR 0x00000001
187/* indicates last part of transmission in this direction */
188#define CEMU_DATA_F_FINAL 0x00000002
189/* incdicates a PB is present and we should continue with TX */
190#define CEMU_DATA_F_PB_AND_TX 0x00000004
191/* incdicates a PB is present and we should continue with RX */
192#define CEMU_DATA_F_PB_AND_RX 0x00000008
193
194/* CEMU_USB_MSGT_DT_CARDINSERT */
195struct cardemu_usb_msg_cardinsert {
196 uint8_t card_insert;
197} __attribute__ ((packed));
198
199/* CEMU_USB_MSGT_DT_SET_ATR */
200struct cardemu_usb_msg_set_atr {
201 uint8_t atr_len;
202 /* variable-length ATR data */
203 uint8_t atr[0];
204} __attribute__ ((packed));
205
206/* CEMU_USB_MSGT_DT_TX_DATA */
207struct cardemu_usb_msg_tx_data {
208 uint32_t flags;
209 uint16_t data_len;
210 /* variable-length TPDU data */
211 uint8_t data[0];
212} __attribute__ ((packed));
213
214/* CEMU_USB_MSGT_DO_RX_DATA */
215struct cardemu_usb_msg_rx_data {
216 uint32_t flags;
217 uint16_t data_len;
218 /* variable-length TPDU data */
219 uint8_t data[0];
220} __attribute__ ((packed));
221
222#define CEMU_STATUS_F_VCC_PRESENT 0x00000001
223#define CEMU_STATUS_F_CLK_ACTIVE 0x00000002
224#define CEMU_STATUS_F_RCEMU_ACTIVE 0x00000004
225#define CEMU_STATUS_F_CARD_INSERT 0x00000008
226#define CEMU_STATUS_F_RESET_ACTIVE 0x00000010
227
228/* CEMU_USB_MSGT_DO_STATUS */
229struct cardemu_usb_msg_status {
230 uint32_t flags;
231 /* phone-applied target voltage in mV */
232 uint16_t voltage_mv;
Harald Welte79f0ea72021-04-05 17:55:54 +0200233 /* F/D related information. Not actual Fn/Dn values but indexes into tables! */
234 union {
235 uint8_t F_index; /* <! Index to ISO7816-3 Table 7 (F and f_max values) */
236 uint8_t fi; /* <! old, wrong name for API compatibility */
237 };
238 union {
239 uint8_t D_index; /* <! Index to ISO7816-3 Table 8 (D value) */
240 uint8_t di; /* <! old, wrong name for API compatibility */
241 };
242 uint8_t wi; /* <! Waiting Integer as defined in ISO7816-3 Section 10.2 */
243 uint32_t waiting_time; /* <! Waiting Time in etu as defined in ISO7816-3 Section 8.1 */
Harald Welte25a9a802017-05-08 13:30:09 +0200244} __attribute__ ((packed));
245
246/* CEMU_USB_MSGT_DO_PTS */
247struct cardemu_usb_msg_pts_info {
248 uint8_t pts_len;
249 /* PTS request as sent from reader */
250 uint8_t req[6];
251 /* PTS response as sent by card */
252 uint8_t resp[6];
253} __attribute__ ((packed));
254
255/* CEMU_USB_MSGT_DO_ERROR */
256struct cardemu_usb_msg_error {
257 uint8_t severity;
258 uint8_t subsystem;
259 uint16_t code;
260 uint8_t msg_len;
261 /* human-readable error message */
262 uint8_t msg[0];
263} __attribute__ ((packed));
264
Harald Weltea5bbe782019-12-16 10:39:55 +0100265/* enable/disable the generation of DO_STATUS on IRQ endpoint */
266#define CEMU_FEAT_F_STATUS_IRQ 0x00000001
267
268/* SIMTRACE_MSGT_BD_CEMU_CONFIG */
269struct cardemu_usb_msg_config {
270 /* bit-mask of CEMU_FEAT_F flags */
271 uint32_t features;
272} __attribute__ ((packed));
273
Harald Welte25a9a802017-05-08 13:30:09 +0200274/***********************************************************************
275 * MODEM CONTROL
276 ***********************************************************************/
277
278/* SIMTRACE_MSGT_DT_MODEM_RESET */
279struct st_modem_reset {
Kévin Redon42bd0262018-06-27 16:43:23 +0200280 /* 0: de-assert reset, 1: assert reset, 2: pulse reset */
Harald Welte25a9a802017-05-08 13:30:09 +0200281 uint8_t asserted;
282 /* if above is '2', duration of pulse in ms */
283 uint16_t pulse_duration_msec;
284} __attribute__((packed));
285
286/* SIMTRACE_MSGT_DT_MODEM_SIM_SELECT */
287struct st_modem_sim_select {
288 /* remote (1), local (0) */
289 uint8_t remote_sim;
290} __attribute__((packed));
291
292/* SIMTRACE_MSGT_BD_MODEM_STATUS */
293#define ST_MDM_STS_BIT_WWAN_LED (1 << 0)
294#define ST_MDM_STS_BIT_CARD_INSERTED (1 << 1)
295struct st_modem_status {
296 /* bit-field of supported status bits */
297 uint8_t supported_mask;
298 /* bit-field of current status bits */
299 uint8_t status_mask;
300 /* bit-field of changed status bits */
301 uint8_t changed_mask;
302} __attribute__((packed));
Kévin Redon012940f2018-07-01 18:21:30 +0200303
304/***********************************************************************
305 * SNIFF
306 ***********************************************************************/
307
308/* SIMTRACE_MSGT_SNIFF_CHANGE flags */
309#define SNIFF_CHANGE_FLAG_CARD_INSERT (1<<0)
310#define SNIFF_CHANGE_FLAG_CARD_EJECT (1<<1)
Kévin Redon8e84f812018-07-26 15:34:03 +0200311#define SNIFF_CHANGE_FLAG_RESET_ASSERT (1<<2)
312#define SNIFF_CHANGE_FLAG_RESET_DEASSERT (1<<3)
Kévin Redon012940f2018-07-01 18:21:30 +0200313#define SNIFF_CHANGE_FLAG_TIMEOUT_WT (1<<4)
Kévin Redonf82f0f62018-07-08 15:10:23 +0200314/* SIMTRACE_MSGT_SNIFF_ATR, SIMTRACE_MSGT_SNIFF_PPS, SIMTRACE_MSGT_SNIFF_TPDU flags */
315#define SNIFF_DATA_FLAG_ERROR_INCOMPLETE (1<<5)
316#define SNIFF_DATA_FLAG_ERROR_MALFORMED (1<<6)
Kévin Redonf66af0c2018-07-11 10:27:13 +0200317#define SNIFF_DATA_FLAG_ERROR_CHECKSUM (1<<7)
Kévin Redon012940f2018-07-01 18:21:30 +0200318
319/* SIMTRACE_MSGT_SNIFF_CHANGE */
320struct sniff_change {
321 /* SIMTRACE_MSGT_SNIFF_CHANGE flags */
322 uint32_t flags;
323} __attribute__ ((packed));
324
325/* SIMTRACE_MSGT_SNIFF_FIDI */
326struct sniff_fidi {
327 /* Fi/Di values as encoded in TA1 */
328 uint8_t fidi;
329} __attribute__ ((packed));
330
331/* SIMTRACE_MSGT_SNIFF_ATR, SIMTRACE_MSGT_SNIFF_PPS, SIMTRACE_MSGT_SNIFF_TPDU */
332struct sniff_data {
Kévin Redonf82f0f62018-07-08 15:10:23 +0200333 /* data flags */
334 uint32_t flags;
Kévin Redon012940f2018-07-01 18:21:30 +0200335 /* data length */
336 uint16_t length;
337 /* data */
338 uint8_t data[0];
339} __attribute__ ((packed));