Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1 | #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 */ |
| 26 | enum 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 Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 32 | CEMU_USB_MSGT_DT_CARDINSERT, /* insert/remove card */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 33 | |
| 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 */ |
| 43 | struct cardemu_usb_msg_hdr { |
| 44 | uint8_t msg_type; /* enum cardemu_usb_msg_type */ |
| 45 | uint8_t seq_nr; /* sequence number */ |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 46 | uint16_t msg_len; /* length of message including hdr */ |
| 47 | uint8_t data[0]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 48 | } __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 Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 59 | /* CEMU_USB_MSGT_DT_CARDINSERT */ |
| 60 | struct cardemu_usb_msg_cardinsert { |
| 61 | struct cardemu_usb_msg_hdr hdr; |
| 62 | uint8_t card_insert; |
| 63 | } __attribute__ ((packed)); |
| 64 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 65 | /* CEMU_USB_MSGT_DT_SET_ATR */ |
| 66 | struct cardemu_usb_msg_set_atr { |
| 67 | struct cardemu_usb_msg_hdr hdr; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 68 | uint8_t atr_len; |
| 69 | /* variable-length ATR data */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 70 | uint8_t atr[0]; |
| 71 | } __attribute__ ((packed)); |
| 72 | |
| 73 | /* CEMU_USB_MSGT_DT_TX_DATA */ |
| 74 | struct cardemu_usb_msg_tx_data { |
| 75 | struct cardemu_usb_msg_hdr hdr; |
| 76 | uint32_t flags; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 77 | uint16_t data_len; |
| 78 | /* variable-length TPDU data */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 79 | uint8_t data[0]; |
| 80 | } __attribute__ ((packed)); |
| 81 | |
| 82 | /* CEMU_USB_MSGT_DO_RX_DATA */ |
| 83 | struct cardemu_usb_msg_rx_data { |
| 84 | struct cardemu_usb_msg_hdr hdr; |
| 85 | uint32_t flags; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 86 | uint16_t data_len; |
| 87 | /* variable-length TPDU data */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 88 | 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 Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 95 | #define CEMU_STATUS_F_RESET_ACTIVE 0x00000010 |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 96 | |
| 97 | /* CEMU_USB_MSGT_DO_STATUS */ |
| 98 | struct 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 */ |
| 111 | struct cardemu_usb_msg_pts_info { |
| 112 | struct cardemu_usb_msg_hdr hdr; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 113 | uint8_t pts_len; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 114 | /* 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 */ |
| 121 | struct cardemu_usb_msg_error { |
| 122 | struct cardemu_usb_msg_hdr hdr; |
| 123 | uint8_t severity; |
| 124 | uint8_t subsystem; |
| 125 | uint16_t code; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 126 | uint8_t msg_len; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 127 | /* human-readable error message */ |
| 128 | uint8_t msg[0]; |
| 129 | } __attribute__ ((packed)); |
| 130 | |
| 131 | static 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 | } |