Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | a67be5f | 2020-09-03 10:04:36 +0200 | [diff] [blame^] | 2 | /* CCID Device handling |
| 3 | * |
| 4 | * (C) 2019-2020 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 21 | #include <stdbool.h> |
| 22 | #include <stdint.h> |
| 23 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 24 | #include "ccid_proto.h" |
Harald Welte | 0634836 | 2019-05-19 00:45:17 +0200 | [diff] [blame] | 25 | #include "logging.h" |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 26 | |
| 27 | #define NR_SLOTS 8 |
| 28 | |
| 29 | #define LOGPCI(ci, lvl, fmt, args ...) LOGP(DCCID, lvl, "%s: " fmt, (ci)->name, ## args) |
| 30 | #define LOGPCS(cs, lvl, fmt, args ...) \ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 31 | LOGP(DCCID, lvl, "%s(%u): " fmt, (cs)->ci->name, (cs)->slot_nr, ## args) |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 32 | |
| 33 | struct msgb; |
| 34 | |
| 35 | struct ccid_pars_decoded { |
| 36 | /* global for T0/T1 */ |
Harald Welte | 860d006 | 2019-05-18 11:18:39 +0200 | [diff] [blame] | 37 | uint32_t fi; |
| 38 | uint32_t di; |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 39 | enum ccid_clock_stop clock_stop; |
| 40 | bool inverse_convention; |
| 41 | |
| 42 | struct { |
| 43 | uint8_t guard_time_etu; |
| 44 | uint8_t waiting_integer; |
| 45 | } t0; |
| 46 | |
| 47 | struct { |
| 48 | enum ccid_t1_csum_type csum_type; |
| 49 | uint8_t guard_time_t1; |
| 50 | uint8_t bwi; |
| 51 | uint8_t cwi; |
| 52 | uint8_t ifsc; |
| 53 | uint8_t nad; |
| 54 | } t1; |
| 55 | }; |
| 56 | |
| 57 | struct ccid_slot { |
| 58 | /* back-pointer to the ccid_instance */ |
| 59 | struct ccid_instance *ci; |
| 60 | /* number of this slot (0 = first) */ |
| 61 | uint8_t slot_nr; |
| 62 | /* is there an ICC physically present (card detect)? */ |
| 63 | bool icc_present; |
| 64 | /* was there an ICC present during the last NotifSlotStatus? |
| 65 | * should be set to zero every USB resume and setConfig != 0 */ |
| 66 | bool icc_present_last; |
| 67 | /* is the ICC physically powered? */ |
| 68 | bool icc_powered; |
| 69 | /* is the ICC currently in reset? */ |
| 70 | bool icc_in_reset; |
| 71 | /* is this slot currently busy with processing a CCID command? */ |
| 72 | bool cmd_busy; |
| 73 | /* decided CCID parameters */ |
| 74 | struct ccid_pars_decoded pars; |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 75 | /* proposed CCID parameters */ |
| 76 | struct ccid_pars_decoded proposed_pars; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 77 | /* default parameters; applied on ResetParameters */ |
| 78 | const struct ccid_pars_decoded *default_pars; |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 79 | volatile uint32_t event; |
| 80 | volatile void* event_data; |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 83 | /* CCID operations provided by USB transport layer */ |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 84 | struct ccid_ops { |
Harald Welte | e6ac416 | 2019-05-16 11:18:00 +0200 | [diff] [blame] | 85 | /* msgb ownership in below functions is transferred, i.e. whoever |
| 86 | * provides the callback function must make sure to msgb_free() them |
| 87 | * once transmission on IN or INT EP has completed. */ |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 88 | int (*send_in)(struct ccid_instance *ci, struct msgb *msg); |
Harald Welte | a7da504 | 2019-05-16 11:08:35 +0200 | [diff] [blame] | 89 | int (*send_int)(struct ccid_instance *ci, struct msgb *msg); |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 92 | /* CCID operations provided by actual slot hardware */ |
| 93 | struct ccid_slot_ops { |
| 94 | /* called once on start-up for initialization */ |
| 95 | int (*init)(struct ccid_slot *cs); |
| 96 | /* called before processing any command for a slot; used e.g. to |
| 97 | * update the (power/clock/...) status from the hardware */ |
| 98 | void (*pre_proc_cb)(struct ccid_slot *cs, struct msgb *msg); |
| 99 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 100 | void (*icc_power_on_async)(struct ccid_slot *cs, struct msgb *msg, |
| 101 | const struct ccid_pc_to_rdr_icc_power_on *ipo); |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 102 | int (*xfr_block_async)(struct ccid_slot *cs, struct msgb *msg, |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 103 | const struct ccid_pc_to_rdr_xfr_block *xfb); |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 104 | void (*set_power)(struct ccid_slot *cs, bool enable); |
| 105 | void (*set_clock)(struct ccid_slot *cs, enum ccid_clock_command cmd); |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 106 | int (*set_params)(struct ccid_slot *cs, uint8_t seq, enum ccid_protocol_num proto, |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 107 | const struct ccid_pars_decoded *pars_dec); |
Eric Wild | 56a5055 | 2020-01-28 16:07:23 +0100 | [diff] [blame] | 108 | int (*set_rate_and_clock)(struct ccid_slot *cs, uint32_t* freq_hz, uint32_t* rate_bps); |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 109 | void (*icc_set_insertion_status)(struct ccid_slot *cs, bool present); |
Eric Wild | 759a646 | 2019-11-11 14:22:52 +0100 | [diff] [blame] | 110 | int (*handle_fsm_events)(struct ccid_slot *cs, bool enable); |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 113 | /* An instance of CCID (i.e. a card reader device) */ |
| 114 | struct ccid_instance { |
| 115 | /* slots within the reader */ |
| 116 | struct ccid_slot slot[NR_SLOTS]; |
| 117 | /* set of function pointers implementing specific operations */ |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 118 | const struct ccid_ops *ops; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 119 | const struct ccid_slot_ops *slot_ops; |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 120 | /* USB CCID Class Specific Descriptor */ |
| 121 | const struct usb_ccid_class_descriptor *class_desc; |
| 122 | /* array of permitted data rates; length: bNumDataRatesSupported */ |
| 123 | const uint32_t *data_rates; |
| 124 | /* array of permitted clock frequencies; length: bNumClockSupported */ |
| 125 | const uint32_t *clock_freqs; |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 126 | const char *name; |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 127 | /* user-supplied opaque data */ |
| 128 | void *priv; |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 131 | int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg); |
| 132 | int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg); |
| 133 | struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 134 | enum ccid_error_code err); |
| 135 | |
| 136 | struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 137 | enum ccid_error_code err, const uint8_t *data, |
| 138 | uint32_t data_len); |
| 139 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 140 | void ccid_instance_init(struct ccid_instance *ci, const struct ccid_ops *ops, |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 141 | const struct ccid_slot_ops *slot_ops, |
| 142 | const struct usb_ccid_class_descriptor *class_desc, |
| 143 | const uint32_t *data_rates, const uint32_t *clock_freqs, |
| 144 | const char *name, void *priv); |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 145 | int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg); |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 146 | struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 147 | enum ccid_error_code err); |
| 148 | struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 149 | enum ccid_error_code err); |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 150 | |
| 151 | /* Invalid request received: Please return STALL */ |
| 152 | #define CCID_CTRL_RET_INVALID -1 |
| 153 | /* Unknown request received: Not something CCID is supposed to handle */ |
| 154 | #define CCID_CTRL_RET_UNKNOWN 0 |
| 155 | /* Request OK. In case of a CTRL-IN: continue by sending wLength bytes to host */ |
| 156 | #define CCID_CTRL_RET_OK 1 |
| 157 | |
| 158 | int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in); |