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