xref: /openbmc/linux/drivers/nfc/pn533/pn533.h (revision 7d545e77)
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 
14 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
15 			     NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
16 			     NFC_PROTO_NFC_DEP_MASK |\
17 			     NFC_PROTO_ISO14443_B_MASK)
18 
19 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
20 				   NFC_PROTO_MIFARE_MASK | \
21 				   NFC_PROTO_FELICA_MASK | \
22 				   NFC_PROTO_ISO14443_MASK | \
23 				   NFC_PROTO_NFC_DEP_MASK)
24 
25 /* Standard pn533 frame definitions (standard and extended)*/
26 #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
27 					+ 2) /* data[0] TFI, data[1] CC */
28 #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
29 
30 #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
31 					+ 2) /* data[0] TFI, data[1] CC */
32 
33 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
34 #define PN533_CMD_DATAEXCH_DATA_MAXLEN	262
35 #define PN533_CMD_DATAFRAME_MAXLEN	240	/* max data length (send) */
36 
37 /*
38  * Max extended frame payload len, excluding TFI and CC
39  * which are already in PN533_FRAME_HEADER_LEN.
40  */
41 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
42 
43 
44 /* Preamble (1), SoPC (2), ACK Code (2), Postamble (1) */
45 #define PN533_STD_FRAME_ACK_SIZE 6
46 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
47 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
48 /* Half start code (3), LEN (4) should be 0xffff for extended frame */
49 #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
50 					&& (hdr)->datalen_checksum == 0xFF)
51 #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
52 
53 /* start of frame */
54 #define PN533_STD_FRAME_SOF 0x00FF
55 
56 /* standard frame identifier: in/out/error */
57 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
58 #define PN533_STD_FRAME_DIR_OUT 0xD4
59 #define PN533_STD_FRAME_DIR_IN 0xD5
60 
61 /* PN533 Commands */
62 #define PN533_FRAME_CMD(f) (f->data[1])
63 
64 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
65 #define PN533_CMD_SAM_CONFIGURATION 0x14
66 #define PN533_CMD_RF_CONFIGURATION 0x32
67 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
68 #define PN533_CMD_IN_COMM_THRU     0x42
69 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
70 #define PN533_CMD_IN_ATR 0x50
71 #define PN533_CMD_IN_RELEASE 0x52
72 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
73 
74 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
75 #define PN533_CMD_TG_GET_DATA 0x86
76 #define PN533_CMD_TG_SET_DATA 0x8e
77 #define PN533_CMD_TG_SET_META_DATA 0x94
78 #define PN533_CMD_UNDEF 0xff
79 
80 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
81 
82 /* PN533 Return codes */
83 #define PN533_CMD_RET_MASK 0x3F
84 #define PN533_CMD_MI_MASK 0x40
85 #define PN533_CMD_RET_SUCCESS 0x00
86 
87 
88 enum  pn533_protocol_type {
89 	PN533_PROTO_REQ_ACK_RESP = 0,
90 	PN533_PROTO_REQ_RESP
91 };
92 
93 /* Poll modulations */
94 enum {
95 	PN533_POLL_MOD_106KBPS_A,
96 	PN533_POLL_MOD_212KBPS_FELICA,
97 	PN533_POLL_MOD_424KBPS_FELICA,
98 	PN533_POLL_MOD_106KBPS_JEWEL,
99 	PN533_POLL_MOD_847KBPS_B,
100 	PN533_LISTEN_MOD,
101 
102 	__PN533_POLL_MOD_AFTER_LAST,
103 };
104 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
105 
106 struct pn533_std_frame {
107 	u8 preamble;
108 	__be16 start_frame;
109 	u8 datalen;
110 	u8 datalen_checksum;
111 	u8 data[];
112 } __packed;
113 
114 struct pn533_ext_frame {	/* Extended Information frame */
115 	u8 preamble;
116 	__be16 start_frame;
117 	__be16 eif_flag;	/* fixed to 0xFFFF */
118 	__be16 datalen;
119 	u8 datalen_checksum;
120 	u8 data[];
121 } __packed;
122 
123 struct pn533 {
124 	struct nfc_dev *nfc_dev;
125 	u32 device_type;
126 	enum pn533_protocol_type protocol_type;
127 
128 	struct sk_buff_head resp_q;
129 	struct sk_buff_head fragment_skb;
130 
131 	struct workqueue_struct	*wq;
132 	struct work_struct cmd_work;
133 	struct work_struct cmd_complete_work;
134 	struct delayed_work poll_work;
135 	struct work_struct mi_rx_work;
136 	struct work_struct mi_tx_work;
137 	struct work_struct mi_tm_rx_work;
138 	struct work_struct mi_tm_tx_work;
139 	struct work_struct tg_work;
140 	struct work_struct rf_work;
141 
142 	struct list_head cmd_queue;
143 	struct pn533_cmd *cmd;
144 	u8 cmd_pending;
145 	struct mutex cmd_lock;  /* protects cmd queue */
146 
147 	void *cmd_complete_mi_arg;
148 	void *cmd_complete_dep_arg;
149 
150 	struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
151 	u8 poll_mod_count;
152 	u8 poll_mod_curr;
153 	u8 poll_dep;
154 	u32 poll_protocols;
155 	u32 listen_protocols;
156 	struct timer_list listen_timer;
157 	int cancel_listen;
158 
159 	u8 *gb;
160 	size_t gb_len;
161 
162 	u8 tgt_available_prots;
163 	u8 tgt_active_prot;
164 	u8 tgt_mode;
165 
166 	struct pn533_frame_ops *ops;
167 
168 	struct device *dev;
169 	void *phy;
170 	struct pn533_phy_ops *phy_ops;
171 };
172 
173 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
174 					struct sk_buff *resp);
175 
176 struct pn533_cmd {
177 	struct list_head queue;
178 	u8 code;
179 	int status;
180 	struct sk_buff *req;
181 	struct sk_buff *resp;
182 	pn533_send_async_complete_t  complete_cb;
183 	void *complete_cb_context;
184 };
185 
186 
187 struct pn533_frame_ops {
188 	void (*tx_frame_init)(void *frame, u8 cmd_code);
189 	void (*tx_frame_finish)(void *frame);
190 	void (*tx_update_payload_len)(void *frame, int len);
191 	int tx_header_len;
192 	int tx_tail_len;
193 
194 	bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
195 	bool (*rx_frame_is_ack)(void *frame);
196 	int (*rx_frame_size)(void *frame);
197 	int rx_header_len;
198 	int rx_tail_len;
199 
200 	int max_payload_len;
201 	u8 (*get_cmd_code)(void *frame);
202 };
203 
204 
205 struct pn533_phy_ops {
206 	int (*send_frame)(struct pn533 *priv,
207 			  struct sk_buff *out);
208 	int (*send_ack)(struct pn533 *dev, gfp_t flags);
209 	void (*abort_cmd)(struct pn533 *priv, gfp_t flags);
210 };
211 
212 
213 struct pn533 *pn533_register_device(u32 device_type,
214 				u32 protocols,
215 				enum pn533_protocol_type protocol_type,
216 				void *phy,
217 				struct pn533_phy_ops *phy_ops,
218 				struct pn533_frame_ops *fops,
219 				struct device *dev,
220 				struct device *parent);
221 
222 int pn533_finalize_setup(struct pn533 *dev);
223 void pn533_unregister_device(struct pn533 *priv);
224 void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status);
225 
226 bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame);
227 bool pn533_rx_frame_is_ack(void *_frame);
228