Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <assert.h> |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 5 | #include <string.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 6 | |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/utils.h> |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 9 | #include <osmocom/core/logging.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 10 | |
| 11 | #include "ccid_proto.h" |
Harald Welte | 8d186ad | 2019-05-15 19:40:29 +0200 | [diff] [blame] | 12 | #include "ccid_device.h" |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 13 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 14 | /* local, stand-alone definition of a USB control request */ |
| 15 | struct _usb_ctrl_req { |
| 16 | uint8_t bRequestType; |
| 17 | uint8_t bRequest; |
| 18 | uint16_t wValue; |
| 19 | uint16_t wIndex; |
| 20 | uint16_t wLength; |
| 21 | } __attribute__ ((packed));; |
| 22 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 23 | /* decode on-the-wire T0 parameters into their parsed form */ |
| 24 | static int decode_ccid_pars_t0(struct ccid_pars_decoded *out, const struct ccid_proto_data_t0 *in) |
| 25 | { |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 26 | /* input validation: only 0x00 and 0x02 permitted for bmTCCKST0 |
| 27 | * if (in->bmTCCKST0 & 0xFD) |
| 28 | * return -11; |
| 29 | * 7816-3 6.1.7 says: "Note: the CCID ignores this bit", placeholder for GETparameters */ |
| 30 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 31 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 32 | if (in->bClockStop & 0xFC) |
| 33 | return -14; |
| 34 | |
| 35 | out->fi = in->bmFindexDindex >> 4; |
| 36 | out->di = in->bmFindexDindex & 0xF; |
| 37 | if (in->bmTCCKST0 & 2) |
| 38 | out->inverse_convention = true; |
| 39 | else |
| 40 | out->inverse_convention = false; |
| 41 | if (in->bGuardTimeT0 == 0xff) |
| 42 | out->t0.guard_time_etu = 0; |
| 43 | else |
| 44 | out->t0.guard_time_etu = in->bGuardTimeT0; |
| 45 | out->t0.waiting_integer = in->bWaitingIntegerT0; |
| 46 | out->clock_stop = in->bClockStop & 0x03; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* encode T0 parameters from parsed form into on-the-wire encoding */ |
| 52 | static void encode_ccid_pars_t0(struct ccid_proto_data_t0 *out, const struct ccid_pars_decoded *in) |
| 53 | { |
| 54 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 55 | if (in->inverse_convention) |
| 56 | out->bmTCCKST0 = 0x02; |
| 57 | else |
| 58 | out->bmTCCKST0 = 0x00; |
| 59 | out->bGuardTimeT0 = in->t0.guard_time_etu; |
| 60 | out->bWaitingIntegerT0 = in->t0.waiting_integer; |
| 61 | out->bClockStop = in->clock_stop & 0x03; |
| 62 | } |
| 63 | |
| 64 | /* decode on-the-wire T1 parameters into their parsed form */ |
| 65 | static int decode_ccid_pars_t1(struct ccid_pars_decoded *out, const struct ccid_proto_data_t1 *in) |
| 66 | { |
| 67 | /* input validation: only some values permitted for bmTCCKST0 */ |
| 68 | if (in->bmTCCKST1 & 0xE8) |
| 69 | return -11; |
| 70 | /* input validation: only 0x00 to 0x9F permitted for bmWaitingIntegersT1 */ |
| 71 | if (in->bWaitingIntegersT1 > 0x9F) |
| 72 | return -13; |
| 73 | /* input validation: only 0x00 to 0x03 permitted for bClockSTop */ |
| 74 | if (in->bClockStop & 0xFC) |
| 75 | return -14; |
| 76 | /* input validation: only 0x00 to 0xFE permitted for bIFSC */ |
| 77 | if (in->bIFSC > 0xFE) |
| 78 | return -15; |
| 79 | |
| 80 | out->fi = in->bmFindexDindex >> 4; |
| 81 | out->di = in->bmFindexDindex & 0xF; |
| 82 | if (in->bmTCCKST1 & 1) |
| 83 | out->t1.csum_type = CCID_CSUM_TYPE_CRC; |
| 84 | else |
| 85 | out->t1.csum_type = CCID_CSUM_TYPE_LRC; |
| 86 | if (in->bmTCCKST1 & 2) |
| 87 | out->inverse_convention = true; |
| 88 | else |
| 89 | out->inverse_convention = false; |
| 90 | out->t1.guard_time_t1 = in->bGuardTimeT1; |
| 91 | out->t1.bwi = in->bWaitingIntegersT1 >> 4; |
| 92 | out->t1.cwi = in->bWaitingIntegersT1 & 0xF; |
| 93 | out->clock_stop = in->bClockStop & 0x03; |
| 94 | out->t1.ifsc = in->bIFSC; |
| 95 | out->t1.nad = in->bNadValue; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* encode T1 parameters from parsed form into on-the-wire encoding */ |
| 101 | static void encode_ccid_pars_t1(struct ccid_proto_data_t1 *out, const struct ccid_pars_decoded *in) |
| 102 | { |
| 103 | out->bmFindexDindex = ((in->fi << 4) & 0xF0) | (in->di & 0x0F); |
| 104 | out->bmTCCKST1 = 0x10; |
| 105 | if (in->t1.csum_type == CCID_CSUM_TYPE_CRC) |
| 106 | out->bmTCCKST1 |= 0x01; |
| 107 | if (in->inverse_convention) |
| 108 | out->bmTCCKST1 |= 0x02; |
| 109 | out->bGuardTimeT1 = in->t1.guard_time_t1; |
| 110 | out->bWaitingIntegersT1 = ((in->t1.bwi << 4) & 0xF0) | (in->t1.cwi & 0x0F); |
| 111 | out->bClockStop = in->clock_stop & 0x03; |
| 112 | out->bIFSC = in->t1.ifsc; |
| 113 | out->bNadValue = in->t1.nad; |
| 114 | } |
| 115 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 116 | #define msgb_ccid_out(x) (union ccid_pc_to_rdr *)msgb_data(x) |
| 117 | #define msgb_ccid_in(x) (union ccid_rdr_to_pc *)msgb_data(x) |
| 118 | |
| 119 | static struct ccid_slot *get_ccid_slot(struct ccid_instance *ci, uint8_t slot_nr) |
| 120 | { |
| 121 | if (slot_nr >= sizeof(ci->slot)) |
| 122 | return NULL; |
| 123 | else |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 124 | return &ci->slot[slot_nr]; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static uint8_t get_icc_status(const struct ccid_slot *cs) |
| 128 | { |
| 129 | if (cs->icc_present && cs->icc_powered && !cs->icc_in_reset) |
| 130 | return CCID_ICC_STATUS_PRES_ACT; |
| 131 | else if (!cs->icc_present) |
| 132 | return CCID_ICC_STATUS_NO_ICC; |
| 133 | else |
| 134 | return CCID_ICC_STATUS_PRES_INACT; |
| 135 | } |
| 136 | |
| 137 | #define SET_HDR(x, msg_type, slot, seq) do { \ |
| 138 | (x)->hdr.bMessageType = msg_type; \ |
| 139 | (x)->hdr.dwLength = 0; \ |
| 140 | (x)->hdr.bSlot = slot; \ |
| 141 | (x)->hdr.bSeq = seq; \ |
| 142 | } while (0) |
| 143 | |
| 144 | #define SET_HDR_IN(x, msg_type, slot, seq, status, error) do { \ |
| 145 | SET_HDR(&(x)->hdr, msg_type, slot, seq); \ |
| 146 | (x)->hdr.bStatus = status; \ |
| 147 | (x)->hdr.bError = error; \ |
| 148 | } while (0) |
| 149 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 150 | #if 0 |
| 151 | static uint8_t ccid_pc_to_rdr_get_seq(const struct ccid_pc_to_rdr *u) |
| 152 | { |
| 153 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 154 | return ch->bSeq; |
| 155 | } |
| 156 | #endif |
| 157 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 158 | /*********************************************************************** |
| 159 | * Message generation / sending |
| 160 | ***********************************************************************/ |
| 161 | |
| 162 | static struct msgb *ccid_msgb_alloc(void) |
| 163 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 164 | struct msgb *msg = msgb_alloc(512, "ccid"); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 165 | OSMO_ASSERT(msg); |
| 166 | return msg; |
| 167 | } |
| 168 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 169 | /* Send given CCID message */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 170 | static int ccid_send(struct ccid_instance *ci, struct msgb *msg) |
| 171 | { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 172 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
| 173 | struct ccid_slot *cs = get_ccid_slot(ci, ch->bSlot); |
| 174 | if (cs) { |
| 175 | LOGPCS(cs, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 176 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 177 | } else { |
| 178 | LOGPCI(ci, LOGL_DEBUG, "Tx CCID(IN) %s %s\n", |
| 179 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 180 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 181 | return ci->ops->send_in(ci, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 184 | /* Send given CCID message for given slot; patch bSlot into message */ |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 185 | int ccid_slot_send(struct ccid_slot *cs, struct msgb *msg) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 186 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 187 | struct ccid_header *ch = (struct ccid_header *) msgb_ccid_in(msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 188 | |
| 189 | /* patch bSlotNr into message */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 190 | ch->bSlot = cs->slot_nr; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 191 | return ccid_send(cs->ci, msg); |
| 192 | } |
| 193 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 194 | /* Send given CCID message and mark slot as un-busy */ |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 195 | int ccid_slot_send_unbusy(struct ccid_slot *cs, struct msgb *msg) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 196 | { |
| 197 | cs->cmd_busy = false; |
| 198 | return ccid_slot_send(cs, msg); |
| 199 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 200 | |
| 201 | /* Section 6.2.1 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 202 | static struct msgb *ccid_gen_data_block_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 203 | uint8_t cmd_sts, enum ccid_error_code err, |
| 204 | const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 205 | { |
| 206 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 207 | struct ccid_rdr_to_pc_data_block *db = |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 208 | (struct ccid_rdr_to_pc_data_block *) msgb_put(msg, sizeof(*db) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 209 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 210 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 211 | SET_HDR_IN(db, RDR_to_PC_DataBlock, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 212 | osmo_store32le(data_len, &db->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 213 | memcpy(db->abData, data, data_len); |
| 214 | return msg; |
| 215 | } |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 216 | struct msgb *ccid_gen_data_block(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 217 | enum ccid_error_code err, const uint8_t *data, |
| 218 | uint32_t data_len) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 219 | { |
| 220 | return ccid_gen_data_block_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 221 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 222 | |
| 223 | /* Section 6.2.2 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 224 | static struct msgb *ccid_gen_slot_status_nr(uint8_t slot_nr, uint8_t icc_status, |
| 225 | uint8_t seq, uint8_t cmd_sts, |
| 226 | enum ccid_error_code err) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 227 | { |
| 228 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 229 | struct ccid_rdr_to_pc_slot_status *ss = |
| 230 | (struct ccid_rdr_to_pc_slot_status *) msgb_put(msg, sizeof(*ss)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 231 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 232 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 233 | SET_HDR_IN(ss, RDR_to_PC_SlotStatus, slot_nr, seq, sts, err); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 234 | return msg; |
| 235 | } |
Harald Welte | 005b09d | 2019-05-16 17:24:29 +0200 | [diff] [blame] | 236 | struct msgb *ccid_gen_slot_status(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 237 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 238 | { |
| 239 | return ccid_gen_slot_status_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err); |
| 240 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 241 | |
| 242 | /* Section 6.2.3 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 243 | static struct msgb *ccid_gen_parameters_t0_nr(uint8_t slot_nr, uint8_t icc_status, |
| 244 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 245 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 246 | { |
| 247 | struct msgb *msg = ccid_msgb_alloc(); |
| 248 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 249 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t0)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 250 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 251 | |
| 252 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 253 | if (dec_par) { |
| 254 | osmo_store32le(sizeof(par->abProtocolData.t0), &par->hdr.hdr.dwLength); |
| 255 | encode_ccid_pars_t0(&par->abProtocolData.t0, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 256 | } |
| 257 | return msg; |
| 258 | } |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 259 | struct msgb *ccid_gen_parameters_t0(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 260 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 261 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 262 | return ccid_gen_parameters_t0_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static struct msgb *ccid_gen_parameters_t1_nr(uint8_t slot_nr, uint8_t icc_status, |
| 266 | uint8_t seq, uint8_t cmd_sts, enum ccid_error_code err, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 267 | const struct ccid_pars_decoded *dec_par) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 268 | { |
| 269 | struct msgb *msg = ccid_msgb_alloc(); |
| 270 | struct ccid_rdr_to_pc_parameters *par = |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 271 | (struct ccid_rdr_to_pc_parameters *) msgb_put(msg, sizeof(par->hdr)+sizeof(par->abProtocolData.t1)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 272 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
| 273 | |
| 274 | SET_HDR_IN(par, RDR_to_PC_Parameters, slot_nr, seq, sts, err); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 275 | if (dec_par) { |
| 276 | osmo_store32le(sizeof(par->abProtocolData.t1), &par->hdr.hdr.dwLength); |
| 277 | encode_ccid_pars_t1(&par->abProtocolData.t1, dec_par); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 278 | } |
| 279 | return msg; |
| 280 | } |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 281 | struct msgb *ccid_gen_parameters_t1(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 282 | enum ccid_error_code err) |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 283 | { |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 284 | return ccid_gen_parameters_t1_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, &cs->pars); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 287 | |
| 288 | /* Section 6.2.4 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 289 | static struct msgb *ccid_gen_escape_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, uint8_t cmd_sts, |
| 290 | enum ccid_error_code err, const uint8_t *data, uint32_t data_len) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 291 | { |
| 292 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 293 | struct ccid_rdr_to_pc_escape *esc = |
| 294 | (struct ccid_rdr_to_pc_escape *) msgb_put(msg, sizeof(*esc) + data_len); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 295 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 296 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 297 | SET_HDR_IN(esc, RDR_to_PC_Escape, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 298 | osmo_store32le(data_len, &esc->hdr.hdr.dwLength); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 299 | memcpy(esc->abData, data, data_len); |
| 300 | return msg; |
| 301 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 302 | static struct msgb *ccid_gen_escape(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 303 | enum ccid_error_code err, const uint8_t *data, |
| 304 | uint32_t data_len) |
| 305 | { |
| 306 | return ccid_gen_escape_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, data, data_len); |
| 307 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 308 | |
| 309 | /* Section 6.2.5 */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 310 | static struct msgb *ccid_gen_clock_and_rate_nr(uint8_t slot_nr, uint8_t icc_status, uint8_t seq, |
| 311 | uint8_t cmd_sts, enum ccid_error_code err, |
| 312 | uint32_t clock_khz, uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 313 | { |
| 314 | struct msgb *msg = ccid_msgb_alloc(); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 315 | struct ccid_rdr_to_pc_data_rate_and_clock *drc = |
| 316 | (struct ccid_rdr_to_pc_data_rate_and_clock *) msgb_put(msg, sizeof(*drc)); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 317 | uint8_t sts = (cmd_sts & CCID_CMD_STATUS_MASK) | icc_status; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 318 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 319 | SET_HDR_IN(drc, RDR_to_PC_DataRateAndClockFrequency, slot_nr, seq, sts, err); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 320 | osmo_store32le(8, &drc->hdr.hdr.dwLength); /* Message-specific data length (wtf?) */ |
| 321 | osmo_store32le(clock_khz, &drc->dwClockFrequency); /* kHz */ |
| 322 | osmo_store32le(rate_bps, &drc->dwDataRate); /* bps */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 323 | return msg; |
| 324 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 325 | static struct msgb *ccid_gen_clock_and_rate(struct ccid_slot *cs, uint8_t seq, uint8_t cmd_sts, |
| 326 | enum ccid_error_code err, uint32_t clock_khz, |
| 327 | uint32_t rate_bps) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 328 | { |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 329 | return ccid_gen_clock_and_rate_nr(cs->slot_nr, get_icc_status(cs), seq, cmd_sts, err, |
| 330 | clock_khz, rate_bps); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 331 | } |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 332 | |
| 333 | /*! generate an error response for given input message_type/slot_nr/seq |
| 334 | * \param[in] msg_type CCID Message Type against which response is to be created |
| 335 | * \param[in] slot_nr CCID Slot Number |
| 336 | * \param[in] icc_status ICC Status of the slot |
| 337 | * \param[in] seq CCID Sequence number |
| 338 | * \param[in] err_code CCID Error Code to send |
| 339 | * \returns dynamically-allocated message buffer containing error response */ |
| 340 | static struct msgb *gen_err_resp(enum ccid_msg_type msg_type, uint8_t slot_nr, uint8_t icc_status, |
| 341 | uint8_t seq, enum ccid_error_code err_code) |
| 342 | { |
| 343 | struct msgb *resp = NULL; |
| 344 | |
| 345 | switch (msg_type) { |
| 346 | case PC_to_RDR_IccPowerOn: |
| 347 | case PC_to_RDR_XfrBlock: |
| 348 | case PC_to_RDR_Secure: |
| 349 | /* Return RDR_to_PC_DataBlock */ |
| 350 | resp = ccid_gen_data_block_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 351 | err_code, NULL, 0); |
| 352 | break; |
| 353 | |
| 354 | case PC_to_RDR_IccPowerOff: |
| 355 | case PC_to_RDR_GetSlotStatus: |
| 356 | case PC_to_RDR_IccClock: |
| 357 | case PC_to_RDR_T0APDU: |
| 358 | case PC_to_RDR_Mechanical: |
| 359 | case PC_to_RDR_Abort: |
| 360 | /* Return RDR_to_PC_SlotStatus */ |
| 361 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 362 | err_code); |
| 363 | break; |
| 364 | |
| 365 | case PC_to_RDR_GetParameters: |
| 366 | case PC_to_RDR_ResetParameters: |
| 367 | case PC_to_RDR_SetParameters: |
| 368 | /* Return RDR_to_PC_Parameters */ |
| 369 | resp = ccid_gen_parameters_t0_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 370 | err_code, NULL); /* FIXME: parameters? */ |
| 371 | break; |
| 372 | |
| 373 | case PC_to_RDR_Escape: |
| 374 | /* Return RDR_to_PC_Escape */ |
| 375 | resp = ccid_gen_escape_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 376 | err_code, NULL, 0); |
| 377 | break; |
| 378 | |
| 379 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 380 | /* Return RDR_to_PC_SlotStatus */ |
| 381 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 382 | err_code); |
| 383 | break; |
| 384 | |
| 385 | default: |
| 386 | /* generate general error */ |
| 387 | resp = ccid_gen_slot_status_nr(slot_nr, icc_status, seq, CCID_CMD_STATUS_FAILED, |
| 388 | CCID_ERR_CMD_NOT_SUPPORTED); |
| 389 | break; |
| 390 | } |
| 391 | return resp; |
| 392 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 393 | |
| 394 | /*********************************************************************** |
| 395 | * Message reception / parsing |
| 396 | ***********************************************************************/ |
| 397 | |
| 398 | /* Section 6.1.3 */ |
| 399 | static int ccid_handle_get_slot_status(struct ccid_slot *cs, struct msgb *msg) |
| 400 | { |
| 401 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 402 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 403 | uint8_t seq = u->get_slot_status.hdr.bSeq; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 404 | struct msgb *resp; |
| 405 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 406 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 407 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 408 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
| 412 | /* Section 6.1.1 */ |
| 413 | static int ccid_handle_icc_power_on(struct ccid_slot *cs, struct msgb *msg) |
| 414 | { |
| 415 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 416 | const struct ccid_header *ch = (const struct ccid_header *) u; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 417 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 418 | /* handle this asynchronously */ |
| 419 | cs->ci->slot_ops->icc_power_on_async(cs, msg, &u->icc_power_on); |
| 420 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* Section 6.1.2 */ |
| 424 | static int ccid_handle_icc_power_off(struct ccid_slot *cs, struct msgb *msg) |
| 425 | { |
| 426 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 427 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 428 | uint8_t seq = u->icc_power_off.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 429 | struct msgb *resp; |
| 430 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 431 | cs->ci->slot_ops->set_power(cs, false); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 432 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 433 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Section 6.1.4 */ |
| 437 | static int ccid_handle_xfr_block(struct ccid_slot *cs, struct msgb *msg) |
| 438 | { |
| 439 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 440 | const struct ccid_header *ch = (const struct ccid_header *) u; |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 441 | struct msgb *resp; |
| 442 | int rc; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 443 | |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 444 | /* handle this asynchronously */ |
Eric Wild | 9e622dc | 2019-11-27 14:43:16 +0100 | [diff] [blame] | 445 | rc = cs->ci->slot_ops->xfr_block_async(cs, msg, &u->xfr_block); |
| 446 | if (rc < 0) { |
| 447 | msgb_trim(msg, sizeof(struct ccid_rdr_to_pc_data_block)); |
| 448 | resp = ccid_gen_data_block(cs, u->xfr_block.hdr.bSeq, CCID_CMD_STATUS_FAILED, -rc, 0, 0); |
| 449 | goto out; |
| 450 | } |
| 451 | /* busy */ |
| 452 | return 1; |
| 453 | out: |
| 454 | ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 505d441 | 2019-05-16 17:26:09 +0200 | [diff] [blame] | 455 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* Section 6.1.5 */ |
| 459 | static int ccid_handle_get_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 460 | { |
| 461 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 462 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 463 | uint8_t seq = u->get_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 464 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 465 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 466 | /* FIXME: T=1 */ |
| 467 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_OK, 0); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 468 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* Section 6.1.6 */ |
| 472 | static int ccid_handle_reset_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 473 | { |
| 474 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 475 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 476 | uint8_t seq = u->reset_parameters.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 477 | struct msgb *resp; |
Eric Wild | 9155231 | 2019-11-18 13:57:11 +0100 | [diff] [blame] | 478 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 479 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 480 | /* copy default parameters from somewhere */ |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 481 | /* FIXME: T=1 */ |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 482 | |
Eric Wild | 9155231 | 2019-11-18 13:57:11 +0100 | [diff] [blame] | 483 | /* validate parameters; abort if they are not supported */ |
| 484 | rc = cs->ci->slot_ops->set_params(cs, seq, CCID_PROTOCOL_NUM_T0, cs->default_pars); |
| 485 | if (rc < 0) { |
| 486 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
| 487 | goto out; |
| 488 | } |
| 489 | |
| 490 | msgb_free(msg); |
| 491 | /* busy, tdpu like callback */ |
| 492 | return 1; |
| 493 | out: |
| 494 | msgb_free(msg); |
| 495 | ccid_slot_send_unbusy(cs, resp); |
| 496 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* Section 6.1.7 */ |
| 500 | static int ccid_handle_set_parameters(struct ccid_slot *cs, struct msgb *msg) |
| 501 | { |
| 502 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 503 | const struct ccid_pc_to_rdr_set_parameters *spar = &u->set_parameters; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 504 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 505 | uint8_t seq = u->set_parameters.hdr.bSeq; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 506 | struct ccid_pars_decoded pars_dec; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 507 | struct msgb *resp; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 508 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 509 | |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 510 | switch (spar->bProtocolNum) { |
| 511 | case CCID_PROTOCOL_NUM_T0: |
| 512 | rc = decode_ccid_pars_t0(&pars_dec, &spar->abProtocolData.t0); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 513 | break; |
| 514 | case CCID_PROTOCOL_NUM_T1: |
| 515 | rc = decode_ccid_pars_t1(&pars_dec, &spar->abProtocolData.t1); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 516 | break; |
| 517 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 518 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Invalid Protocol 0x%02x\n",spar->bProtocolNum); |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 519 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, 0); |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | if (rc < 0) { |
| 524 | LOGP(DCCID, LOGL_ERROR, "SetParameters: Unable to parse: %d\n", rc); |
| 525 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
| 526 | goto out; |
| 527 | } |
| 528 | |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 529 | cs->proposed_pars = pars_dec; |
| 530 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 531 | /* validate parameters; abort if they are not supported */ |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 532 | rc = cs->ci->slot_ops->set_params(cs, seq, spar->bProtocolNum, &pars_dec); |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 533 | if (rc < 0) { |
| 534 | resp = ccid_gen_parameters_t0(cs, seq, CCID_CMD_STATUS_FAILED, -rc); |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 535 | goto out; |
Harald Welte | 8579ce5 | 2019-05-15 12:22:24 +0200 | [diff] [blame] | 536 | } |
Eric Wild | 9155231 | 2019-11-18 13:57:11 +0100 | [diff] [blame] | 537 | |
| 538 | msgb_free(msg); |
Eric Wild | ad1edce | 2019-11-27 16:51:08 +0100 | [diff] [blame] | 539 | /* busy, tdpu like callback */ |
| 540 | return 1; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 541 | out: |
Eric Wild | 9155231 | 2019-11-18 13:57:11 +0100 | [diff] [blame] | 542 | msgb_free(msg); |
| 543 | ccid_slot_send_unbusy(cs, resp); |
| 544 | return 1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* Section 6.1.8 */ |
| 548 | static int ccid_handle_escape(struct ccid_slot *cs, struct msgb *msg) |
| 549 | { |
| 550 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 551 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 552 | uint8_t seq = u->escape.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 553 | struct msgb *resp; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 554 | |
| 555 | resp = ccid_gen_escape(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED, NULL, 0); |
| 556 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* Section 6.1.9 */ |
| 560 | static int ccid_handle_icc_clock(struct ccid_slot *cs, struct msgb *msg) |
| 561 | { |
| 562 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 563 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 564 | uint8_t seq = u->icc_clock.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 565 | struct msgb *resp; |
| 566 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 567 | cs->ci->slot_ops->set_clock(cs, u->icc_clock.bClockCommand); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 568 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 569 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* Section 6.1.10 */ |
| 573 | static int ccid_handle_t0apdu(struct ccid_slot *cs, struct msgb *msg) |
| 574 | { |
| 575 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 576 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 577 | uint8_t seq = u->t0apdu.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 578 | struct msgb *resp; |
| 579 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 580 | /* FIXME: Required for APDU level exchange */ |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 581 | //resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 582 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 583 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Section 6.1.11 */ |
| 587 | static int ccid_handle_secure(struct ccid_slot *cs, struct msgb *msg) |
| 588 | { |
| 589 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 590 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 591 | uint8_t seq = u->secure.hdr.bSeq; |
| 592 | struct msgb *resp; |
| 593 | |
| 594 | /* FIXME */ |
| 595 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 596 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* Section 6.1.12 */ |
| 600 | static int ccid_handle_mechanical(struct ccid_slot *cs, struct msgb *msg) |
| 601 | { |
| 602 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 603 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 604 | uint8_t seq = u->mechanical.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 605 | struct msgb *resp; |
| 606 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 607 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 608 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* Section 6.1.13 */ |
| 612 | static int ccid_handle_abort(struct ccid_slot *cs, struct msgb *msg) |
| 613 | { |
| 614 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 615 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 616 | uint8_t seq = u->abort.hdr.bSeq; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 617 | struct msgb *resp; |
| 618 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 619 | /* Check if the currently in-progress message is Abortable */ |
| 620 | switch (0/* FIXME */) { |
| 621 | case PC_to_RDR_IccPowerOn: |
| 622 | case PC_to_RDR_XfrBlock: |
| 623 | case PC_to_RDR_Escape: |
| 624 | case PC_to_RDR_Secure: |
| 625 | case PC_to_RDR_Mechanical: |
| 626 | //case PC_to_RDR_Abort: /* seriously? WTF! */ |
| 627 | break; |
| 628 | default: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 629 | LOGP(DCCID, LOGL_ERROR, "Abort for non-Abortable Message Type\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 630 | /* CCID spec lists CMD_NOT_ABORTED, but gives no numberic value ?!? */ |
| 631 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_FAILED, CCID_ERR_CMD_NOT_SUPPORTED); |
| 632 | return ccid_slot_send_unbusy(cs, resp); |
| 633 | } |
| 634 | |
| 635 | /* FIXME */ |
| 636 | resp = ccid_gen_slot_status(cs, seq, CCID_CMD_STATUS_OK, 0); |
| 637 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /* Section 6.1.14 */ |
| 641 | static int ccid_handle_set_rate_and_clock(struct ccid_slot *cs, struct msgb *msg) |
| 642 | { |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 643 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 644 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 645 | uint8_t seq = u->set_rate_and_clock.hdr.bSeq; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 646 | uint32_t freq_hz = osmo_load32le(&u->set_rate_and_clock.dwClockFrequency); |
| 647 | uint32_t rate_bps = osmo_load32le(&u->set_rate_and_clock.dwDataRate); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 648 | struct msgb *resp; |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 649 | int rc; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 650 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 651 | /* FIXME: which rate to return in failure case? */ |
| 652 | rc = cs->ci->slot_ops->set_rate_and_clock(cs, freq_hz, rate_bps); |
| 653 | if (rc < 0) |
| 654 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_FAILED, -rc, 9600, 2500000); |
| 655 | else |
| 656 | resp = ccid_gen_clock_and_rate(cs, seq, CCID_CMD_STATUS_OK, 0, rate_bps, freq_hz); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 657 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 660 | /*! Handle data arriving from the host on the OUT endpoint. |
| 661 | * \param[in] cs CCID Instance on which to operate |
Harald Welte | e6ac416 | 2019-05-16 11:18:00 +0200 | [diff] [blame] | 662 | * \param[in] msgb received message buffer containing one CCID OUT EP message from the host. |
| 663 | * Ownership of message buffer is transferred, i.e. it's our job to msgb_free() |
| 664 | * it eventually, after we're done with it (could be asynchronously). |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 665 | * \returns 0 on success; negative on error */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 666 | int ccid_handle_out(struct ccid_instance *ci, struct msgb *msg) |
| 667 | { |
| 668 | const union ccid_pc_to_rdr *u = msgb_ccid_out(msg); |
| 669 | const struct ccid_header *ch = (const struct ccid_header *) u; |
| 670 | unsigned int len = msgb_length(msg); |
| 671 | struct ccid_slot *cs; |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 672 | struct msgb *resp; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 673 | int rc; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 674 | |
| 675 | if (len < sizeof(*ch)) { |
| 676 | /* FIXME */ |
Harald Welte | 1b3fb63 | 2019-11-12 06:45:48 +0100 | [diff] [blame] | 677 | msgb_free(msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 678 | return -1; |
| 679 | } |
| 680 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 681 | /* Check for invalid slot number */ |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 682 | cs = get_ccid_slot(ci, ch->bSlot); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 683 | if (!cs) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 684 | LOGPCI(ci, LOGL_ERROR, "Invalid bSlot %u\n", ch->bSlot); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 685 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, 5); |
Harald Welte | 1b3fb63 | 2019-11-12 06:45:48 +0100 | [diff] [blame] | 686 | msgb_free(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 687 | return ccid_send(ci, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 690 | /* Check if slot is already busy; Reject any additional commands meanwhile */ |
| 691 | if (cs->cmd_busy) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 692 | LOGPCS(cs, LOGL_ERROR, "Slot Busy, but another cmd received\n"); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 693 | /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */ |
| 694 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq, |
| 695 | CCID_ERR_CMD_SLOT_BUSY); |
Harald Welte | 1b3fb63 | 2019-11-12 06:45:48 +0100 | [diff] [blame] | 696 | msgb_free(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 697 | return ccid_send(ci, resp); |
| 698 | } |
| 699 | |
Eric Wild | 9155231 | 2019-11-18 13:57:11 +0100 | [diff] [blame] | 700 | if(!cs->icc_present) { |
| 701 | LOGPCS(cs, LOGL_ERROR, "No icc present, but another cmd received\n"); |
| 702 | /* FIXME: ABORT logic as per section 5.3.1 of CCID Spec v1.1 */ |
| 703 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, get_icc_status(cs), ch->bSeq, |
| 704 | CCID_ERR_ICC_MUTE); |
| 705 | msgb_free(msg); |
| 706 | return ccid_send(ci, resp); |
| 707 | } |
| 708 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 709 | LOGPCS(cs, LOGL_DEBUG, "Rx CCID(OUT) %s %s\n", |
| 710 | get_value_string(ccid_msg_type_vals, ch->bMessageType), msgb_hexdump(msg)); |
| 711 | |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 712 | /* we're now processing a command for the slot; mark slot as busy */ |
| 713 | cs->cmd_busy = true; |
| 714 | |
| 715 | /* TODO: enqueue into the per-slot specific input queue */ |
| 716 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 717 | /* call pre-processing call-back function; allows reader to update state */ |
| 718 | if (ci->slot_ops->pre_proc_cb) |
| 719 | ci->slot_ops->pre_proc_cb(cs, msg); |
| 720 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 721 | switch (ch->bMessageType) { |
| 722 | case PC_to_RDR_GetSlotStatus: |
Eric Wild | 33403af | 2019-10-01 15:01:58 +0200 | [diff] [blame] | 723 | if (len < sizeof(u->get_slot_status)) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 724 | goto short_msg; |
| 725 | rc = ccid_handle_get_slot_status(cs, msg); |
| 726 | break; |
| 727 | case PC_to_RDR_IccPowerOn: |
| 728 | if (len != sizeof(u->icc_power_on)) |
| 729 | goto short_msg; |
| 730 | rc = ccid_handle_icc_power_on(cs, msg); |
| 731 | break; |
| 732 | case PC_to_RDR_IccPowerOff: |
| 733 | if (len != sizeof(u->icc_power_off)) |
| 734 | goto short_msg; |
| 735 | rc = ccid_handle_icc_power_off(cs, msg); |
| 736 | break; |
| 737 | case PC_to_RDR_XfrBlock: |
| 738 | if (len < sizeof(u->xfr_block)) |
| 739 | goto short_msg; |
| 740 | rc = ccid_handle_xfr_block(cs, msg); |
| 741 | break; |
| 742 | case PC_to_RDR_GetParameters: |
| 743 | if (len != sizeof(u->get_parameters)) |
| 744 | goto short_msg; |
| 745 | rc = ccid_handle_get_parameters(cs, msg); |
| 746 | break; |
| 747 | case PC_to_RDR_ResetParameters: |
| 748 | if (len != sizeof(u->reset_parameters)) |
| 749 | goto short_msg; |
| 750 | rc = ccid_handle_reset_parameters(cs, msg); |
| 751 | break; |
| 752 | case PC_to_RDR_SetParameters: |
Eric Wild | 33403af | 2019-10-01 15:01:58 +0200 | [diff] [blame] | 753 | // smallest union member |
| 754 | if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10)) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 755 | goto short_msg; |
| 756 | rc = ccid_handle_set_parameters(cs, msg); |
| 757 | break; |
| 758 | case PC_to_RDR_Escape: |
| 759 | if (len < sizeof(u->escape)) |
| 760 | goto short_msg; |
| 761 | rc = ccid_handle_escape(cs, msg); |
| 762 | break; |
| 763 | case PC_to_RDR_IccClock: |
| 764 | if (len != sizeof(u->icc_clock)) |
| 765 | goto short_msg; |
| 766 | rc = ccid_handle_icc_clock(cs, msg); |
| 767 | break; |
| 768 | case PC_to_RDR_T0APDU: |
| 769 | if (len != /*FIXME*/ sizeof(u->t0apdu)) |
| 770 | goto short_msg; |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 771 | rc = ccid_handle_t0apdu(cs, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 772 | break; |
| 773 | case PC_to_RDR_Secure: |
| 774 | if (len < sizeof(u->secure)) |
| 775 | goto short_msg; |
| 776 | rc = ccid_handle_secure(cs, msg); |
| 777 | break; |
| 778 | case PC_to_RDR_Mechanical: |
| 779 | if (len != sizeof(u->mechanical)) |
| 780 | goto short_msg; |
| 781 | rc = ccid_handle_mechanical(cs, msg); |
| 782 | break; |
| 783 | case PC_to_RDR_Abort: |
| 784 | if (len != sizeof(u->abort)) |
| 785 | goto short_msg; |
| 786 | rc = ccid_handle_abort(cs, msg); |
| 787 | break; |
| 788 | case PC_to_RDR_SetDataRateAndClockFrequency: |
| 789 | if (len != sizeof(u->set_rate_and_clock)) |
| 790 | goto short_msg; |
| 791 | rc = ccid_handle_set_rate_and_clock(cs, msg); |
| 792 | break; |
| 793 | default: |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 794 | /* generic response with bERror = 0 (command not supported) */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 795 | LOGP(DCCID, LOGL_NOTICE, "Unknown CCID Message received: 0x%02x\n", ch->bMessageType); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 796 | resp = gen_err_resp(ch->bMessageType, ch->bSlot, CCID_ICC_STATUS_NO_ICC, ch->bSeq, |
| 797 | CCID_ERR_CMD_NOT_SUPPORTED); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 798 | msgb_free(msg); |
Harald Welte | 8772f58 | 2019-05-14 16:34:47 +0200 | [diff] [blame] | 799 | return ccid_slot_send_unbusy(cs, resp); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 800 | } |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 801 | /* the various ccid_handle_* functions can return '1' to tell us that they took ownership |
| 802 | * of the msgb */ |
| 803 | if (rc != 1) |
| 804 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 805 | return 0; |
| 806 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 807 | short_msg: |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 808 | LOGP(DCCID, LOGL_ERROR, "Short CCID message received: %s; ignoring\n", msgb_hexdump(msg)); |
Harald Welte | 922ff93 | 2019-05-16 11:21:51 +0200 | [diff] [blame] | 809 | msgb_free(msg); |
Harald Welte | 92e7c0b | 2019-05-14 09:32:10 +0200 | [diff] [blame] | 810 | return -1; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 811 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 812 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 813 | /* Section 5.3.1 ABORT */ |
| 814 | static int ccid_handle_ctrl_abort(struct ccid_instance *ci, const struct _usb_ctrl_req *req) |
| 815 | { |
| 816 | uint16_t w_value = osmo_load16le(&req->wValue); |
| 817 | uint8_t slot_nr = w_value & 0xff; |
| 818 | uint8_t seq = w_value >> 8; |
| 819 | struct ccid_slot *cs; |
| 820 | |
| 821 | if (slot_nr >= ARRAY_SIZE(ci->slot)) |
| 822 | return CCID_CTRL_RET_INVALID; |
| 823 | |
| 824 | cs = &ci->slot[slot_nr]; |
| 825 | |
| 826 | LOGP(DCCID, LOGL_NOTICE, "Not handling PC_to_RDR_Abort; please implement it\n"); |
| 827 | /* Upon receiving the Control pipe ABORT request the CCID should check |
| 828 | * the state of the requested slot. */ |
| 829 | |
| 830 | /* If the last Bulk-OUT message received by the CCID was a |
| 831 | * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT |
| 832 | * request, then the CCID will respond to the Bulk-OUT message with |
| 833 | * the RDR_to_PC_SlotStatus response. */ |
| 834 | |
| 835 | /* FIXME */ |
| 836 | |
| 837 | /* If the previous Bulk-OUT message received by the CCID was not a |
| 838 | * PC_to_RDR_Abort command with the same bSlot and bSeq as the ABORT |
| 839 | * request, then the CCID will fail all Bulk-Out commands to that slot |
| 840 | * until the PC_to_RDR_Abort command with the same bSlot and bSeq is |
| 841 | * received. Bulk-OUT commands will be failed by sending a response |
| 842 | * with bmCommandStatus=Failed and bError=CMD_ABORTED. */ |
| 843 | |
| 844 | /* FIXME */ |
| 845 | return CCID_CTRL_RET_OK; |
| 846 | } |
| 847 | |
| 848 | /* Section 5.3.2 GET_CLOCK_FREQUENCIES */ |
| 849 | static int ccid_handle_ctrl_get_clock_freq(struct ccid_instance *ci, const struct _usb_ctrl_req *req, |
| 850 | const uint8_t **data_in) |
| 851 | { |
| 852 | uint16_t len = osmo_load16le(&req->wLength); |
| 853 | |
| 854 | if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported) |
| 855 | return CCID_CTRL_RET_INVALID; |
| 856 | |
| 857 | *data_in = (const uint8_t *) ci->clock_freqs; |
| 858 | return CCID_CTRL_RET_OK; |
| 859 | } |
| 860 | |
| 861 | /* Section 5.3.3 GET_DATA_RATES */ |
| 862 | static int ccid_handle_ctrl_get_data_rates(struct ccid_instance *ci, const struct _usb_ctrl_req *req, |
| 863 | const uint8_t **data_in) |
| 864 | { |
| 865 | uint16_t len = osmo_load16le(&req->wLength); |
| 866 | |
| 867 | if (len != sizeof(uint32_t) * ci->class_desc->bNumClockSupported) |
| 868 | return CCID_CTRL_RET_INVALID; |
| 869 | |
| 870 | *data_in = (const uint8_t *) ci->data_rates; |
| 871 | return CCID_CTRL_RET_OK; |
| 872 | } |
| 873 | |
| 874 | /*! Handle [class specific] CTRL request. We assume the caller has already verified that the |
| 875 | * request was made to the correct interface as well as it is a class-specific request. |
| 876 | * \param[in] ci CCID Instance for which CTRL request was received |
| 877 | * \param[in] ctrl_req buffer holding the 8 bytes CTRL transfer header |
| 878 | * \param[out] data_in data to be returned to the host in the IN transaction (if any) |
| 879 | * \returns CCID_CTRL_RET_OK, CCID_CTRL_RET_INVALID or CCID_CTRL_RET_UNKNOWN |
| 880 | */ |
| 881 | int ccid_handle_ctrl(struct ccid_instance *ci, const uint8_t *ctrl_req, const uint8_t **data_in) |
| 882 | { |
| 883 | const struct _usb_ctrl_req *req = (const struct _usb_ctrl_req *) ctrl_req; |
| 884 | int rc; |
| 885 | |
| 886 | LOGPCI(ci, LOGL_DEBUG, "CTRL bmReqT=0x%02X bRequest=%s, wValue=0x%04X, wIndex=0x%04X, wLength=%d\n", |
| 887 | req->bRequestType, get_value_string(ccid_class_spec_req_vals, req->bRequest), |
| 888 | req->wValue, req->wIndex, req->wLength); |
| 889 | |
| 890 | switch (req->bRequest) { |
| 891 | case CLASS_SPEC_CCID_ABORT: |
| 892 | rc = ccid_handle_ctrl_abort(ci, req); |
| 893 | break; |
| 894 | case CLASS_SPEC_CCID_GET_CLOCK_FREQ: |
| 895 | rc = ccid_handle_ctrl_get_clock_freq(ci, req, data_in); |
| 896 | break; |
| 897 | case CLASS_SPEC_CCID_GET_DATA_RATES: |
| 898 | rc = ccid_handle_ctrl_get_data_rates(ci, req, data_in); |
| 899 | break; |
| 900 | default: |
| 901 | return CCID_CTRL_RET_UNKNOWN; |
| 902 | } |
| 903 | return rc; |
| 904 | } |
| 905 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 906 | 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] | 907 | const struct ccid_slot_ops *slot_ops, |
| 908 | const struct usb_ccid_class_descriptor *class_desc, |
| 909 | const uint32_t *data_rates, const uint32_t *clock_freqs, |
| 910 | const char *name, void *priv) |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 911 | { |
| 912 | int i; |
| 913 | |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 914 | ci->ops = ops; |
| 915 | ci->slot_ops = slot_ops; |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 916 | ci->class_desc = class_desc; |
| 917 | ci->clock_freqs = clock_freqs; |
| 918 | ci->data_rates = data_rates; |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 919 | ci->name = name; |
| 920 | ci->priv = priv; |
Harald Welte | d338507 | 2019-05-16 17:38:23 +0200 | [diff] [blame] | 921 | |
| 922 | for (i = 0; i < ARRAY_SIZE(ci->slot); i++) { |
| 923 | struct ccid_slot *cs = &ci->slot[i]; |
| 924 | cs->slot_nr = i; |
| 925 | cs->ci = ci; |
| 926 | |
| 927 | slot_ops->init(cs); |
| 928 | } |
| 929 | |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 930 | } |