2 * Driver for NXP PN533 NFC Chip
4 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Copyright (C) 2012-2013 Tieto Poland
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.
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.
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/>.
21 #define PN533_DEVICE_STD 0x1
22 #define PN533_DEVICE_PASORI 0x2
23 #define PN533_DEVICE_ACR122U 0x3
24 #define PN533_DEVICE_PN532 0x4
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)
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)
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*/
42 #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
43 + 2) /* data[0] TFI, data[1] CC */
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) */
50 * Max extended frame payload len, excluding TFI and CC
51 * which are already in PN533_FRAME_HEADER_LEN.
53 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
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)])
66 #define PN533_STD_FRAME_SOF 0x00FF
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
74 #define PN533_FRAME_CMD(f) (f->data[1])
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
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
92 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
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
100 enum pn533_protocol_type
{
101 PN533_PROTO_REQ_ACK_RESP
= 0,
105 /* Poll modulations */
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
,
114 __PN533_POLL_MOD_AFTER_LAST
,
116 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
118 struct pn533_std_frame
{
126 struct pn533_ext_frame
{ /* Extended Information frame */
129 __be16 eif_flag
; /* fixed to 0xFFFF */
136 struct nfc_dev
*nfc_dev
;
138 enum pn533_protocol_type protocol_type
;
140 struct sk_buff_head resp_q
;
141 struct sk_buff_head fragment_skb
;
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
;
154 struct list_head cmd_queue
;
155 struct pn533_cmd
*cmd
;
157 struct mutex cmd_lock
; /* protects cmd queue */
159 void *cmd_complete_mi_arg
;
160 void *cmd_complete_dep_arg
;
162 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
167 u32 listen_protocols
;
168 struct timer_list listen_timer
;
174 u8 tgt_available_prots
;
178 struct pn533_frame_ops
*ops
;
182 struct pn533_phy_ops
*phy_ops
;
185 typedef int (*pn533_send_async_complete_t
) (struct pn533
*dev
, void *arg
,
186 struct sk_buff
*resp
);
189 struct list_head queue
;
193 struct sk_buff
*resp
;
194 pn533_send_async_complete_t complete_cb
;
195 void *complete_cb_context
;
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
);
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
);
213 u8 (*get_cmd_code
)(void *frame
);
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
);
225 struct pn533
*pn533_register_device(u32 device_type
,
227 enum pn533_protocol_type protocol_type
,
229 struct pn533_phy_ops
*phy_ops
,
230 struct pn533_frame_ops
*fops
,
232 struct device
*parent
);
234 int pn533_finalize_setup(struct pn533
*dev
);
235 void pn533_unregister_device(struct pn533
*priv
);
236 void pn533_recv_frame(struct pn533
*dev
, struct sk_buff
*skb
, int status
);
238 bool pn533_rx_frame_is_cmd_response(struct pn533
*dev
, void *frame
);
239 bool pn533_rx_frame_is_ack(void *_frame
);