1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Driver for NXP PN533 NFC Chip 4 * 5 * Copyright (C) 2011 Instituto Nokia de Tecnologia 6 * Copyright (C) 2012-2013 Tieto Poland 7 */ 8 9 #define PN533_DEVICE_STD 0x1 10 #define PN533_DEVICE_PASORI 0x2 11 #define PN533_DEVICE_ACR122U 0x3 12 #define PN533_DEVICE_PN532 0x4 13 #define PN533_DEVICE_PN532_AUTOPOLL 0x5 14 15 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\ 16 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\ 17 NFC_PROTO_NFC_DEP_MASK |\ 18 NFC_PROTO_ISO14443_B_MASK) 19 20 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \ 21 NFC_PROTO_MIFARE_MASK | \ 22 NFC_PROTO_FELICA_MASK | \ 23 NFC_PROTO_ISO14443_MASK | \ 24 NFC_PROTO_NFC_DEP_MASK) 25 26 /* Standard pn533 frame definitions (standard and extended)*/ 27 #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \ 28 + 2) /* data[0] TFI, data[1] CC */ 29 #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/ 30 31 #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \ 32 + 2) /* data[0] TFI, data[1] CC */ 33 34 #define PN533_CMD_DATAEXCH_HEAD_LEN 1 35 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 36 #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */ 37 38 /* 39 * Max extended frame payload len, excluding TFI and CC 40 * which are already in PN533_FRAME_HEADER_LEN. 41 */ 42 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263 43 44 45 /* Preamble (1), SoPC (2), ACK Code (2), Postamble (1) */ 46 #define PN533_STD_FRAME_ACK_SIZE 6 47 /* 48 * Preamble (1), SoPC (2), Packet Length (1), Packet Length Checksum (1), 49 * Specific Application Level Error Code (1) , Postamble (1) 50 */ 51 #define PN533_STD_ERROR_FRAME_SIZE 8 52 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen]) 53 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) 54 /* Half start code (3), LEN (4) should be 0xffff for extended frame */ 55 #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \ 56 && (hdr)->datalen_checksum == 0xFF) 57 #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)]) 58 59 /* start of frame */ 60 #define PN533_STD_FRAME_SOF 0x00FF 61 62 /* standard frame identifier: in/out/error */ 63 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */ 64 #define PN533_STD_FRAME_DIR_OUT 0xD4 65 #define PN533_STD_FRAME_DIR_IN 0xD5 66 67 /* PN533 Commands */ 68 #define PN533_FRAME_CMD(f) (f->data[1]) 69 70 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02 71 #define PN533_CMD_SAM_CONFIGURATION 0x14 72 #define PN533_CMD_RF_CONFIGURATION 0x32 73 #define PN533_CMD_IN_DATA_EXCHANGE 0x40 74 #define PN533_CMD_IN_COMM_THRU 0x42 75 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A 76 #define PN533_CMD_IN_ATR 0x50 77 #define PN533_CMD_IN_RELEASE 0x52 78 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56 79 #define PN533_CMD_IN_AUTOPOLL 0x60 80 81 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c 82 #define PN533_CMD_TG_GET_DATA 0x86 83 #define PN533_CMD_TG_SET_DATA 0x8e 84 #define PN533_CMD_TG_SET_META_DATA 0x94 85 #define PN533_CMD_UNDEF 0xff 86 87 #define PN533_CMD_RESPONSE(cmd) (cmd + 1) 88 89 /* PN533 Return codes */ 90 #define PN533_CMD_RET_MASK 0x3F 91 #define PN533_CMD_MI_MASK 0x40 92 #define PN533_CMD_RET_SUCCESS 0x00 93 94 #define PN533_FRAME_DATALEN_ACK 0x00 95 #define PN533_FRAME_DATALEN_ERROR 0x01 96 #define PN533_FRAME_DATALEN_EXTENDED 0xFF 97 98 enum pn533_protocol_type { 99 PN533_PROTO_REQ_ACK_RESP = 0, 100 PN533_PROTO_REQ_RESP 101 }; 102 103 /* Poll modulations */ 104 enum { 105 PN533_POLL_MOD_106KBPS_A, 106 PN533_POLL_MOD_212KBPS_FELICA, 107 PN533_POLL_MOD_424KBPS_FELICA, 108 PN533_POLL_MOD_106KBPS_JEWEL, 109 PN533_POLL_MOD_847KBPS_B, 110 PN533_LISTEN_MOD, 111 112 __PN533_POLL_MOD_AFTER_LAST, 113 }; 114 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1) 115 116 struct pn533_std_frame { 117 u8 preamble; 118 __be16 start_frame; 119 u8 datalen; 120 u8 datalen_checksum; 121 u8 data[]; 122 } __packed; 123 124 struct pn533_ext_frame { /* Extended Information frame */ 125 u8 preamble; 126 __be16 start_frame; 127 __be16 eif_flag; /* fixed to 0xFFFF */ 128 __be16 datalen; 129 u8 datalen_checksum; 130 u8 data[]; 131 } __packed; 132 133 struct pn533 { 134 struct nfc_dev *nfc_dev; 135 u32 device_type; 136 enum pn533_protocol_type protocol_type; 137 138 struct sk_buff_head resp_q; 139 struct sk_buff_head fragment_skb; 140 141 struct workqueue_struct *wq; 142 struct work_struct cmd_work; 143 struct work_struct cmd_complete_work; 144 struct delayed_work poll_work; 145 struct work_struct mi_rx_work; 146 struct work_struct mi_tx_work; 147 struct work_struct mi_tm_rx_work; 148 struct work_struct mi_tm_tx_work; 149 struct work_struct tg_work; 150 struct work_struct rf_work; 151 152 struct list_head cmd_queue; 153 struct pn533_cmd *cmd; 154 u8 cmd_pending; 155 struct mutex cmd_lock; /* protects cmd queue */ 156 157 void *cmd_complete_mi_arg; 158 void *cmd_complete_dep_arg; 159 160 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1]; 161 u8 poll_mod_count; 162 u8 poll_mod_curr; 163 u8 poll_dep; 164 u32 poll_protocols; 165 u32 listen_protocols; 166 struct timer_list listen_timer; 167 int cancel_listen; 168 169 u8 *gb; 170 size_t gb_len; 171 172 u8 tgt_available_prots; 173 u8 tgt_active_prot; 174 u8 tgt_mode; 175 176 struct pn533_frame_ops *ops; 177 178 struct device *dev; 179 void *phy; 180 const struct pn533_phy_ops *phy_ops; 181 }; 182 183 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg, 184 struct sk_buff *resp); 185 186 struct pn533_cmd { 187 struct list_head queue; 188 u8 code; 189 int status; 190 struct sk_buff *req; 191 struct sk_buff *resp; 192 pn533_send_async_complete_t complete_cb; 193 void *complete_cb_context; 194 }; 195 196 197 struct pn533_frame_ops { 198 void (*tx_frame_init)(void *frame, u8 cmd_code); 199 void (*tx_frame_finish)(void *frame); 200 void (*tx_update_payload_len)(void *frame, int len); 201 int tx_header_len; 202 int tx_tail_len; 203 204 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev); 205 bool (*rx_frame_is_ack)(void *frame); 206 int (*rx_frame_size)(void *frame); 207 int rx_header_len; 208 int rx_tail_len; 209 210 int max_payload_len; 211 u8 (*get_cmd_code)(void *frame); 212 }; 213 214 215 struct pn533_phy_ops { 216 int (*send_frame)(struct pn533 *priv, 217 struct sk_buff *out); 218 int (*send_ack)(struct pn533 *dev, gfp_t flags); 219 void (*abort_cmd)(struct pn533 *priv, gfp_t flags); 220 /* 221 * dev_up and dev_down are optional. 222 * They are used to inform the phy layer that the nfc chip 223 * is going to be really used very soon. The phy layer can then 224 * bring up it's interface to the chip and have it suspended for power 225 * saving reasons otherwise. 226 */ 227 int (*dev_up)(struct pn533 *priv); 228 int (*dev_down)(struct pn533 *priv); 229 }; 230 231 232 struct pn533 *pn53x_common_init(u32 device_type, 233 enum pn533_protocol_type protocol_type, 234 void *phy, 235 const struct pn533_phy_ops *phy_ops, 236 struct pn533_frame_ops *fops, 237 struct device *dev); 238 239 int pn533_finalize_setup(struct pn533 *dev); 240 void pn53x_common_clean(struct pn533 *priv); 241 void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status); 242 int pn532_i2c_nfc_alloc(struct pn533 *priv, u32 protocols, 243 struct device *parent); 244 int pn53x_register_nfc(struct pn533 *priv, u32 protocols, 245 struct device *parent); 246 void pn53x_unregister_nfc(struct pn533 *priv); 247 248 bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame); 249 bool pn533_rx_frame_is_ack(void *_frame); 250