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