Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* SIMtrace2 USB protocol |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 2 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 3 | * (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 Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 5 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 6 | * 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 Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 10 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 11 | * 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 Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 15 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 16 | * 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 Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 19 | */ |
Kévin Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 20 | #pragma once |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 24 | |
| 25 | /*********************************************************************** |
| 26 | * COMMON HEADER |
| 27 | ***********************************************************************/ |
| 28 | |
| 29 | enum simtrace_msg_class { |
| 30 | SIMTRACE_MSGC_GENERIC = 0, |
| 31 | /* Card Emulation / Forwarding */ |
| 32 | SIMTRACE_MSGC_CARDEM, |
Kévin Redon | 42bd026 | 2018-06-27 16:43:23 +0200 | [diff] [blame] | 33 | /* Modem Control (if modem is attached next to device) */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 34 | SIMTRACE_MSGC_MODEM, |
Kévin Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 35 | /* Reader/phone-car/SIM communication sniff */ |
| 36 | SIMTRACE_MSGC_SNIFF, |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 37 | |
| 38 | /* first vendor-specific request */ |
| 39 | _SIMTRACE_MGSC_VENDOR_FIRST = 127, |
| 40 | }; |
| 41 | |
| 42 | enum 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 */ |
| 50 | enum 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 Welte | a5bbe78 | 2019-12-16 10:39:55 +0100 | [diff] [blame] | 65 | /* Set configurable parameters */ |
| 66 | SIMTRACE_MSGT_BD_CEMU_CONFIG, |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /* SIMTRACE_MSGC_MODEM */ |
| 70 | enum 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 Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 79 | /* SIMTRACE_MSGC_SNIFF */ |
| 80 | enum 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 Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /* common message header */ |
| 94 | struct 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 Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 105 | * Capabilities |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 106 | ***********************************************************************/ |
| 107 | |
| 108 | /* generic capabilities */ |
| 109 | enum 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 Redon | 42bd026 | 2018-06-27 16:43:23 +0200 | [diff] [blame] | 120 | /* Has Single-Pole Dual-Throw (local/remote SIM) */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 121 | 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 Redon | 42bd026 | 2018-06-27 16:43:23 +0200 | [diff] [blame] | 140 | /* vendor-specific capabilities of sysmocom devices */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 141 | enum 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 Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 150 | /* SIMTRACE_CMD_BD_BOARD_INFO */ |
| 151 | struct 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 */ |
| 195 | struct cardemu_usb_msg_cardinsert { |
| 196 | uint8_t card_insert; |
| 197 | } __attribute__ ((packed)); |
| 198 | |
| 199 | /* CEMU_USB_MSGT_DT_SET_ATR */ |
| 200 | struct 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 */ |
| 207 | struct 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 */ |
| 215 | struct 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 */ |
| 229 | struct cardemu_usb_msg_status { |
| 230 | uint32_t flags; |
| 231 | /* phone-applied target voltage in mV */ |
| 232 | uint16_t voltage_mv; |
| 233 | /* Fi/Di related information */ |
| 234 | uint8_t fi; |
| 235 | uint8_t di; |
| 236 | uint8_t wi; |
| 237 | uint32_t waiting_time; |
| 238 | } __attribute__ ((packed)); |
| 239 | |
| 240 | /* CEMU_USB_MSGT_DO_PTS */ |
| 241 | struct cardemu_usb_msg_pts_info { |
| 242 | uint8_t pts_len; |
| 243 | /* PTS request as sent from reader */ |
| 244 | uint8_t req[6]; |
| 245 | /* PTS response as sent by card */ |
| 246 | uint8_t resp[6]; |
| 247 | } __attribute__ ((packed)); |
| 248 | |
| 249 | /* CEMU_USB_MSGT_DO_ERROR */ |
| 250 | struct cardemu_usb_msg_error { |
| 251 | uint8_t severity; |
| 252 | uint8_t subsystem; |
| 253 | uint16_t code; |
| 254 | uint8_t msg_len; |
| 255 | /* human-readable error message */ |
| 256 | uint8_t msg[0]; |
| 257 | } __attribute__ ((packed)); |
| 258 | |
Harald Welte | a5bbe78 | 2019-12-16 10:39:55 +0100 | [diff] [blame] | 259 | /* enable/disable the generation of DO_STATUS on IRQ endpoint */ |
| 260 | #define CEMU_FEAT_F_STATUS_IRQ 0x00000001 |
| 261 | |
| 262 | /* SIMTRACE_MSGT_BD_CEMU_CONFIG */ |
| 263 | struct cardemu_usb_msg_config { |
| 264 | /* bit-mask of CEMU_FEAT_F flags */ |
| 265 | uint32_t features; |
| 266 | } __attribute__ ((packed)); |
| 267 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 268 | /*********************************************************************** |
| 269 | * MODEM CONTROL |
| 270 | ***********************************************************************/ |
| 271 | |
| 272 | /* SIMTRACE_MSGT_DT_MODEM_RESET */ |
| 273 | struct st_modem_reset { |
Kévin Redon | 42bd026 | 2018-06-27 16:43:23 +0200 | [diff] [blame] | 274 | /* 0: de-assert reset, 1: assert reset, 2: pulse reset */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 275 | uint8_t asserted; |
| 276 | /* if above is '2', duration of pulse in ms */ |
| 277 | uint16_t pulse_duration_msec; |
| 278 | } __attribute__((packed)); |
| 279 | |
| 280 | /* SIMTRACE_MSGT_DT_MODEM_SIM_SELECT */ |
| 281 | struct st_modem_sim_select { |
| 282 | /* remote (1), local (0) */ |
| 283 | uint8_t remote_sim; |
| 284 | } __attribute__((packed)); |
| 285 | |
| 286 | /* SIMTRACE_MSGT_BD_MODEM_STATUS */ |
| 287 | #define ST_MDM_STS_BIT_WWAN_LED (1 << 0) |
| 288 | #define ST_MDM_STS_BIT_CARD_INSERTED (1 << 1) |
| 289 | struct st_modem_status { |
| 290 | /* bit-field of supported status bits */ |
| 291 | uint8_t supported_mask; |
| 292 | /* bit-field of current status bits */ |
| 293 | uint8_t status_mask; |
| 294 | /* bit-field of changed status bits */ |
| 295 | uint8_t changed_mask; |
| 296 | } __attribute__((packed)); |
Kévin Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 297 | |
| 298 | /*********************************************************************** |
| 299 | * SNIFF |
| 300 | ***********************************************************************/ |
| 301 | |
| 302 | /* SIMTRACE_MSGT_SNIFF_CHANGE flags */ |
| 303 | #define SNIFF_CHANGE_FLAG_CARD_INSERT (1<<0) |
| 304 | #define SNIFF_CHANGE_FLAG_CARD_EJECT (1<<1) |
Kévin Redon | 8e84f81 | 2018-07-26 15:34:03 +0200 | [diff] [blame] | 305 | #define SNIFF_CHANGE_FLAG_RESET_ASSERT (1<<2) |
| 306 | #define SNIFF_CHANGE_FLAG_RESET_DEASSERT (1<<3) |
Kévin Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 307 | #define SNIFF_CHANGE_FLAG_TIMEOUT_WT (1<<4) |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 308 | /* SIMTRACE_MSGT_SNIFF_ATR, SIMTRACE_MSGT_SNIFF_PPS, SIMTRACE_MSGT_SNIFF_TPDU flags */ |
| 309 | #define SNIFF_DATA_FLAG_ERROR_INCOMPLETE (1<<5) |
| 310 | #define SNIFF_DATA_FLAG_ERROR_MALFORMED (1<<6) |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 311 | #define SNIFF_DATA_FLAG_ERROR_CHECKSUM (1<<7) |
Kévin Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 312 | |
| 313 | /* SIMTRACE_MSGT_SNIFF_CHANGE */ |
| 314 | struct sniff_change { |
| 315 | /* SIMTRACE_MSGT_SNIFF_CHANGE flags */ |
| 316 | uint32_t flags; |
| 317 | } __attribute__ ((packed)); |
| 318 | |
| 319 | /* SIMTRACE_MSGT_SNIFF_FIDI */ |
| 320 | struct sniff_fidi { |
| 321 | /* Fi/Di values as encoded in TA1 */ |
| 322 | uint8_t fidi; |
| 323 | } __attribute__ ((packed)); |
| 324 | |
| 325 | /* SIMTRACE_MSGT_SNIFF_ATR, SIMTRACE_MSGT_SNIFF_PPS, SIMTRACE_MSGT_SNIFF_TPDU */ |
| 326 | struct sniff_data { |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 327 | /* data flags */ |
| 328 | uint32_t flags; |
Kévin Redon | 012940f | 2018-07-01 18:21:30 +0200 | [diff] [blame] | 329 | /* data length */ |
| 330 | uint16_t length; |
| 331 | /* data */ |
| 332 | uint8_t data[0]; |
| 333 | } __attribute__ ((packed)); |