blob: a172a32aa9d9329e8caaed6942609151080b0a82 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002/*
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01003 * Driver for NXP PN533 NFC Chip - core functions
4 *
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03005 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02006 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03007 */
8
9#include <linux/device.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/slab.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030013#include <linux/nfc.h>
14#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030015#include <net/nfc/nfc.h>
Michael Thalmeier9815c7c2016-03-25 15:46:53 +010016#include "pn533.h"
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030017
Michael Thalmeier9815c7c2016-03-25 15:46:53 +010018#define VERSION "0.3"
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030019
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020020/* How much time we spend listening for initiators */
21#define PN533_LISTEN_TIME 2
Samuel Ortiz46f793b2013-07-03 14:50:36 +020022/* Delay between each poll frame (ms) */
23#define PN533_POLL_INTERVAL 10
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020024
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030025/* structs for pn533 commands */
26
27/* PN533_CMD_GET_FIRMWARE_VERSION */
28struct pn533_fw_version {
29 u8 ic;
30 u8 ver;
31 u8 rev;
32 u8 support;
33};
34
35/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz60d9edd52013-04-11 11:45:41 +020036#define PN533_CFGITEM_RF_FIELD 0x01
37#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030038#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz60d9edd52013-04-11 11:45:41 +020039#define PN533_CFGITEM_PASORI 0x82
40
Samuel Ortiz3a8eab32013-07-01 17:26:58 +020041#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
42#define PN533_CFGITEM_RF_FIELD_ON 0x1
43#define PN533_CFGITEM_RF_FIELD_OFF 0x0
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030044
Samuel Ortiz34a85bf2012-05-29 21:34:08 +020045#define PN533_CONFIG_TIMING_102 0xb
46#define PN533_CONFIG_TIMING_204 0xc
47#define PN533_CONFIG_TIMING_409 0xd
48#define PN533_CONFIG_TIMING_819 0xe
49
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030050#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
51#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
52
53struct pn533_config_max_retries {
54 u8 mx_rty_atr;
55 u8 mx_rty_psl;
56 u8 mx_rty_passive_act;
57} __packed;
58
Samuel Ortiz34a85bf2012-05-29 21:34:08 +020059struct pn533_config_timing {
60 u8 rfu;
61 u8 atr_res_timeout;
62 u8 dep_timeout;
63} __packed;
64
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030065/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
66
67/* felica commands opcode */
68#define PN533_FELICA_OPC_SENSF_REQ 0
69#define PN533_FELICA_OPC_SENSF_RES 1
70/* felica SENSF_REQ parameters */
71#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
72#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
73#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
74#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
75
76/* type B initiator_data values */
77#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
78#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
79#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
80
81union pn533_cmd_poll_initdata {
82 struct {
83 u8 afi;
84 u8 polling_method;
85 } __packed type_b;
86 struct {
87 u8 opcode;
88 __be16 sc;
89 u8 rc;
90 u8 tsn;
91 } __packed felica;
92};
93
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030094struct pn533_poll_modulations {
95 struct {
96 u8 maxtg;
97 u8 brty;
98 union pn533_cmd_poll_initdata initiator_data;
99 } __packed data;
100 u8 len;
101};
102
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100103static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300104 [PN533_POLL_MOD_106KBPS_A] = {
105 .data = {
106 .maxtg = 1,
107 .brty = 0,
108 },
109 .len = 2,
110 },
111 [PN533_POLL_MOD_212KBPS_FELICA] = {
112 .data = {
113 .maxtg = 1,
114 .brty = 1,
115 .initiator_data.felica = {
116 .opcode = PN533_FELICA_OPC_SENSF_REQ,
117 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200118 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200119 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300120 },
121 },
122 .len = 7,
123 },
124 [PN533_POLL_MOD_424KBPS_FELICA] = {
125 .data = {
126 .maxtg = 1,
127 .brty = 2,
128 .initiator_data.felica = {
129 .opcode = PN533_FELICA_OPC_SENSF_REQ,
130 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200131 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200132 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300133 },
134 },
135 .len = 7,
136 },
137 [PN533_POLL_MOD_106KBPS_JEWEL] = {
138 .data = {
139 .maxtg = 1,
140 .brty = 4,
141 },
142 .len = 2,
143 },
144 [PN533_POLL_MOD_847KBPS_B] = {
145 .data = {
146 .maxtg = 1,
147 .brty = 8,
148 .initiator_data.type_b = {
149 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
150 .polling_method =
151 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
152 },
153 },
154 .len = 3,
155 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200156 [PN533_LISTEN_MOD] = {
157 .len = 0,
158 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300159};
160
161/* PN533_CMD_IN_ATR */
162
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300163struct pn533_cmd_activate_response {
164 u8 status;
165 u8 nfcid3t[10];
166 u8 didt;
167 u8 bst;
168 u8 brt;
169 u8 to;
170 u8 ppt;
171 /* optional */
172 u8 gt[];
173} __packed;
174
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100175struct pn533_cmd_jump_dep_response {
176 u8 status;
177 u8 tg;
178 u8 nfcid3t[10];
179 u8 didt;
180 u8 bst;
181 u8 brt;
182 u8 to;
183 u8 ppt;
184 /* optional */
185 u8 gt[];
186} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300187
Samuel Ortizad3823c2012-05-30 23:54:55 +0200188
189/* PN533_TG_INIT_AS_TARGET */
190#define PN533_INIT_TARGET_PASSIVE 0x1
191#define PN533_INIT_TARGET_DEP 0x2
192
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200193#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
194#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
195#define PN533_INIT_TARGET_RESP_DEP 0x4
196
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200197/* The rule: value(high byte) + value(low byte) + checksum = 0 */
198static inline u8 pn533_ext_checksum(u16 value)
199{
200 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
201}
202
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300203/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200204static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300205{
206 return ~value + 1;
207}
208
209/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200210static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300211{
212 u8 sum = 0;
213 int i;
214
215 for (i = 0; i < datalen; i++)
216 sum += data[i];
217
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200218 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300219}
220
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200221static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300222{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200223 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100224
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300225 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200226 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
227 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200228 PN533_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300229 frame->datalen = 2;
230}
231
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200232static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300233{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200234 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100235
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200236 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300237
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200238 PN533_STD_FRAME_CHECKSUM(frame) =
239 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300240
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200241 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300242}
243
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200244static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100245{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200246 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100247
248 frame->datalen += len;
249}
250
Olivier Guiter56a63c822013-06-13 13:43:29 +0000251static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300252{
253 u8 checksum;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200254 struct pn533_std_frame *stdf = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300255
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200256 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300257 return false;
258
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200259 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
260 /* Standard frame code */
Olivier Guiter56a63c822013-06-13 13:43:29 +0000261 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300262
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200263 checksum = pn533_std_checksum(stdf->datalen);
264 if (checksum != stdf->datalen_checksum)
265 return false;
266
267 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
268 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
269 return false;
270 } else {
271 /* Extended */
272 struct pn533_ext_frame *eif = _frame;
273
Olivier Guiter56a63c822013-06-13 13:43:29 +0000274 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
275
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200276 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
277 if (checksum != eif->datalen_checksum)
278 return false;
279
280 /* check data checksum */
281 checksum = pn533_std_data_checksum(eif->data,
282 be16_to_cpu(eif->datalen));
283 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
284 return false;
285 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300286
287 return true;
288}
289
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100290bool pn533_rx_frame_is_ack(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300291{
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100292 struct pn533_std_frame *frame = _frame;
293
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200294 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300295 return false;
296
297 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
298 return false;
299
300 return true;
301}
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100302EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200304static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300305{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200306 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100307
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200308 /* check for Extended Information frame */
309 if (PN533_STD_IS_EXTENDED(f)) {
310 struct pn533_ext_frame *eif = frame;
311
312 return sizeof(struct pn533_ext_frame)
313 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
314 }
315
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200316 return sizeof(struct pn533_std_frame) + f->datalen +
317 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100318}
319
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200320static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100321{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200322 struct pn533_std_frame *f = frame;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200323 struct pn533_ext_frame *eif = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100324
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200325 if (PN533_STD_IS_EXTENDED(f))
326 return PN533_FRAME_CMD(eif);
327 else
328 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100329}
330
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100331bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
332{
333 return (dev->ops->get_cmd_code(frame) ==
334 PN533_CMD_RESPONSE(dev->cmd->code));
335}
336EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);
337
338
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100339static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200340 .tx_frame_init = pn533_std_tx_frame_init,
341 .tx_frame_finish = pn533_std_tx_frame_finish,
342 .tx_update_payload_len = pn533_std_tx_update_payload_len,
343 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
344 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100345
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200346 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
347 .rx_frame_size = pn533_std_rx_frame_size,
348 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
349 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100350
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200351 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
352 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100353};
354
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100355static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
356 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100357{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100358 /* payload is already there, just update datalen */
359 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100360 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100361
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100362
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100363 skb_push(skb, ops->tx_header_len);
364 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100365
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100366 ops->tx_frame_init(skb->data, cmd_code);
367 ops->tx_update_payload_len(skb->data, payload_len);
368 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100369}
370
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200371static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100372{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200373 struct pn533_cmd *cmd = dev->cmd;
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300374 struct sk_buff *resp;
375 int status, rc = 0;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100376
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300377 if (!cmd) {
378 dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
379 goto done;
380 }
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100381
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300382 dev_kfree_skb(cmd->req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100383
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300384 status = cmd->status;
385 resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100386
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100387 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200388 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
389 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100390 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200391 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100392 }
393
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300394 /* when no response is set we got interrupted */
395 if (!resp)
396 resp = ERR_PTR(-EINTR);
397
398 if (!IS_ERR(resp)) {
399 skb_pull(resp, dev->ops->rx_header_len);
400 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
401 }
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100402
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200403 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100404
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200405done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200406 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200407 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100408 return rc;
409}
410
411static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100412 struct sk_buff *req,
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100413 pn533_send_async_complete_t complete_cb,
414 void *complete_cb_context)
415{
416 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100417 int rc = 0;
418
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100419 dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100420
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200421 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
422 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100423 return -ENOMEM;
424
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200425 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200426 cmd->req = req;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200427 cmd->complete_cb = complete_cb;
428 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100429
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100430 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100431
432 mutex_lock(&dev->cmd_lock);
433
434 if (!dev->cmd_pending) {
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300435 dev->cmd = cmd;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100436 rc = dev->phy_ops->send_frame(dev, req);
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300437 if (rc) {
438 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100439 goto error;
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300440 }
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100441
442 dev->cmd_pending = 1;
443 goto unlock;
444 }
445
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100446 dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
Joe Perchesb4834832013-04-05 12:27:37 -0700447 __func__, cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100448
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100449 INIT_LIST_HEAD(&cmd->queue);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100450 list_add_tail(&cmd->queue, &dev->cmd_queue);
451
452 goto unlock;
453
454error:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200455 kfree(cmd);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100456unlock:
457 mutex_unlock(&dev->cmd_lock);
458 return rc;
459}
460
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100461static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
462 struct sk_buff *req,
463 pn533_send_async_complete_t complete_cb,
464 void *complete_cb_context)
465{
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100466 int rc;
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100467
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100468 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100469 complete_cb_context);
Waldemar Rymarkiewicz15461ae2012-11-26 14:18:35 +0100470
471 return rc;
472}
473
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100474static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
475 struct sk_buff *req,
476 pn533_send_async_complete_t complete_cb,
477 void *complete_cb_context)
478{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100479 int rc;
480
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100481 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100482 complete_cb_context);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100483
484 return rc;
485}
486
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100487/*
488 * pn533_send_cmd_direct_async
489 *
Masahiro Yamada0cf2a842017-02-27 14:29:39 -0800490 * The function sends a piority cmd directly to the chip omitting the cmd
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100491 * queue. It's intended to be used by chaining mechanism of received responses
492 * where the host has to request every single chunk of data before scheduling
493 * next cmd from the queue.
494 */
495static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
496 struct sk_buff *req,
497 pn533_send_async_complete_t complete_cb,
498 void *complete_cb_context)
499{
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200500 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100501 int rc;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100502
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200503 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100504 if (!cmd)
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100505 return -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100506
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200507 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200508 cmd->req = req;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200509 cmd->complete_cb = complete_cb;
510 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100511
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100512 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100513
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300514 dev->cmd = cmd;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100515 rc = dev->phy_ops->send_frame(dev, req);
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300516 if (rc < 0) {
517 dev->cmd = NULL;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200518 kfree(cmd);
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300519 }
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100520
521 return rc;
522}
523
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +0200524static void pn533_wq_cmd_complete(struct work_struct *work)
525{
526 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
527 int rc;
528
529 rc = pn533_send_async_complete(dev);
530 if (rc != -EINPROGRESS)
531 queue_work(dev->wq, &dev->cmd_work);
532}
533
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200534static void pn533_wq_cmd(struct work_struct *work)
535{
536 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
537 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200538 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200539
540 mutex_lock(&dev->cmd_lock);
541
542 if (list_empty(&dev->cmd_queue)) {
543 dev->cmd_pending = 0;
544 mutex_unlock(&dev->cmd_lock);
545 return;
546 }
547
548 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
549
Szymon Janc60ad07a2012-10-25 17:29:45 +0200550 list_del(&cmd->queue);
551
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200552 mutex_unlock(&dev->cmd_lock);
553
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300554 dev->cmd = cmd;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100555 rc = dev->phy_ops->send_frame(dev, cmd->req);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200556 if (rc < 0) {
Andrey Rusalin5dd9c1b2016-12-28 20:10:58 +0300557 dev->cmd = NULL;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200558 dev_kfree_skb(cmd->req);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200559 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200560 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200561 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200562
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200563}
564
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300565struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100566 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300567 struct completion done;
568};
569
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100570static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
571 struct sk_buff *resp)
572{
573 struct pn533_sync_cmd_response *arg = _arg;
574
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100575 arg->resp = resp;
576 complete(&arg->done);
577
578 return 0;
579}
580
581/* pn533_send_cmd_sync
582 *
583 * Please note the req parameter is freed inside the function to
584 * limit a number of return value interpretations by the caller.
585 *
586 * 1. negative in case of error during TX path -> req should be freed
587 *
588 * 2. negative in case of error during RX path -> req should not be freed
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100589 * as it's been already freed at the beginning of RX path by
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100590 * async_complete_cb.
591 *
592 * 3. valid pointer in case of succesfult RX path
593 *
594 * A caller has to check a return value with IS_ERR macro. If the test pass,
595 * the returned pointer is valid.
596 *
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100597 */
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100598static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
599 struct sk_buff *req)
600{
601 int rc;
602 struct pn533_sync_cmd_response arg;
603
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100604 init_completion(&arg.done);
605
606 rc = pn533_send_cmd_async(dev, cmd_code, req,
607 pn533_send_sync_complete, &arg);
608 if (rc) {
609 dev_kfree_skb(req);
610 return ERR_PTR(rc);
611 }
612
613 wait_for_completion(&arg.done);
614
615 return arg.resp;
616}
617
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100618static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100619{
620 struct sk_buff *skb;
621
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100622 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100623 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100624 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100625
626 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100627 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100628
629 return skb;
630}
631
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300632struct pn533_target_type_a {
633 __be16 sens_res;
634 u8 sel_res;
635 u8 nfcid_len;
636 u8 nfcid_data[];
637} __packed;
638
639
640#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
641#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
642#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
643
644#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
645#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
646
647#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
648#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
649
650#define PN533_TYPE_A_SEL_PROT_MIFARE 0
651#define PN533_TYPE_A_SEL_PROT_ISO14443 1
652#define PN533_TYPE_A_SEL_PROT_DEP 2
653#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
654
655static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
656 int target_data_len)
657{
658 u8 ssd;
659 u8 platconf;
660
661 if (target_data_len < sizeof(struct pn533_target_type_a))
662 return false;
663
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100664 /*
665 * The length check of nfcid[] and ats[] are not being performed because
666 * the values are not being used
667 */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300668
669 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
670 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
671 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
672
673 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100674 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
675 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
676 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300677 return false;
678
679 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
680 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
681 return false;
682
683 return true;
684}
685
686static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
687 int tgt_data_len)
688{
689 struct pn533_target_type_a *tgt_type_a;
690
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100691 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300692
693 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
694 return -EPROTO;
695
696 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
697 case PN533_TYPE_A_SEL_PROT_MIFARE:
698 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
699 break;
700 case PN533_TYPE_A_SEL_PROT_ISO14443:
701 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
702 break;
703 case PN533_TYPE_A_SEL_PROT_DEP:
704 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
705 break;
706 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
707 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
708 NFC_PROTO_NFC_DEP_MASK;
709 break;
710 }
711
712 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
713 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +0100714 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
715 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300716
717 return 0;
718}
719
720struct pn533_target_felica {
721 u8 pol_res;
722 u8 opcode;
Samuel Ortiz322bce92013-05-27 15:29:11 +0200723 u8 nfcid2[NFC_NFCID2_MAXSIZE];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300724 u8 pad[8];
725 /* optional */
726 u8 syst_code[];
727} __packed;
728
729#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
730#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
731
732static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
733 int target_data_len)
734{
735 if (target_data_len < sizeof(struct pn533_target_felica))
736 return false;
737
738 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
739 return false;
740
741 return true;
742}
743
744static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
745 int tgt_data_len)
746{
747 struct pn533_target_felica *tgt_felica;
748
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100749 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300750
751 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
752 return -EPROTO;
753
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100754 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
755 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300756 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
757 else
758 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
759
Samuel Ortiz79757542012-03-05 01:03:45 +0100760 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
761 nfc_tgt->sensf_res_len = 9;
762
Samuel Ortiz322bce92013-05-27 15:29:11 +0200763 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
764 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
765
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300766 return 0;
767}
768
769struct pn533_target_jewel {
770 __be16 sens_res;
771 u8 jewelid[4];
772} __packed;
773
774static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
775 int target_data_len)
776{
777 u8 ssd;
778 u8 platconf;
779
780 if (target_data_len < sizeof(struct pn533_target_jewel))
781 return false;
782
783 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
784 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
785 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
786
787 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100788 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
789 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
790 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300791 return false;
792
793 return true;
794}
795
796static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
797 int tgt_data_len)
798{
799 struct pn533_target_jewel *tgt_jewel;
800
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100801 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300802
803 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
804 return -EPROTO;
805
806 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
807 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +0100808 nfc_tgt->nfcid1_len = 4;
809 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300810
811 return 0;
812}
813
814struct pn533_type_b_prot_info {
815 u8 bitrate;
816 u8 fsci_type;
817 u8 fwi_adc_fo;
818} __packed;
819
820#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
821#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
822#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
823
824struct pn533_type_b_sens_res {
825 u8 opcode;
826 u8 nfcid[4];
827 u8 appdata[4];
828 struct pn533_type_b_prot_info prot_info;
829} __packed;
830
831#define PN533_TYPE_B_OPC_SENSB_RES 0x50
832
833struct pn533_target_type_b {
834 struct pn533_type_b_sens_res sensb_res;
835 u8 attrib_res_len;
836 u8 attrib_res[];
837} __packed;
838
839static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
840 int target_data_len)
841{
842 if (target_data_len < sizeof(struct pn533_target_type_b))
843 return false;
844
845 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
846 return false;
847
848 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
849 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
850 return false;
851
852 return true;
853}
854
855static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
856 int tgt_data_len)
857{
858 struct pn533_target_type_b *tgt_type_b;
859
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100860 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300861
862 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
863 return -EPROTO;
864
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200865 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300866
867 return 0;
868}
869
Michael Thalmeierc952f912016-04-21 16:43:52 +0200870static void pn533_poll_reset_mod_list(struct pn533 *dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100871static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
872 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300873{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300874 struct nfc_target nfc_tgt;
875 int rc;
876
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100877 dev_dbg(dev->dev, "%s: modulation=%d\n",
Joe Perchesb4834832013-04-05 12:27:37 -0700878 __func__, dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300879
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100880 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300881 return -EPROTO;
882
Samuel Ortiz98b3ac12012-03-05 01:03:39 +0100883 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
884
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300885 switch (dev->poll_mod_curr) {
886 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100887 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300888 break;
889 case PN533_POLL_MOD_212KBPS_FELICA:
890 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100891 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300892 break;
893 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100894 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300895 break;
896 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100897 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300898 break;
899 default:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100900 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -0700901 "Unknown current poll modulation\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300902 return -EPROTO;
903 }
904
905 if (rc)
906 return rc;
907
908 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100909 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -0700910 "The Tg found doesn't have the desired protocol\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300911 return -EAGAIN;
912 }
913
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100914 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -0700915 "Target found - supported protocols: 0x%x\n",
916 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300917
918 dev->tgt_available_prots = nfc_tgt.supported_protocols;
919
Michael Thalmeierc952f912016-04-21 16:43:52 +0200920 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300921 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
922
923 return 0;
924}
925
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200926static inline void pn533_poll_next_mod(struct pn533 *dev)
927{
928 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
929}
930
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300931static void pn533_poll_reset_mod_list(struct pn533 *dev)
932{
933 dev->poll_mod_count = 0;
934}
935
936static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
937{
938 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100939 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300940 dev->poll_mod_count++;
941}
942
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200943static void pn533_poll_create_mod_list(struct pn533 *dev,
944 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300945{
946 pn533_poll_reset_mod_list(dev);
947
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +0100948 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
949 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
950 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300951 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
952
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +0100953 if (im_protocols & NFC_PROTO_FELICA_MASK ||
954 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300955 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
956 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
957 }
958
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200959 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300960 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
961
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200962 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300963 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300964
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200965 if (tm_protocols)
966 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300967}
968
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100969static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300970{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100971 u8 nbtg, tg, *tgdata;
972 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300973
Samuel Ortiz673088f2013-08-21 15:06:55 +0200974 /* Toggle the DEP polling */
Michael Thalmeiere997ebbe462016-03-25 15:46:51 +0100975 if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
976 dev->poll_dep = 1;
Samuel Ortiz673088f2013-08-21 15:06:55 +0200977
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100978 nbtg = resp->data[0];
979 tg = resp->data[1];
980 tgdata = &resp->data[2];
981 tgdata_len = resp->len - 2; /* nbtg + tg */
982
983 if (nbtg) {
984 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300985
986 /* We must stop the poll after a valid target found */
Michael Thalmeierc952f912016-04-21 16:43:52 +0200987 if (rc == 0)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200988 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300989 }
990
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200991 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300992}
993
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100994static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +0200995{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100996 struct sk_buff *skb;
yuan linyub952f4d2017-06-18 22:52:04 +0800997 u8 *felica, *nfcid3;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100998
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100999 u8 *gbytes = dev->gb;
1000 size_t gbytes_len = dev->gb_len;
1001
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001002 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1003 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1004 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1005 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001006
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001007 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1008 0x0, 0x0, 0x0,
1009 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001010
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001011 unsigned int skb_len = 36 + /*
1012 * mode (1), mifare (6),
1013 * felica (18), nfcid3 (10), gb_len (1)
1014 */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001015 gbytes_len +
1016 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001017
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001018 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001019 if (!skb)
1020 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001021
1022 /* DEP support only */
Johannes Berg634fef62017-06-16 14:29:24 +02001023 skb_put_u8(skb, PN533_INIT_TARGET_DEP);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001024
1025 /* MIFARE params */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001026 skb_put_data(skb, mifare_params, 6);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001027
1028 /* Felica params */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001029 felica = skb_put_data(skb, felica_params, 18);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001030 get_random_bytes(felica + 2, 6);
1031
1032 /* NFCID3 */
Johannes Bergaa9f9792017-06-13 14:28:18 +02001033 nfcid3 = skb_put_zero(skb, 10);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001034 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001035
1036 /* General bytes */
Johannes Berg634fef62017-06-16 14:29:24 +02001037 skb_put_u8(skb, gbytes_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001038
yuan linyub952f4d2017-06-18 22:52:04 +08001039 skb_put_data(skb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001040
Samuel Ortizad3823c2012-05-30 23:54:55 +02001041 /* Len Tk */
Johannes Berg634fef62017-06-16 14:29:24 +02001042 skb_put_u8(skb, 0);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001043
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001044 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001045}
1046
Olivier Guiter3c13b242013-09-23 12:24:38 +02001047static void pn533_wq_tm_mi_recv(struct work_struct *work);
1048static struct sk_buff *pn533_build_response(struct pn533 *dev);
1049
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001050static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001051 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001052{
Olivier Guiter3c13b242013-09-23 12:24:38 +02001053 struct sk_buff *skb;
1054 u8 status, ret, mi;
1055 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001056
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001057 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001058
Olivier Guiter3c13b242013-09-23 12:24:38 +02001059 if (IS_ERR(resp)) {
1060 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001061 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001062 }
1063
Olivier Guiter3c13b242013-09-23 12:24:38 +02001064 status = resp->data[0];
1065
1066 ret = status & PN533_CMD_RET_MASK;
1067 mi = status & PN533_CMD_MI_MASK;
1068
1069 skb_pull(resp, sizeof(status));
1070
1071 if (ret != PN533_CMD_RET_SUCCESS) {
1072 rc = -EIO;
1073 goto error;
1074 }
1075
1076 skb_queue_tail(&dev->resp_q, resp);
1077
1078 if (mi) {
1079 queue_work(dev->wq, &dev->mi_tm_rx_work);
1080 return -EINPROGRESS;
1081 }
1082
1083 skb = pn533_build_response(dev);
1084 if (!skb) {
1085 rc = -EIO;
1086 goto error;
1087 }
1088
1089 return nfc_tm_data_received(dev->nfc_dev, skb);
1090
1091error:
1092 nfc_tm_deactivated(dev->nfc_dev);
1093 dev->tgt_mode = 0;
1094 skb_queue_purge(&dev->resp_q);
1095 dev_kfree_skb(resp);
1096
1097 return rc;
1098}
1099
1100static void pn533_wq_tm_mi_recv(struct work_struct *work)
1101{
1102 struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1103 struct sk_buff *skb;
1104 int rc;
1105
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001106 dev_dbg(dev->dev, "%s\n", __func__);
Olivier Guiter3c13b242013-09-23 12:24:38 +02001107
1108 skb = pn533_alloc_skb(dev, 0);
1109 if (!skb)
1110 return;
1111
1112 rc = pn533_send_cmd_direct_async(dev,
1113 PN533_CMD_TG_GET_DATA,
1114 skb,
1115 pn533_tm_get_data_complete,
1116 NULL);
1117
1118 if (rc < 0)
1119 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001120}
1121
Olivier Guiter93ad4202013-09-23 12:24:39 +02001122static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1123 struct sk_buff *resp);
1124static void pn533_wq_tm_mi_send(struct work_struct *work)
1125{
1126 struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1127 struct sk_buff *skb;
1128 int rc;
1129
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001130 dev_dbg(dev->dev, "%s\n", __func__);
Olivier Guiter93ad4202013-09-23 12:24:39 +02001131
1132 /* Grab the first skb in the queue */
1133 skb = skb_dequeue(&dev->fragment_skb);
1134 if (skb == NULL) { /* No more data */
1135 /* Reset the queue for future use */
1136 skb_queue_head_init(&dev->fragment_skb);
1137 goto error;
1138 }
1139
1140 /* last entry - remove MI bit */
1141 if (skb_queue_len(&dev->fragment_skb) == 0) {
1142 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1143 skb, pn533_tm_send_complete, NULL);
1144 } else
1145 rc = pn533_send_cmd_direct_async(dev,
1146 PN533_CMD_TG_SET_META_DATA,
1147 skb, pn533_tm_send_complete, NULL);
1148
1149 if (rc == 0) /* success */
1150 return;
1151
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001152 dev_err(dev->dev,
Olivier Guiter93ad4202013-09-23 12:24:39 +02001153 "Error %d when trying to perform set meta data_exchange", rc);
1154
1155 dev_kfree_skb(skb);
1156
1157error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001158 dev->phy_ops->send_ack(dev, GFP_KERNEL);
Olivier Guiter93ad4202013-09-23 12:24:39 +02001159 queue_work(dev->wq, &dev->cmd_work);
1160}
1161
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001162static void pn533_wq_tg_get_data(struct work_struct *work)
1163{
1164 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001165 struct sk_buff *skb;
1166 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001167
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001168 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001169
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001170 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001171 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001172 return;
1173
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001174 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1175 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001176
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001177 if (rc < 0)
1178 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001179}
1180
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001181#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001182static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001183{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001184 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001185 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001186 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001187
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001188 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001189
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001190 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001191 return -EINVAL;
1192
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001193 mode = resp->data[0];
1194 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001195
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001196 dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001197 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001198
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001199 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1200 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001201 comm_mode = NFC_COMM_ACTIVE;
1202
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001203 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001204 return -EOPNOTSUPP;
1205
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001206 gb = cmd + ATR_REQ_GB_OFFSET;
1207 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001208
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001209 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1210 comm_mode, gb, gb_len);
1211 if (rc < 0) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001212 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001213 "Error when signaling target activation\n");
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001214 return rc;
1215 }
1216
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001217 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001218 queue_work(dev->wq, &dev->tg_work);
1219
1220 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001221}
1222
Kees Cooke99e88a2017-10-16 14:43:17 -07001223static void pn533_listen_mode_timer(struct timer_list *t)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001224{
Kees Cooke99e88a2017-10-16 14:43:17 -07001225 struct pn533 *dev = from_timer(dev, t, listen_timer);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001226
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001227 dev_dbg(dev->dev, "Listen mode timeout\n");
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001228
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001229 dev->cancel_listen = 1;
1230
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001231 pn533_poll_next_mod(dev);
1232
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001233 queue_delayed_work(dev->wq, &dev->poll_work,
1234 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001235}
1236
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001237static int pn533_rf_complete(struct pn533 *dev, void *arg,
1238 struct sk_buff *resp)
1239{
1240 int rc = 0;
1241
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001242 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001243
1244 if (IS_ERR(resp)) {
1245 rc = PTR_ERR(resp);
1246
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001247 nfc_err(dev->dev, "RF setting error %d\n", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001248
1249 return rc;
1250 }
1251
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001252 queue_delayed_work(dev->wq, &dev->poll_work,
1253 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001254
1255 dev_kfree_skb(resp);
1256 return rc;
1257}
1258
1259static void pn533_wq_rf(struct work_struct *work)
1260{
1261 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1262 struct sk_buff *skb;
1263 int rc;
1264
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001265 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001266
1267 skb = pn533_alloc_skb(dev, 2);
1268 if (!skb)
1269 return;
1270
Johannes Berg634fef62017-06-16 14:29:24 +02001271 skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
1272 skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001273
1274 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1275 pn533_rf_complete, NULL);
1276 if (rc < 0) {
1277 dev_kfree_skb(skb);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001278 nfc_err(dev->dev, "RF setting error %d\n", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001279 }
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001280}
1281
Samuel Ortiz673088f2013-08-21 15:06:55 +02001282static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1283 struct sk_buff *resp)
1284{
1285 struct pn533_cmd_jump_dep_response *rsp;
1286 struct nfc_target nfc_target;
1287 u8 target_gt_len;
1288 int rc;
1289
1290 if (IS_ERR(resp))
1291 return PTR_ERR(resp);
1292
1293 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1294
1295 rc = rsp->status & PN533_CMD_RET_MASK;
1296 if (rc != PN533_CMD_RET_SUCCESS) {
1297 /* Not target found, turn radio off */
1298 queue_work(dev->wq, &dev->rf_work);
1299
1300 dev_kfree_skb(resp);
1301 return 0;
1302 }
1303
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001304 dev_dbg(dev->dev, "Creating new target");
Samuel Ortiz673088f2013-08-21 15:06:55 +02001305
1306 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1307 nfc_target.nfcid1_len = 10;
1308 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1309 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1310 if (rc)
1311 goto error;
1312
1313 dev->tgt_available_prots = 0;
1314 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1315
1316 /* ATR_RES general bytes are located at offset 17 */
1317 target_gt_len = resp->len - 17;
1318 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1319 rsp->gt, target_gt_len);
1320 if (!rc) {
1321 rc = nfc_dep_link_is_up(dev->nfc_dev,
1322 dev->nfc_dev->targets[0].idx,
1323 0, NFC_RF_INITIATOR);
1324
1325 if (!rc)
1326 pn533_poll_reset_mod_list(dev);
1327 }
1328error:
1329 dev_kfree_skb(resp);
1330 return rc;
1331}
1332
1333#define PASSIVE_DATA_LEN 5
1334static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1335{
1336 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1337 struct sk_buff *skb;
1338 int rc, skb_len;
1339 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1340 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1341
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001342 dev_dbg(dev->dev, "%s", __func__);
Samuel Ortiz673088f2013-08-21 15:06:55 +02001343
1344 if (!dev->gb) {
1345 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1346
1347 if (!dev->gb || !dev->gb_len) {
1348 dev->poll_dep = 0;
1349 queue_work(dev->wq, &dev->rf_work);
1350 }
1351 }
1352
1353 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1354 skb_len += PASSIVE_DATA_LEN;
1355
1356 /* NFCID3 */
1357 skb_len += NFC_NFCID3_MAXSIZE;
1358 nfcid3[0] = 0x1;
1359 nfcid3[1] = 0xfe;
1360 get_random_bytes(nfcid3 + 2, 6);
1361
1362 skb = pn533_alloc_skb(dev, skb_len);
1363 if (!skb)
1364 return -ENOMEM;
1365
Johannes Berg634fef62017-06-16 14:29:24 +02001366 skb_put_u8(skb, 0x01); /* Active */
1367 skb_put_u8(skb, 0x02); /* 424 kbps */
Samuel Ortiz673088f2013-08-21 15:06:55 +02001368
1369 next = skb_put(skb, 1); /* Next */
1370 *next = 0;
1371
1372 /* Copy passive data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001373 skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
Samuel Ortiz673088f2013-08-21 15:06:55 +02001374 *next |= 1;
1375
1376 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001377 skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
Samuel Ortiz673088f2013-08-21 15:06:55 +02001378 *next |= 2;
1379
Johannes Berg59ae1d12017-06-16 14:29:20 +02001380 skb_put_data(skb, dev->gb, dev->gb_len);
Samuel Ortiz673088f2013-08-21 15:06:55 +02001381 *next |= 4; /* We have some Gi */
1382
1383 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1384 pn533_poll_dep_complete, NULL);
1385
1386 if (rc < 0)
1387 dev_kfree_skb(skb);
1388
1389 return rc;
1390}
1391
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001392static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001393 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001394{
1395 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001396 int rc;
1397
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001398 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001399
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001400 if (IS_ERR(resp)) {
1401 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001402
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001403 nfc_err(dev->dev, "%s Poll complete error %d\n",
Joe Perches073a6252013-04-05 12:27:38 -07001404 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001405
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001406 if (rc == -ENOENT) {
1407 if (dev->poll_mod_count != 0)
1408 return rc;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001409 goto stop_poll;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001410 } else if (rc < 0) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001411 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001412 "Error %d when running poll\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001413 goto stop_poll;
1414 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001415 }
1416
1417 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1418
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001419 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001420 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001421 rc = pn533_init_target_complete(dev, resp);
1422 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001423 }
1424
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001425 /* Initiator mode */
1426 rc = pn533_start_poll_complete(dev, resp);
1427 if (!rc)
1428 goto done;
1429
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001430 if (!dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001431 dev_dbg(dev->dev, "Polling has been stopped\n");
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001432 goto done;
1433 }
1434
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001435 pn533_poll_next_mod(dev);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001436 /* Not target found, turn radio off */
1437 queue_work(dev->wq, &dev->rf_work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001438
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001439done:
1440 dev_kfree_skb(resp);
1441 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001442
1443stop_poll:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001444 nfc_err(dev->dev, "Polling operation has been stopped\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001445
Samuel Ortizad3823c2012-05-30 23:54:55 +02001446 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001447 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001448 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001449}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001450
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001451static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1452 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001453{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001454 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001455
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001456 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001457 if (!skb)
1458 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001459
Johannes Berg59ae1d12017-06-16 14:29:20 +02001460 skb_put_data(skb, &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001461
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001462 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001463}
1464
1465static int pn533_send_poll_frame(struct pn533 *dev)
1466{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001467 struct pn533_poll_modulations *mod;
1468 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001469 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001470 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001471
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001472 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001473
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001474 dev_dbg(dev->dev, "%s mod len %d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001475 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001476
Michael Thalmeiere997ebbe462016-03-25 15:46:51 +01001477 if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) {
Samuel Ortiz673088f2013-08-21 15:06:55 +02001478 dev->poll_dep = 0;
1479 return pn533_poll_dep(dev->nfc_dev);
1480 }
1481
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001482 if (mod->len == 0) { /* Listen mode */
1483 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001484 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001485 } else { /* Polling mode */
1486 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001487 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001488 }
1489
1490 if (!skb) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001491 nfc_err(dev->dev, "Failed to allocate skb\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001492 return -ENOMEM;
1493 }
1494
1495 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1496 NULL);
1497 if (rc < 0) {
1498 dev_kfree_skb(skb);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001499 nfc_err(dev->dev, "Polling loop error %d\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001500 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001501
1502 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001503}
1504
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001505static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001506{
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001507 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001508 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001509 int rc;
1510
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001511 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1512
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001513 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001514 "%s cancel_listen %d modulation len %d\n",
1515 __func__, dev->cancel_listen, cur_mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001516
1517 if (dev->cancel_listen == 1) {
1518 dev->cancel_listen = 0;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001519 dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001520 }
1521
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001522 rc = pn533_send_poll_frame(dev);
1523 if (rc)
1524 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001525
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001526 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1527 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001528}
1529
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001530static int pn533_start_poll(struct nfc_dev *nfc_dev,
1531 u32 im_protocols, u32 tm_protocols)
1532{
1533 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02001534 struct pn533_poll_modulations *cur_mod;
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02001535 u8 rand_mod;
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02001536 int rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001537
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001538 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001539 "%s: im protocols 0x%x tm protocols 0x%x\n",
1540 __func__, im_protocols, tm_protocols);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001541
1542 if (dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001543 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001544 "Cannot poll with a target already activated\n");
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001545 return -EBUSY;
1546 }
1547
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001548 if (dev->tgt_mode) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001549 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001550 "Cannot poll while already being activated\n");
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001551 return -EBUSY;
1552 }
1553
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001554 if (tm_protocols) {
1555 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1556 if (dev->gb == NULL)
1557 tm_protocols = 0;
1558 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001559
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001560 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1561 dev->poll_protocols = im_protocols;
1562 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001563
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02001564 /* Do not always start polling from the same modulation */
1565 get_random_bytes(&rand_mod, sizeof(rand_mod));
1566 rand_mod %= dev->poll_mod_count;
1567 dev->poll_mod_curr = rand_mod;
1568
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02001569 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1570
1571 rc = pn533_send_poll_frame(dev);
1572
1573 /* Start listen timer */
1574 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
1575 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1576
1577 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001578}
1579
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001580static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1581{
1582 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1583
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001584 del_timer(&dev->listen_timer);
1585
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001586 if (!dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001587 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001588 "Polling operation was not running\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001589 return;
1590 }
1591
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001592 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001593 flush_delayed_work(&dev->poll_work);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001594 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001595}
1596
1597static int pn533_activate_target_nfcdep(struct pn533 *dev)
1598{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001599 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001600 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001601 int rc;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001602 struct sk_buff *skb;
1603 struct sk_buff *resp;
1604
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001605 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001606
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001607 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001608 if (!skb)
1609 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001610
Johannes Berg634fef62017-06-16 14:29:24 +02001611 skb_put_u8(skb, 1); /* TG */
1612 skb_put_u8(skb, 0); /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001613
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001614 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1615 if (IS_ERR(resp))
1616 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001617
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001618 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001619 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001620 if (rc != PN533_CMD_RET_SUCCESS) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001621 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001622 "Target activation failed (error 0x%x)\n", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001623 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001624 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001625 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001626
Samuel Ortiz541d9202011-12-14 16:43:10 +01001627 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001628 gt_len = resp->len - 16;
1629 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001630
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001631 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001632 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001633}
1634
Eric Lapuyade90099432012-05-07 12:31:13 +02001635static int pn533_activate_target(struct nfc_dev *nfc_dev,
1636 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001637{
1638 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1639 int rc;
1640
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001641 dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001642
1643 if (dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001644 nfc_err(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001645 "Cannot activate while polling\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001646 return -EBUSY;
1647 }
1648
1649 if (dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001650 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001651 "There is already an active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001652 return -EBUSY;
1653 }
1654
1655 if (!dev->tgt_available_prots) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001656 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001657 "There is no available target to activate\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001658 return -EINVAL;
1659 }
1660
1661 if (!(dev->tgt_available_prots & (1 << protocol))) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001662 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001663 "Target doesn't support requested proto %u\n",
1664 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001665 return -EINVAL;
1666 }
1667
1668 if (protocol == NFC_PROTO_NFC_DEP) {
1669 rc = pn533_activate_target_nfcdep(dev);
1670 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001671 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001672 "Activating target with DEP failed %d\n", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001673 return rc;
1674 }
1675 }
1676
1677 dev->tgt_active_prot = protocol;
1678 dev->tgt_available_prots = 0;
1679
1680 return 0;
1681}
1682
Michael Thalmeier37f895d72016-03-25 15:46:52 +01001683static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1684 struct sk_buff *resp)
1685{
1686 int rc = 0;
1687
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001688 dev_dbg(dev->dev, "%s\n", __func__);
Michael Thalmeier37f895d72016-03-25 15:46:52 +01001689
1690 if (IS_ERR(resp)) {
1691 rc = PTR_ERR(resp);
1692
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001693 nfc_err(dev->dev, "Target release error %d\n", rc);
Michael Thalmeier37f895d72016-03-25 15:46:52 +01001694
1695 return rc;
1696 }
1697
1698 rc = resp->data[0] & PN533_CMD_RET_MASK;
1699 if (rc != PN533_CMD_RET_SUCCESS)
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001700 nfc_err(dev->dev,
Michael Thalmeier37f895d72016-03-25 15:46:52 +01001701 "Error 0x%x when releasing the target\n", rc);
1702
1703 dev_kfree_skb(resp);
1704 return rc;
1705}
1706
Eric Lapuyade90099432012-05-07 12:31:13 +02001707static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
Christophe Ricard96d45812015-10-25 22:54:43 +01001708 struct nfc_target *target, u8 mode)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001709{
1710 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001711 struct sk_buff *skb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001712 int rc;
1713
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001714 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001715
1716 if (!dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001717 nfc_err(dev->dev, "There is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001718 return;
1719 }
1720
1721 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001722 skb_queue_purge(&dev->resp_q);
1723
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001724 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001725 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001726 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001727
Johannes Berg634fef62017-06-16 14:29:24 +02001728 skb_put_u8(skb, 1); /* TG*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001729
Michael Thalmeier37f895d72016-03-25 15:46:52 +01001730 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
1731 pn533_deactivate_target_complete, NULL);
1732 if (rc < 0) {
1733 dev_kfree_skb(skb);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001734 nfc_err(dev->dev, "Target release error %d\n", rc);
Michael Thalmeier37f895d72016-03-25 15:46:52 +01001735 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001736}
1737
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001738
1739static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001740 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001741{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001742 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001743 u8 target_gt_len;
1744 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001745 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001746
1747 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001748
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001749 if (IS_ERR(resp))
1750 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001751
1752 if (dev->tgt_available_prots &&
1753 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001754 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001755 "The target does not support DEP\n");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001756 rc = -EINVAL;
1757 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001758 }
1759
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001760 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1761
1762 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001763 if (rc != PN533_CMD_RET_SUCCESS) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001764 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001765 "Bringing DEP link up failed (error 0x%x)\n", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001766 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001767 }
1768
1769 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001770 struct nfc_target nfc_target;
1771
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001772 dev_dbg(dev->dev, "Creating new target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001773
1774 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001775 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001776 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001777 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1778 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001779 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001780
1781 dev->tgt_available_prots = 0;
1782 }
1783
1784 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1785
1786 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001787 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001788 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001789 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001790 if (rc == 0)
1791 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001792 dev->nfc_dev->targets[0].idx,
1793 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001794
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001795error:
1796 dev_kfree_skb(resp);
1797 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001798}
1799
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001800static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
Eric Lapuyade90099432012-05-07 12:31:13 +02001801static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001802 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001803{
1804 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001805 struct sk_buff *skb;
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001806 int rc, skb_len;
1807 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
Samuel Ortizd7f33452012-05-29 21:45:21 +02001808 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001809
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001810 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001811
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001812 if (dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001813 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001814 "Cannot bring the DEP link up while polling\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001815 return -EBUSY;
1816 }
1817
1818 if (dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001819 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001820 "There is already an active target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001821 return -EBUSY;
1822 }
1823
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001824 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001825 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001826
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001827 /* NFCID3 */
1828 skb_len += NFC_NFCID3_MAXSIZE;
1829 if (target && !target->nfcid2_len) {
1830 nfcid3[0] = 0x1;
1831 nfcid3[1] = 0xfe;
1832 get_random_bytes(nfcid3 + 2, 6);
1833 }
Samuel Ortiz322bce92013-05-27 15:29:11 +02001834
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001835 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001836 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001837 return -ENOMEM;
1838
Johannes Berg634fef62017-06-16 14:29:24 +02001839 skb_put_u8(skb, !comm_mode); /* ActPass */
1840 skb_put_u8(skb, 0x02); /* 424 kbps */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001841
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001842 next = skb_put(skb, 1); /* Next */
1843 *next = 0;
1844
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001845 /* Copy passive data */
Johannes Berg59ae1d12017-06-16 14:29:20 +02001846 skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001847 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001848
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001849 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1850 if (target && target->nfcid2_len)
Samuel Ortiz322bce92013-05-27 15:29:11 +02001851 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
1852 target->nfcid2_len);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001853 else
Johannes Berg59ae1d12017-06-16 14:29:20 +02001854 skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001855 *next |= 2;
Samuel Ortiz322bce92013-05-27 15:29:11 +02001856
Samuel Ortiz47807d32012-03-05 01:03:50 +01001857 if (gb != NULL && gb_len > 0) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001858 skb_put_data(skb, gb, gb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001859 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001860 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001861 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001862 }
1863
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001864 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1865 if (!arg) {
1866 dev_kfree_skb(skb);
1867 return -ENOMEM;
1868 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001869
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001870 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001871
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001872 pn533_rf_field(dev->nfc_dev, 0);
1873
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001874 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1875 pn533_in_dep_link_up_complete, arg);
1876
1877 if (rc < 0) {
1878 dev_kfree_skb(skb);
1879 kfree(arg);
1880 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001881
1882 return rc;
1883}
1884
1885static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1886{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001887 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1888
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001889 dev_dbg(dev->dev, "%s\n", __func__);
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001890
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001891 pn533_poll_reset_mod_list(dev);
1892
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001893 if (dev->tgt_mode || dev->tgt_active_prot)
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001894 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001895
1896 dev->tgt_active_prot = 0;
1897 dev->tgt_mode = 0;
1898
1899 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001900
1901 return 0;
1902}
1903
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001904struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001905 data_exchange_cb_t cb;
1906 void *cb_context;
1907};
1908
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001909static struct sk_buff *pn533_build_response(struct pn533 *dev)
1910{
1911 struct sk_buff *skb, *tmp, *t;
1912 unsigned int skb_len = 0, tmp_len = 0;
1913
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001914 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001915
1916 if (skb_queue_empty(&dev->resp_q))
1917 return NULL;
1918
1919 if (skb_queue_len(&dev->resp_q) == 1) {
1920 skb = skb_dequeue(&dev->resp_q);
1921 goto out;
1922 }
1923
1924 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1925 skb_len += tmp->len;
1926
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001927 dev_dbg(dev->dev, "%s total length %d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001928 __func__, skb_len);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001929
1930 skb = alloc_skb(skb_len, GFP_KERNEL);
1931 if (skb == NULL)
1932 goto out;
1933
1934 skb_put(skb, skb_len);
1935
1936 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1937 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1938 tmp_len += tmp->len;
1939 }
1940
1941out:
1942 skb_queue_purge(&dev->resp_q);
1943
1944 return skb;
1945}
1946
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001947static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001948 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001949{
1950 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001951 struct sk_buff *skb;
1952 int rc = 0;
1953 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001954
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001955 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001956
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001957 if (IS_ERR(resp)) {
1958 rc = PTR_ERR(resp);
1959 goto _error;
1960 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001961
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001962 status = resp->data[0];
1963 ret = status & PN533_CMD_RET_MASK;
1964 mi = status & PN533_CMD_MI_MASK;
1965
1966 skb_pull(resp, sizeof(status));
1967
1968 if (ret != PN533_CMD_RET_SUCCESS) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001969 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001970 "Exchanging data failed (error 0x%x)\n", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001971 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001972 goto error;
1973 }
1974
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001975 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001976
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001977 if (mi) {
1978 dev->cmd_complete_mi_arg = arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +00001979 queue_work(dev->wq, &dev->mi_rx_work);
1980 return -EINPROGRESS;
1981 }
1982
1983 /* Prepare for the next round */
1984 if (skb_queue_len(&dev->fragment_skb) > 0) {
1985 dev->cmd_complete_dep_arg = arg;
1986 queue_work(dev->wq, &dev->mi_tx_work);
1987
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001988 return -EINPROGRESS;
1989 }
1990
1991 skb = pn533_build_response(dev);
Julia Lawall5df848f2015-04-05 14:06:36 +02001992 if (!skb) {
1993 rc = -ENOMEM;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001994 goto error;
Julia Lawall5df848f2015-04-05 14:06:36 +02001995 }
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001996
1997 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001998 kfree(arg);
1999 return 0;
2000
2001error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002002 dev_kfree_skb(resp);
2003_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002004 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002005 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002006 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002007 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002008}
2009
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002010/*
2011 * Receive an incoming pn533 frame. skb contains only header and payload.
2012 * If skb == NULL, it is a notification that the link below is dead.
2013 */
2014void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
2015{
Michael Thalmeierb31d5102016-04-21 16:43:53 +02002016 if (!dev->cmd)
2017 goto sched_wq;
2018
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002019 dev->cmd->status = status;
2020
Michael Thalmeierb31d5102016-04-21 16:43:53 +02002021 if (status != 0) {
2022 dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
2023 goto sched_wq;
2024 }
2025
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002026 if (skb == NULL) {
2027 pr_err("NULL Frame -> link is dead\n");
2028 goto sched_wq;
2029 }
2030
2031 if (pn533_rx_frame_is_ack(skb->data)) {
2032 dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2033 dev_kfree_skb(skb);
2034 return;
2035 }
2036
2037 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2038 dev->ops->rx_frame_size(skb->data), false);
2039
2040 if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2041 nfc_err(dev->dev, "Received an invalid frame\n");
2042 dev->cmd->status = -EIO;
2043 } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2044 nfc_err(dev->dev, "It it not the response to the last command\n");
2045 dev->cmd->status = -EIO;
2046 }
2047
2048 dev->cmd->resp = skb;
2049
2050sched_wq:
2051 queue_work(dev->wq, &dev->cmd_complete_work);
2052}
2053EXPORT_SYMBOL(pn533_recv_frame);
2054
Olivier Guiter963a82e2013-06-13 13:43:28 +00002055/* Split the Tx skb into small chunks */
2056static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2057{
2058 struct sk_buff *frag;
2059 int frag_size;
2060
2061 do {
2062 /* Remaining size */
2063 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2064 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2065 else
2066 frag_size = skb->len;
2067
2068 /* Allocate and reserve */
2069 frag = pn533_alloc_skb(dev, frag_size);
2070 if (!frag) {
2071 skb_queue_purge(&dev->fragment_skb);
2072 break;
2073 }
2074
Olivier Guiter22953f92013-09-23 12:24:37 +02002075 if (!dev->tgt_mode) {
2076 /* Reserve the TG/MI byte */
2077 skb_reserve(frag, 1);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002078
Olivier Guiter22953f92013-09-23 12:24:37 +02002079 /* MI + TG */
2080 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
Johannes Bergd58ff352017-06-16 14:29:23 +02002081 *(u8 *)skb_push(frag, sizeof(u8)) =
2082 (PN533_CMD_MI_MASK | 1);
Olivier Guiter22953f92013-09-23 12:24:37 +02002083 else
Johannes Bergd58ff352017-06-16 14:29:23 +02002084 *(u8 *)skb_push(frag, sizeof(u8)) = 1; /* TG */
Olivier Guiter22953f92013-09-23 12:24:37 +02002085 }
Olivier Guiter963a82e2013-06-13 13:43:28 +00002086
Johannes Berg59ae1d12017-06-16 14:29:20 +02002087 skb_put_data(frag, skb->data, frag_size);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002088
2089 /* Reduce the size of incoming buffer */
2090 skb_pull(skb, frag_size);
2091
2092 /* Add this to skb_queue */
2093 skb_queue_tail(&dev->fragment_skb, frag);
2094
2095 } while (skb->len > 0);
2096
2097 dev_kfree_skb(skb);
2098
2099 return skb_queue_len(&dev->fragment_skb);
2100}
2101
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002102static int pn533_transceive(struct nfc_dev *nfc_dev,
2103 struct nfc_target *target, struct sk_buff *skb,
2104 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002105{
2106 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002107 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002108 int rc;
2109
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002110 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111
2112 if (!dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002113 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002114 "Can't exchange data if there is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002115 rc = -EINVAL;
2116 goto error;
2117 }
2118
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002119 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002120 if (!arg) {
2121 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002122 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123 }
2124
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002125 arg->cb = cb;
2126 arg->cb_context = cb_context;
2127
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002128 switch (dev->device_type) {
2129 case PN533_DEVICE_PASORI:
2130 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2131 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2132 skb,
2133 pn533_data_exchange_complete,
2134 arg);
2135
2136 break;
2137 }
Gustavo A. R. Silva9fe0a752019-02-13 12:54:03 -06002138 /* fall through */
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002139 default:
Olivier Guiter963a82e2013-06-13 13:43:28 +00002140 /* jumbo frame ? */
2141 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2142 rc = pn533_fill_fragment_skbs(dev, skb);
2143 if (rc <= 0)
2144 goto error;
2145
2146 skb = skb_dequeue(&dev->fragment_skb);
2147 if (!skb) {
2148 rc = -EIO;
2149 goto error;
2150 }
2151 } else {
Johannes Bergd58ff352017-06-16 14:29:23 +02002152 *(u8 *)skb_push(skb, sizeof(u8)) = 1; /* TG */
Olivier Guiter963a82e2013-06-13 13:43:28 +00002153 }
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002154
2155 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2156 skb, pn533_data_exchange_complete,
2157 arg);
2158
2159 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002160 }
2161
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002162 if (rc < 0) /* rc from send_async */
2163 goto error;
2164
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002165 return 0;
2166
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002167error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002168 kfree(arg);
2169 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002170 return rc;
2171}
2172
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002173static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002174 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002175{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002176 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002177
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002178 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002179
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002180 if (IS_ERR(resp))
2181 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002182
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002183 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002184
Olivier Guiter93ad4202013-09-23 12:24:39 +02002185 /* Prepare for the next round */
2186 if (skb_queue_len(&dev->fragment_skb) > 0) {
2187 queue_work(dev->wq, &dev->mi_tm_tx_work);
2188 return -EINPROGRESS;
2189 }
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002190 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002191
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002192 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002193 nfc_tm_deactivated(dev->nfc_dev);
2194
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002195 dev->tgt_mode = 0;
2196
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002197 return 0;
2198 }
2199
2200 queue_work(dev->wq, &dev->tg_work);
2201
2202 return 0;
2203}
2204
2205static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2206{
2207 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002208 int rc;
2209
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002210 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002211
Olivier Guiter93ad4202013-09-23 12:24:39 +02002212 /* let's split in multiple chunks if size's too big */
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002213 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Olivier Guiter93ad4202013-09-23 12:24:39 +02002214 rc = pn533_fill_fragment_skbs(dev, skb);
2215 if (rc <= 0)
2216 goto error;
2217
2218 /* get the first skb */
2219 skb = skb_dequeue(&dev->fragment_skb);
2220 if (!skb) {
2221 rc = -EIO;
2222 goto error;
2223 }
2224
2225 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2226 pn533_tm_send_complete, NULL);
2227 } else {
2228 /* Send th skb */
2229 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2230 pn533_tm_send_complete, NULL);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002231 }
2232
Olivier Guiter93ad4202013-09-23 12:24:39 +02002233error:
2234 if (rc < 0) {
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002235 dev_kfree_skb(skb);
Olivier Guiter93ad4202013-09-23 12:24:39 +02002236 skb_queue_purge(&dev->fragment_skb);
2237 }
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002238
2239 return rc;
2240}
2241
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002242static void pn533_wq_mi_recv(struct work_struct *work)
2243{
Olivier Guiter963a82e2013-06-13 13:43:28 +00002244 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002245 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002246 int rc;
2247
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002248 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002249
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002250 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002251 if (!skb)
2252 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002253
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002254 switch (dev->device_type) {
2255 case PN533_DEVICE_PASORI:
2256 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2257 rc = pn533_send_cmd_direct_async(dev,
2258 PN533_CMD_IN_COMM_THRU,
2259 skb,
2260 pn533_data_exchange_complete,
2261 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002262
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002263 break;
2264 }
Gustavo A. R. Silva9fe0a752019-02-13 12:54:03 -06002265 /* fall through */
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002266 default:
Johannes Berg634fef62017-06-16 14:29:24 +02002267 skb_put_u8(skb, 1); /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002268
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002269 rc = pn533_send_cmd_direct_async(dev,
2270 PN533_CMD_IN_DATA_EXCHANGE,
2271 skb,
2272 pn533_data_exchange_complete,
2273 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002274
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002275 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002276 }
2277
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002278 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002279 return;
2280
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002281 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002282 "Error %d when trying to perform data_exchange\n", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002283
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002284 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002285 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002286
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002287error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002288 dev->phy_ops->send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002289 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002290}
2291
Olivier Guiter963a82e2013-06-13 13:43:28 +00002292static void pn533_wq_mi_send(struct work_struct *work)
2293{
2294 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2295 struct sk_buff *skb;
2296 int rc;
2297
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002298 dev_dbg(dev->dev, "%s\n", __func__);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002299
2300 /* Grab the first skb in the queue */
2301 skb = skb_dequeue(&dev->fragment_skb);
2302
2303 if (skb == NULL) { /* No more data */
2304 /* Reset the queue for future use */
2305 skb_queue_head_init(&dev->fragment_skb);
2306 goto error;
2307 }
2308
2309 switch (dev->device_type) {
2310 case PN533_DEVICE_PASORI:
2311 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2312 rc = -EIO;
2313 break;
2314 }
2315
2316 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2317 skb,
2318 pn533_data_exchange_complete,
2319 dev->cmd_complete_dep_arg);
2320
2321 break;
2322
2323 default:
2324 /* Still some fragments? */
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002325 rc = pn533_send_cmd_direct_async(dev,
2326 PN533_CMD_IN_DATA_EXCHANGE,
Olivier Guiter963a82e2013-06-13 13:43:28 +00002327 skb,
2328 pn533_data_exchange_complete,
2329 dev->cmd_complete_dep_arg);
2330
2331 break;
2332 }
2333
2334 if (rc == 0) /* success */
2335 return;
2336
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002337 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002338 "Error %d when trying to perform data_exchange\n", rc);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002339
2340 dev_kfree_skb(skb);
2341 kfree(dev->cmd_complete_dep_arg);
2342
2343error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002344 dev->phy_ops->send_ack(dev, GFP_KERNEL);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002345 queue_work(dev->wq, &dev->cmd_work);
2346}
2347
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002348static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2349 u8 cfgdata_len)
2350{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002351 struct sk_buff *skb;
2352 struct sk_buff *resp;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002353 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002354
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002355 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002356
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002357 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002358
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002359 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002360 if (!skb)
2361 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002362
Johannes Berg634fef62017-06-16 14:29:24 +02002363 skb_put_u8(skb, cfgitem);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002364 skb_put_data(skb, cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002365
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002366 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2367 if (IS_ERR(resp))
2368 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002369
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002370 dev_kfree_skb(resp);
2371 return 0;
2372}
2373
2374static int pn533_get_firmware_version(struct pn533 *dev,
2375 struct pn533_fw_version *fv)
2376{
2377 struct sk_buff *skb;
2378 struct sk_buff *resp;
2379
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002380 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002381 if (!skb)
2382 return -ENOMEM;
2383
2384 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2385 if (IS_ERR(resp))
2386 return PTR_ERR(resp);
2387
2388 fv->ic = resp->data[0];
2389 fv->ver = resp->data[1];
2390 fv->rev = resp->data[2];
2391 fv->support = resp->data[3];
2392
2393 dev_kfree_skb(resp);
2394 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002395}
2396
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002397static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002398{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002399 struct sk_buff *skb;
2400 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002401
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002402 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002403
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002404 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002405 if (!skb)
2406 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002407
Johannes Berg634fef62017-06-16 14:29:24 +02002408 skb_put_u8(skb, 0x1);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002409
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002410 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2411 if (IS_ERR(resp))
2412 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002413
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002414 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002415
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002416 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002417}
2418
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002419static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2420{
2421 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2422 u8 rf_field = !!rf;
2423 int rc;
2424
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02002425 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2426
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002427 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2428 (u8 *)&rf_field, 1);
2429 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002430 nfc_err(dev->dev, "Error on setting RF field\n");
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002431 return rc;
2432 }
2433
2434 return rc;
2435}
2436
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002437static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2438{
2439 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2440 struct sk_buff *skb;
2441 struct sk_buff *resp;
2442
2443 skb = pn533_alloc_skb(dev, 1);
2444 if (!skb)
2445 return -ENOMEM;
2446
Johannes Berg634fef62017-06-16 14:29:24 +02002447 skb_put_u8(skb, 0x01);
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002448
2449 resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2450 if (IS_ERR(resp))
2451 return PTR_ERR(resp);
2452
2453 dev_kfree_skb(resp);
2454 return 0;
2455}
2456
Sachin Kamate44666b2013-09-20 14:30:41 +05302457static int pn533_dev_up(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002458{
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002459 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2460
2461 if (dev->device_type == PN533_DEVICE_PN532) {
2462 int rc = pn532_sam_configuration(nfc_dev);
2463
2464 if (rc)
2465 return rc;
2466 }
2467
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002468 return pn533_rf_field(nfc_dev, 1);
2469}
2470
Sachin Kamate44666b2013-09-20 14:30:41 +05302471static int pn533_dev_down(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002472{
2473 return pn533_rf_field(nfc_dev, 0);
2474}
2475
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002476static struct nfc_ops pn533_nfc_ops = {
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002477 .dev_up = pn533_dev_up,
2478 .dev_down = pn533_dev_down,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002479 .dep_link_up = pn533_dep_link_up,
2480 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002481 .start_poll = pn533_start_poll,
2482 .stop_poll = pn533_stop_poll,
2483 .activate_target = pn533_activate_target,
2484 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002485 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002486 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002487};
2488
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002489static int pn533_setup(struct pn533 *dev)
2490{
2491 struct pn533_config_max_retries max_retries;
2492 struct pn533_config_timing timing;
2493 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2494 int rc;
2495
2496 switch (dev->device_type) {
2497 case PN533_DEVICE_STD:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002498 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002499 case PN533_DEVICE_ACR122U:
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002500 case PN533_DEVICE_PN532:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002501 max_retries.mx_rty_atr = 0x2;
2502 max_retries.mx_rty_psl = 0x1;
2503 max_retries.mx_rty_passive_act =
2504 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2505
2506 timing.rfu = PN533_CONFIG_TIMING_102;
2507 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2508 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2509
2510 break;
2511
2512 default:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002513 nfc_err(dev->dev, "Unknown device type %d\n",
Joe Perches073a6252013-04-05 12:27:38 -07002514 dev->device_type);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002515 return -EINVAL;
2516 }
2517
2518 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2519 (u8 *)&max_retries, sizeof(max_retries));
2520 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002521 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002522 "Error on setting MAX_RETRIES config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002523 return rc;
2524 }
2525
2526
2527 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2528 (u8 *)&timing, sizeof(timing));
2529 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002530 nfc_err(dev->dev, "Error on setting RF timings\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002531 return rc;
2532 }
2533
2534 switch (dev->device_type) {
2535 case PN533_DEVICE_STD:
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002536 case PN533_DEVICE_PN532:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002537 break;
2538
2539 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002540 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002541
2542 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2543 pasori_cfg, 3);
2544 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002545 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002546 "Error while settings PASORI config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002547 return rc;
2548 }
2549
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002550 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002551
2552 break;
2553 }
2554
2555 return 0;
2556}
2557
Andrey Rusalin32ecc752016-12-28 20:10:59 +03002558int pn533_finalize_setup(struct pn533 *dev)
2559{
2560
2561 struct pn533_fw_version fw_ver;
2562 int rc;
2563
2564 memset(&fw_ver, 0, sizeof(fw_ver));
2565
2566 rc = pn533_get_firmware_version(dev, &fw_ver);
2567 if (rc) {
2568 nfc_err(dev->dev, "Unable to get FW version\n");
2569 return rc;
2570 }
2571
2572 nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
2573 fw_ver.ic, fw_ver.ver, fw_ver.rev);
2574
2575 rc = pn533_setup(dev);
2576 if (rc)
2577 return rc;
2578
2579 return 0;
2580}
2581EXPORT_SYMBOL_GPL(pn533_finalize_setup);
2582
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002583struct pn533 *pn533_register_device(u32 device_type,
2584 u32 protocols,
2585 enum pn533_protocol_type protocol_type,
2586 void *phy,
2587 struct pn533_phy_ops *phy_ops,
2588 struct pn533_frame_ops *fops,
Michael Thalmeierb16931b2016-04-21 16:43:50 +02002589 struct device *dev,
2590 struct device *parent)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002591{
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002592 struct pn533 *priv;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002593 int rc = -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002594
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002595 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2596 if (!priv)
2597 return ERR_PTR(-ENOMEM);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002598
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002599 priv->phy = phy;
2600 priv->phy_ops = phy_ops;
2601 priv->dev = dev;
2602 if (fops != NULL)
2603 priv->ops = fops;
2604 else
2605 priv->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002606
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002607 priv->protocol_type = protocol_type;
2608 priv->device_type = device_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002609
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002610 mutex_init(&priv->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002611
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002612 INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2613 INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2614 INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2615 INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2616 INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2617 INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2618 INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2619 INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2620 INIT_WORK(&priv->rf_work, pn533_wq_rf);
2621 priv->wq = alloc_ordered_workqueue("pn533", 0);
2622 if (priv->wq == NULL)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002623 goto error;
2624
Kees Cooke99e88a2017-10-16 14:43:17 -07002625 timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002626
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002627 skb_queue_head_init(&priv->resp_q);
2628 skb_queue_head_init(&priv->fragment_skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002629
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002630 INIT_LIST_HEAD(&priv->cmd_queue);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002631
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002632 priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2633 priv->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002634 PN533_CMD_DATAEXCH_HEAD_LEN,
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002635 priv->ops->tx_tail_len);
2636 if (!priv->nfc_dev) {
Wei Yongjun4674d0f2013-05-07 20:07:52 +08002637 rc = -ENOMEM;
Samuel Ortiz4849f852012-04-10 19:43:17 +02002638 goto destroy_wq;
Wei Yongjun4674d0f2013-05-07 20:07:52 +08002639 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002640
Michael Thalmeierb16931b2016-04-21 16:43:50 +02002641 nfc_set_parent_dev(priv->nfc_dev, parent);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002642 nfc_set_drvdata(priv->nfc_dev, priv);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002643
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002644 rc = nfc_register_device(priv->nfc_dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002645 if (rc)
2646 goto free_nfc_dev;
2647
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002648 return priv;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002649
2650free_nfc_dev:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002651 nfc_free_device(priv->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002652
Samuel Ortiz4849f852012-04-10 19:43:17 +02002653destroy_wq:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002654 destroy_workqueue(priv->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002655error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002656 kfree(priv);
2657 return ERR_PTR(rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002658}
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002659EXPORT_SYMBOL_GPL(pn533_register_device);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002660
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002661void pn533_unregister_device(struct pn533 *priv)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002662{
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002663 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002664
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002665 nfc_unregister_device(priv->nfc_dev);
2666 nfc_free_device(priv->nfc_dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002667
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002668 flush_delayed_work(&priv->poll_work);
2669 destroy_workqueue(priv->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002670
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002671 skb_queue_purge(&priv->resp_q);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002672
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002673 del_timer(&priv->listen_timer);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002674
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002675 list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002676 list_del(&cmd->queue);
2677 kfree(cmd);
2678 }
2679
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002680 kfree(priv);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002681}
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002682EXPORT_SYMBOL_GPL(pn533_unregister_device);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002683
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002684
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002685MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2686MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2687MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002688MODULE_DESCRIPTION("PN533 driver ver " VERSION);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002689MODULE_VERSION(VERSION);
2690MODULE_LICENSE("GPL");