blob: d9c55830b2b26a1fb67b41562b3b16c0b09f8830 [file] [log] [blame]
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001/*
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002 * Driver for NXP PN533 NFC Chip - core functions
3 *
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03004 * Copyright (C) 2011 Instituto Nokia de Tecnologia
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02005 * Copyright (C) 2012-2013 Tieto Poland
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080018 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030019 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030025#include <linux/nfc.h>
26#include <linux/netdevice.h>
Ilan Elias55eb94f2011-09-18 11:19:34 +030027#include <net/nfc/nfc.h>
Michael Thalmeier9815c7c2016-03-25 15:46:53 +010028#include "pn533.h"
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030029
Michael Thalmeier9815c7c2016-03-25 15:46:53 +010030#define VERSION "0.3"
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030031
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020032/* How much time we spend listening for initiators */
33#define PN533_LISTEN_TIME 2
Samuel Ortiz46f793b2013-07-03 14:50:36 +020034/* Delay between each poll frame (ms) */
35#define PN533_POLL_INTERVAL 10
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +020036
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030037/* structs for pn533 commands */
38
39/* PN533_CMD_GET_FIRMWARE_VERSION */
40struct pn533_fw_version {
41 u8 ic;
42 u8 ver;
43 u8 rev;
44 u8 support;
45};
46
47/* PN533_CMD_RF_CONFIGURATION */
Samuel Ortiz60d9edd52013-04-11 11:45:41 +020048#define PN533_CFGITEM_RF_FIELD 0x01
49#define PN533_CFGITEM_TIMING 0x02
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030050#define PN533_CFGITEM_MAX_RETRIES 0x05
Samuel Ortiz60d9edd52013-04-11 11:45:41 +020051#define PN533_CFGITEM_PASORI 0x82
52
Samuel Ortiz3a8eab32013-07-01 17:26:58 +020053#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
54#define PN533_CFGITEM_RF_FIELD_ON 0x1
55#define PN533_CFGITEM_RF_FIELD_OFF 0x0
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030056
Samuel Ortiz34a85bf2012-05-29 21:34:08 +020057#define PN533_CONFIG_TIMING_102 0xb
58#define PN533_CONFIG_TIMING_204 0xc
59#define PN533_CONFIG_TIMING_409 0xd
60#define PN533_CONFIG_TIMING_819 0xe
61
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030062#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
63#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
64
65struct pn533_config_max_retries {
66 u8 mx_rty_atr;
67 u8 mx_rty_psl;
68 u8 mx_rty_passive_act;
69} __packed;
70
Samuel Ortiz34a85bf2012-05-29 21:34:08 +020071struct pn533_config_timing {
72 u8 rfu;
73 u8 atr_res_timeout;
74 u8 dep_timeout;
75} __packed;
76
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -030077/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
78
79/* felica commands opcode */
80#define PN533_FELICA_OPC_SENSF_REQ 0
81#define PN533_FELICA_OPC_SENSF_RES 1
82/* felica SENSF_REQ parameters */
83#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
84#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
85#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
86#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
87
88/* type B initiator_data values */
89#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
90#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
91#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
92
93union pn533_cmd_poll_initdata {
94 struct {
95 u8 afi;
96 u8 polling_method;
97 } __packed type_b;
98 struct {
99 u8 opcode;
100 __be16 sc;
101 u8 rc;
102 u8 tsn;
103 } __packed felica;
104};
105
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300106struct pn533_poll_modulations {
107 struct {
108 u8 maxtg;
109 u8 brty;
110 union pn533_cmd_poll_initdata initiator_data;
111 } __packed data;
112 u8 len;
113};
114
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100115static const struct pn533_poll_modulations poll_mod[] = {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300116 [PN533_POLL_MOD_106KBPS_A] = {
117 .data = {
118 .maxtg = 1,
119 .brty = 0,
120 },
121 .len = 2,
122 },
123 [PN533_POLL_MOD_212KBPS_FELICA] = {
124 .data = {
125 .maxtg = 1,
126 .brty = 1,
127 .initiator_data.felica = {
128 .opcode = PN533_FELICA_OPC_SENSF_REQ,
129 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200130 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200131 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300132 },
133 },
134 .len = 7,
135 },
136 [PN533_POLL_MOD_424KBPS_FELICA] = {
137 .data = {
138 .maxtg = 1,
139 .brty = 2,
140 .initiator_data.felica = {
141 .opcode = PN533_FELICA_OPC_SENSF_REQ,
142 .sc = PN533_FELICA_SENSF_SC_ALL,
Samuel Ortiza94e10f2013-06-28 15:43:19 +0200143 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
Samuel Ortiz31c44462013-05-27 14:59:40 +0200144 .tsn = 0x03,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300145 },
146 },
147 .len = 7,
148 },
149 [PN533_POLL_MOD_106KBPS_JEWEL] = {
150 .data = {
151 .maxtg = 1,
152 .brty = 4,
153 },
154 .len = 2,
155 },
156 [PN533_POLL_MOD_847KBPS_B] = {
157 .data = {
158 .maxtg = 1,
159 .brty = 8,
160 .initiator_data.type_b = {
161 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
162 .polling_method =
163 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
164 },
165 },
166 .len = 3,
167 },
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200168 [PN533_LISTEN_MOD] = {
169 .len = 0,
170 },
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300171};
172
173/* PN533_CMD_IN_ATR */
174
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300175struct pn533_cmd_activate_response {
176 u8 status;
177 u8 nfcid3t[10];
178 u8 didt;
179 u8 bst;
180 u8 brt;
181 u8 to;
182 u8 ppt;
183 /* optional */
184 u8 gt[];
185} __packed;
186
Samuel Ortiz361f3cb2011-12-14 16:43:11 +0100187struct pn533_cmd_jump_dep_response {
188 u8 status;
189 u8 tg;
190 u8 nfcid3t[10];
191 u8 didt;
192 u8 bst;
193 u8 brt;
194 u8 to;
195 u8 ppt;
196 /* optional */
197 u8 gt[];
198} __packed;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300199
Samuel Ortizad3823c2012-05-30 23:54:55 +0200200
201/* PN533_TG_INIT_AS_TARGET */
202#define PN533_INIT_TARGET_PASSIVE 0x1
203#define PN533_INIT_TARGET_DEP 0x2
204
Samuel Ortizfc40a8c2012-06-01 13:21:13 +0200205#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
206#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
207#define PN533_INIT_TARGET_RESP_DEP 0x4
208
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200209/* The rule: value(high byte) + value(low byte) + checksum = 0 */
210static inline u8 pn533_ext_checksum(u16 value)
211{
212 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
213}
214
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300215/* The rule: value + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200216static inline u8 pn533_std_checksum(u8 value)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300217{
218 return ~value + 1;
219}
220
221/* The rule: sum(data elements) + checksum = 0 */
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200222static u8 pn533_std_data_checksum(u8 *data, int datalen)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300223{
224 u8 sum = 0;
225 int i;
226
227 for (i = 0; i < datalen; i++)
228 sum += data[i];
229
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200230 return pn533_std_checksum(sum);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300231}
232
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200233static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300234{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200235 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100236
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300237 frame->preamble = 0;
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200238 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
239 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200240 PN533_FRAME_CMD(frame) = cmd_code;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300241 frame->datalen = 2;
242}
243
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200244static void pn533_std_tx_frame_finish(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300245{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200246 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100247
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200248 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300249
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200250 PN533_STD_FRAME_CHECKSUM(frame) =
251 pn533_std_data_checksum(frame->data, frame->datalen);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300252
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200253 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300254}
255
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200256static void pn533_std_tx_update_payload_len(void *_frame, int len)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100257{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200258 struct pn533_std_frame *frame = _frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100259
260 frame->datalen += len;
261}
262
Olivier Guiter56a63c822013-06-13 13:43:29 +0000263static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300264{
265 u8 checksum;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200266 struct pn533_std_frame *stdf = _frame;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300267
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200268 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300269 return false;
270
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200271 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
272 /* Standard frame code */
Olivier Guiter56a63c822013-06-13 13:43:29 +0000273 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300274
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200275 checksum = pn533_std_checksum(stdf->datalen);
276 if (checksum != stdf->datalen_checksum)
277 return false;
278
279 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
280 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
281 return false;
282 } else {
283 /* Extended */
284 struct pn533_ext_frame *eif = _frame;
285
Olivier Guiter56a63c822013-06-13 13:43:29 +0000286 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
287
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200288 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
289 if (checksum != eif->datalen_checksum)
290 return false;
291
292 /* check data checksum */
293 checksum = pn533_std_data_checksum(eif->data,
294 be16_to_cpu(eif->datalen));
295 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
296 return false;
297 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300298
299 return true;
300}
301
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100302bool pn533_rx_frame_is_ack(void *_frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300303{
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100304 struct pn533_std_frame *frame = _frame;
305
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200306 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300307 return false;
308
309 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
310 return false;
311
312 return true;
313}
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100314EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300315
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200316static inline int pn533_std_rx_frame_size(void *frame)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300317{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200318 struct pn533_std_frame *f = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100319
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200320 /* check for Extended Information frame */
321 if (PN533_STD_IS_EXTENDED(f)) {
322 struct pn533_ext_frame *eif = frame;
323
324 return sizeof(struct pn533_ext_frame)
325 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
326 }
327
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200328 return sizeof(struct pn533_std_frame) + f->datalen +
329 PN533_STD_FRAME_TAIL_LEN;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100330}
331
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200332static u8 pn533_std_get_cmd_code(void *frame)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100333{
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200334 struct pn533_std_frame *f = frame;
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200335 struct pn533_ext_frame *eif = frame;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100336
Olivier Guiter1575b9d2013-06-13 15:43:27 +0200337 if (PN533_STD_IS_EXTENDED(f))
338 return PN533_FRAME_CMD(eif);
339 else
340 return PN533_FRAME_CMD(f);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100341}
342
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100343bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
344{
345 return (dev->ops->get_cmd_code(frame) ==
346 PN533_CMD_RESPONSE(dev->cmd->code));
347}
348EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);
349
350
Waldemar Rymarkiewiczef3d56e2013-01-16 15:51:51 +0100351static struct pn533_frame_ops pn533_std_frame_ops = {
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200352 .tx_frame_init = pn533_std_tx_frame_init,
353 .tx_frame_finish = pn533_std_tx_frame_finish,
354 .tx_update_payload_len = pn533_std_tx_update_payload_len,
355 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
356 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100357
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200358 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
359 .rx_frame_size = pn533_std_rx_frame_size,
360 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
361 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100362
Waldemar Rymarkiewicz63123102013-04-03 08:01:58 +0200363 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
364 .get_cmd_code = pn533_std_get_cmd_code,
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100365};
366
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100367static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
368 struct sk_buff *skb)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100369{
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100370 /* payload is already there, just update datalen */
371 int payload_len = skb->len;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100372 struct pn533_frame_ops *ops = dev->ops;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100373
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100374
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100375 skb_push(skb, ops->tx_header_len);
376 skb_put(skb, ops->tx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100377
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100378 ops->tx_frame_init(skb->data, cmd_code);
379 ops->tx_update_payload_len(skb->data, payload_len);
380 ops->tx_frame_finish(skb->data);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100381}
382
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200383static int pn533_send_async_complete(struct pn533 *dev)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100384{
Waldemar Rymarkiewiczddf19d22013-04-03 08:02:08 +0200385 struct pn533_cmd *cmd = dev->cmd;
Waldemar Rymarkiewiczf87bc9f2013-04-03 08:02:11 +0200386 int status = cmd->status;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100387
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200388 struct sk_buff *req = cmd->req;
389 struct sk_buff *resp = cmd->resp;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100390
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100391 int rc;
392
393 dev_kfree_skb(req);
394
Waldemar Rymarkiewicz0c33d262012-12-10 14:42:59 +0100395 if (status < 0) {
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200396 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
397 ERR_PTR(status));
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100398 dev_kfree_skb(resp);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200399 goto done;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100400 }
401
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100402 skb_pull(resp, dev->ops->rx_header_len);
403 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100404
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200405 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100406
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200407done:
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200408 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200409 dev->cmd = NULL;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100410 return rc;
411}
412
413static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100414 struct sk_buff *req,
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100415 pn533_send_async_complete_t complete_cb,
416 void *complete_cb_context)
417{
418 struct pn533_cmd *cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100419 int rc = 0;
420
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100421 dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100422
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200423 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
424 if (!cmd)
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100425 return -ENOMEM;
426
Waldemar Rymarkiewicz4b2a9532013-04-03 08:02:06 +0200427 cmd->code = cmd_code;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200428 cmd->req = req;
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200429 cmd->complete_cb = complete_cb;
430 cmd->complete_cb_context = complete_cb_context;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100431
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100432 pn533_build_cmd_frame(dev, cmd_code, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100433
434 mutex_lock(&dev->cmd_lock);
435
436 if (!dev->cmd_pending) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100437 rc = dev->phy_ops->send_frame(dev, req);
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100438 if (rc)
439 goto error;
440
441 dev->cmd_pending = 1;
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200442 dev->cmd = cmd;
Waldemar Rymarkiewiczaada17a2012-11-26 14:18:34 +0100443 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 *
490 * The function sends a piority cmd directly to the chip omiting the cmd
491 * 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
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100514 rc = dev->phy_ops->send_frame(dev, req);
515 if (rc < 0)
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200516 kfree(cmd);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100517 else
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200518 dev->cmd = cmd;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +0100519
520 return rc;
521}
522
Waldemar Rymarkiewiczc79490e2013-04-03 08:02:09 +0200523static void pn533_wq_cmd_complete(struct work_struct *work)
524{
525 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
526 int rc;
527
528 rc = pn533_send_async_complete(dev);
529 if (rc != -EINPROGRESS)
530 queue_work(dev->wq, &dev->cmd_work);
531}
532
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200533static void pn533_wq_cmd(struct work_struct *work)
534{
535 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
536 struct pn533_cmd *cmd;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200537 int rc;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200538
539 mutex_lock(&dev->cmd_lock);
540
541 if (list_empty(&dev->cmd_queue)) {
542 dev->cmd_pending = 0;
543 mutex_unlock(&dev->cmd_lock);
544 return;
545 }
546
547 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
548
Szymon Janc60ad07a2012-10-25 17:29:45 +0200549 list_del(&cmd->queue);
550
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200551 mutex_unlock(&dev->cmd_lock);
552
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100553 rc = dev->phy_ops->send_frame(dev, cmd->req);
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200554 if (rc < 0) {
555 dev_kfree_skb(cmd->req);
Waldemar Rymarkiewicz42316042013-04-03 08:02:04 +0200556 kfree(cmd);
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200557 return;
Waldemar Rymarkiewicz0ce1fbd2013-04-03 08:02:03 +0200558 }
Waldemar Rymarkiewicz2c206fb2013-04-03 08:02:05 +0200559
560 dev->cmd = cmd;
Samuel Ortiz5d50b362012-08-17 23:47:54 +0200561}
562
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300563struct pn533_sync_cmd_response {
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100564 struct sk_buff *resp;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300565 struct completion done;
566};
567
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100568static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
569 struct sk_buff *resp)
570{
571 struct pn533_sync_cmd_response *arg = _arg;
572
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100573 arg->resp = resp;
574 complete(&arg->done);
575
576 return 0;
577}
578
579/* pn533_send_cmd_sync
580 *
581 * Please note the req parameter is freed inside the function to
582 * limit a number of return value interpretations by the caller.
583 *
584 * 1. negative in case of error during TX path -> req should be freed
585 *
586 * 2. negative in case of error during RX path -> req should not be freed
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100587 * as it's been already freed at the beginning of RX path by
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100588 * async_complete_cb.
589 *
590 * 3. valid pointer in case of succesfult RX path
591 *
592 * A caller has to check a return value with IS_ERR macro. If the test pass,
593 * the returned pointer is valid.
594 *
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100595 */
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100596static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
597 struct sk_buff *req)
598{
599 int rc;
600 struct pn533_sync_cmd_response arg;
601
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +0100602 init_completion(&arg.done);
603
604 rc = pn533_send_cmd_async(dev, cmd_code, req,
605 pn533_send_sync_complete, &arg);
606 if (rc) {
607 dev_kfree_skb(req);
608 return ERR_PTR(rc);
609 }
610
611 wait_for_completion(&arg.done);
612
613 return arg.resp;
614}
615
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100616static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100617{
618 struct sk_buff *skb;
619
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100620 skb = alloc_skb(dev->ops->tx_header_len +
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100621 size +
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100622 dev->ops->tx_tail_len, GFP_KERNEL);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100623
624 if (skb)
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100625 skb_reserve(skb, dev->ops->tx_header_len);
Waldemar Rymarkiewiczd22b2db2012-11-26 14:18:37 +0100626
627 return skb;
628}
629
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300630struct pn533_target_type_a {
631 __be16 sens_res;
632 u8 sel_res;
633 u8 nfcid_len;
634 u8 nfcid_data[];
635} __packed;
636
637
638#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
639#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
640#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
641
642#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
643#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
644
645#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
646#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
647
648#define PN533_TYPE_A_SEL_PROT_MIFARE 0
649#define PN533_TYPE_A_SEL_PROT_ISO14443 1
650#define PN533_TYPE_A_SEL_PROT_DEP 2
651#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
652
653static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
654 int target_data_len)
655{
656 u8 ssd;
657 u8 platconf;
658
659 if (target_data_len < sizeof(struct pn533_target_type_a))
660 return false;
661
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100662 /*
663 * The length check of nfcid[] and ats[] are not being performed because
664 * the values are not being used
665 */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300666
667 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
668 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
669 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
670
671 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100672 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
673 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
674 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300675 return false;
676
677 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
678 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
679 return false;
680
681 return true;
682}
683
684static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
685 int tgt_data_len)
686{
687 struct pn533_target_type_a *tgt_type_a;
688
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100689 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300690
691 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
692 return -EPROTO;
693
694 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
695 case PN533_TYPE_A_SEL_PROT_MIFARE:
696 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
697 break;
698 case PN533_TYPE_A_SEL_PROT_ISO14443:
699 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
700 break;
701 case PN533_TYPE_A_SEL_PROT_DEP:
702 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
703 break;
704 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
705 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
706 NFC_PROTO_NFC_DEP_MASK;
707 break;
708 }
709
710 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
711 nfc_tgt->sel_res = tgt_type_a->sel_res;
Samuel Ortizc3b1e1e2012-03-05 01:03:33 +0100712 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
713 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300714
715 return 0;
716}
717
718struct pn533_target_felica {
719 u8 pol_res;
720 u8 opcode;
Samuel Ortiz322bce92013-05-27 15:29:11 +0200721 u8 nfcid2[NFC_NFCID2_MAXSIZE];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300722 u8 pad[8];
723 /* optional */
724 u8 syst_code[];
725} __packed;
726
727#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
728#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
729
730static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
731 int target_data_len)
732{
733 if (target_data_len < sizeof(struct pn533_target_felica))
734 return false;
735
736 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
737 return false;
738
739 return true;
740}
741
742static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
743 int tgt_data_len)
744{
745 struct pn533_target_felica *tgt_felica;
746
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100747 tgt_felica = (struct pn533_target_felica *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300748
749 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
750 return -EPROTO;
751
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100752 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
753 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300754 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
755 else
756 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
757
Samuel Ortiz79757542012-03-05 01:03:45 +0100758 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
759 nfc_tgt->sensf_res_len = 9;
760
Samuel Ortiz322bce92013-05-27 15:29:11 +0200761 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
762 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
763
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300764 return 0;
765}
766
767struct pn533_target_jewel {
768 __be16 sens_res;
769 u8 jewelid[4];
770} __packed;
771
772static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
773 int target_data_len)
774{
775 u8 ssd;
776 u8 platconf;
777
778 if (target_data_len < sizeof(struct pn533_target_jewel))
779 return false;
780
781 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
782 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
783 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
784
785 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
Waldemar Rymarkiewicz5d467742012-12-10 14:42:51 +0100786 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
787 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
788 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300789 return false;
790
791 return true;
792}
793
794static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
795 int tgt_data_len)
796{
797 struct pn533_target_jewel *tgt_jewel;
798
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100799 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300800
801 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
802 return -EPROTO;
803
804 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
805 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
Samuel Ortizd8dc1072012-03-05 01:03:46 +0100806 nfc_tgt->nfcid1_len = 4;
807 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300808
809 return 0;
810}
811
812struct pn533_type_b_prot_info {
813 u8 bitrate;
814 u8 fsci_type;
815 u8 fwi_adc_fo;
816} __packed;
817
818#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
819#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
820#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
821
822struct pn533_type_b_sens_res {
823 u8 opcode;
824 u8 nfcid[4];
825 u8 appdata[4];
826 struct pn533_type_b_prot_info prot_info;
827} __packed;
828
829#define PN533_TYPE_B_OPC_SENSB_RES 0x50
830
831struct pn533_target_type_b {
832 struct pn533_type_b_sens_res sensb_res;
833 u8 attrib_res_len;
834 u8 attrib_res[];
835} __packed;
836
837static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
838 int target_data_len)
839{
840 if (target_data_len < sizeof(struct pn533_target_type_b))
841 return false;
842
843 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
844 return false;
845
846 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
847 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
848 return false;
849
850 return true;
851}
852
853static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
854 int tgt_data_len)
855{
856 struct pn533_target_type_b *tgt_type_b;
857
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100858 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300859
860 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
861 return -EPROTO;
862
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200863 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300864
865 return 0;
866}
867
Michael Thalmeierc952f912016-04-21 16:43:52 +0200868static void pn533_poll_reset_mod_list(struct pn533 *dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100869static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
870 int tgdata_len)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300871{
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300872 struct nfc_target nfc_tgt;
873 int rc;
874
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100875 dev_dbg(dev->dev, "%s: modulation=%d\n",
Joe Perchesb4834832013-04-05 12:27:37 -0700876 __func__, dev->poll_mod_curr);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300877
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100878 if (tg != 1)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300879 return -EPROTO;
880
Samuel Ortiz98b3ac12012-03-05 01:03:39 +0100881 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
882
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300883 switch (dev->poll_mod_curr) {
884 case PN533_POLL_MOD_106KBPS_A:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100885 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300886 break;
887 case PN533_POLL_MOD_212KBPS_FELICA:
888 case PN533_POLL_MOD_424KBPS_FELICA:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100889 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300890 break;
891 case PN533_POLL_MOD_106KBPS_JEWEL:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100892 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300893 break;
894 case PN533_POLL_MOD_847KBPS_B:
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100895 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300896 break;
897 default:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100898 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -0700899 "Unknown current poll modulation\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300900 return -EPROTO;
901 }
902
903 if (rc)
904 return rc;
905
906 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100907 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -0700908 "The Tg found doesn't have the desired protocol\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300909 return -EAGAIN;
910 }
911
Michael Thalmeier9815c7c2016-03-25 15:46:53 +0100912 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -0700913 "Target found - supported protocols: 0x%x\n",
914 nfc_tgt.supported_protocols);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300915
916 dev->tgt_available_prots = nfc_tgt.supported_protocols;
917
Michael Thalmeierc952f912016-04-21 16:43:52 +0200918 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300919 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
920
921 return 0;
922}
923
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200924static inline void pn533_poll_next_mod(struct pn533 *dev)
925{
926 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
927}
928
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300929static void pn533_poll_reset_mod_list(struct pn533 *dev)
930{
931 dev->poll_mod_count = 0;
932}
933
934static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
935{
936 dev->poll_mod_active[dev->poll_mod_count] =
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +0100937 (struct pn533_poll_modulations *)&poll_mod[mod_index];
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300938 dev->poll_mod_count++;
939}
940
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200941static void pn533_poll_create_mod_list(struct pn533 *dev,
942 u32 im_protocols, u32 tm_protocols)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300943{
944 pn533_poll_reset_mod_list(dev);
945
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +0100946 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
947 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
948 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300949 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
950
Waldemar Rymarkiewiczb08e8602012-12-10 14:42:52 +0100951 if (im_protocols & NFC_PROTO_FELICA_MASK ||
952 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300953 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
954 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
955 }
956
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200957 if (im_protocols & NFC_PROTO_JEWEL_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300958 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
959
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200960 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300961 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300962
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200963 if (tm_protocols)
964 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300965}
966
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100967static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300968{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100969 u8 nbtg, tg, *tgdata;
970 int rc, tgdata_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300971
Samuel Ortiz673088f2013-08-21 15:06:55 +0200972 /* Toggle the DEP polling */
Michael Thalmeiere997ebb2016-03-25 15:46:51 +0100973 if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
974 dev->poll_dep = 1;
Samuel Ortiz673088f2013-08-21 15:06:55 +0200975
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100976 nbtg = resp->data[0];
977 tg = resp->data[1];
978 tgdata = &resp->data[2];
979 tgdata_len = resp->len - 2; /* nbtg + tg */
980
981 if (nbtg) {
982 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300983
984 /* We must stop the poll after a valid target found */
Michael Thalmeierc952f912016-04-21 16:43:52 +0200985 if (rc == 0)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200986 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300987 }
988
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +0200989 return -EAGAIN;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -0300990}
991
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100992static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
Samuel Ortizad3823c2012-05-30 23:54:55 +0200993{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +0100994 struct sk_buff *skb;
995 u8 *felica, *nfcid3, *gb;
996
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +0100997 u8 *gbytes = dev->gb;
998 size_t gbytes_len = dev->gb_len;
999
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001000 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1001 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1002 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1003 0xff, 0xff}; /* System code */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001004
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001005 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1006 0x0, 0x0, 0x0,
1007 0x40}; /* SEL_RES for DEP */
Samuel Ortizad3823c2012-05-30 23:54:55 +02001008
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001009 unsigned int skb_len = 36 + /*
1010 * mode (1), mifare (6),
1011 * felica (18), nfcid3 (10), gb_len (1)
1012 */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001013 gbytes_len +
1014 1; /* len Tk*/
Samuel Ortizad3823c2012-05-30 23:54:55 +02001015
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001016 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001017 if (!skb)
1018 return NULL;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001019
1020 /* DEP support only */
Thierry Escande52f2eae2013-02-05 10:08:35 +01001021 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001022
1023 /* MIFARE params */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001024 memcpy(skb_put(skb, 6), mifare_params, 6);
1025
1026 /* Felica params */
1027 felica = skb_put(skb, 18);
1028 memcpy(felica, felica_params, 18);
1029 get_random_bytes(felica + 2, 6);
1030
1031 /* NFCID3 */
1032 nfcid3 = skb_put(skb, 10);
1033 memset(nfcid3, 0, 10);
1034 memcpy(nfcid3, felica, 8);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001035
1036 /* General bytes */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001037 *skb_put(skb, 1) = gbytes_len;
1038
1039 gb = skb_put(skb, gbytes_len);
1040 memcpy(gb, gbytes, gbytes_len);
Samuel Ortiz51d9e802012-05-30 01:48:46 +02001041
Samuel Ortizad3823c2012-05-30 23:54:55 +02001042 /* Len Tk */
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001043 *skb_put(skb, 1) = 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001044
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001045 return skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001046}
1047
Olivier Guiter3c13b242013-09-23 12:24:38 +02001048static void pn533_wq_tm_mi_recv(struct work_struct *work);
1049static struct sk_buff *pn533_build_response(struct pn533 *dev);
1050
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001051static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001052 struct sk_buff *resp)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001053{
Olivier Guiter3c13b242013-09-23 12:24:38 +02001054 struct sk_buff *skb;
1055 u8 status, ret, mi;
1056 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001057
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001058 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001059
Olivier Guiter3c13b242013-09-23 12:24:38 +02001060 if (IS_ERR(resp)) {
1061 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001062 return PTR_ERR(resp);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001063 }
1064
Olivier Guiter3c13b242013-09-23 12:24:38 +02001065 status = resp->data[0];
1066
1067 ret = status & PN533_CMD_RET_MASK;
1068 mi = status & PN533_CMD_MI_MASK;
1069
1070 skb_pull(resp, sizeof(status));
1071
1072 if (ret != PN533_CMD_RET_SUCCESS) {
1073 rc = -EIO;
1074 goto error;
1075 }
1076
1077 skb_queue_tail(&dev->resp_q, resp);
1078
1079 if (mi) {
1080 queue_work(dev->wq, &dev->mi_tm_rx_work);
1081 return -EINPROGRESS;
1082 }
1083
1084 skb = pn533_build_response(dev);
1085 if (!skb) {
1086 rc = -EIO;
1087 goto error;
1088 }
1089
1090 return nfc_tm_data_received(dev->nfc_dev, skb);
1091
1092error:
1093 nfc_tm_deactivated(dev->nfc_dev);
1094 dev->tgt_mode = 0;
1095 skb_queue_purge(&dev->resp_q);
1096 dev_kfree_skb(resp);
1097
1098 return rc;
1099}
1100
1101static void pn533_wq_tm_mi_recv(struct work_struct *work)
1102{
1103 struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1104 struct sk_buff *skb;
1105 int rc;
1106
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001107 dev_dbg(dev->dev, "%s\n", __func__);
Olivier Guiter3c13b242013-09-23 12:24:38 +02001108
1109 skb = pn533_alloc_skb(dev, 0);
1110 if (!skb)
1111 return;
1112
1113 rc = pn533_send_cmd_direct_async(dev,
1114 PN533_CMD_TG_GET_DATA,
1115 skb,
1116 pn533_tm_get_data_complete,
1117 NULL);
1118
1119 if (rc < 0)
1120 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001121}
1122
Olivier Guiter93ad4202013-09-23 12:24:39 +02001123static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1124 struct sk_buff *resp);
1125static void pn533_wq_tm_mi_send(struct work_struct *work)
1126{
1127 struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1128 struct sk_buff *skb;
1129 int rc;
1130
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001131 dev_dbg(dev->dev, "%s\n", __func__);
Olivier Guiter93ad4202013-09-23 12:24:39 +02001132
1133 /* Grab the first skb in the queue */
1134 skb = skb_dequeue(&dev->fragment_skb);
1135 if (skb == NULL) { /* No more data */
1136 /* Reset the queue for future use */
1137 skb_queue_head_init(&dev->fragment_skb);
1138 goto error;
1139 }
1140
1141 /* last entry - remove MI bit */
1142 if (skb_queue_len(&dev->fragment_skb) == 0) {
1143 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1144 skb, pn533_tm_send_complete, NULL);
1145 } else
1146 rc = pn533_send_cmd_direct_async(dev,
1147 PN533_CMD_TG_SET_META_DATA,
1148 skb, pn533_tm_send_complete, NULL);
1149
1150 if (rc == 0) /* success */
1151 return;
1152
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001153 dev_err(dev->dev,
Olivier Guiter93ad4202013-09-23 12:24:39 +02001154 "Error %d when trying to perform set meta data_exchange", rc);
1155
1156 dev_kfree_skb(skb);
1157
1158error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001159 dev->phy_ops->send_ack(dev, GFP_KERNEL);
Olivier Guiter93ad4202013-09-23 12:24:39 +02001160 queue_work(dev->wq, &dev->cmd_work);
1161}
1162
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001163static void pn533_wq_tg_get_data(struct work_struct *work)
1164{
1165 struct pn533 *dev = container_of(work, struct pn533, tg_work);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001166 struct sk_buff *skb;
1167 int rc;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001168
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001169 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001170
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001171 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001172 if (!skb)
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001173 return;
1174
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001175 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1176 pn533_tm_get_data_complete, NULL);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001177
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01001178 if (rc < 0)
1179 dev_kfree_skb(skb);
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001180}
1181
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001182#define ATR_REQ_GB_OFFSET 17
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001183static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
Samuel Ortizad3823c2012-05-30 23:54:55 +02001184{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001185 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001186 size_t gb_len;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001187 int rc;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001188
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001189 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001190
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001191 if (resp->len < ATR_REQ_GB_OFFSET + 1)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001192 return -EINVAL;
1193
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001194 mode = resp->data[0];
1195 cmd = &resp->data[1];
Samuel Ortizad3823c2012-05-30 23:54:55 +02001196
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001197 dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001198 mode, resp->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001199
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001200 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1201 PN533_INIT_TARGET_RESP_ACTIVE)
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001202 comm_mode = NFC_COMM_ACTIVE;
1203
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001204 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001205 return -EOPNOTSUPP;
1206
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001207 gb = cmd + ATR_REQ_GB_OFFSET;
1208 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
Samuel Ortizfc40a8c2012-06-01 13:21:13 +02001209
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001210 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1211 comm_mode, gb, gb_len);
1212 if (rc < 0) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001213 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001214 "Error when signaling target activation\n");
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001215 return rc;
1216 }
1217
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001218 dev->tgt_mode = 1;
Samuel Ortiz103b34c2012-05-31 00:07:51 +02001219 queue_work(dev->wq, &dev->tg_work);
1220
1221 return 0;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001222}
1223
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001224static void pn533_listen_mode_timer(unsigned long data)
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001225{
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001226 struct pn533 *dev = (struct pn533 *)data;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001227
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001228 dev_dbg(dev->dev, "Listen mode timeout\n");
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001229
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001230 dev->cancel_listen = 1;
1231
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001232 pn533_poll_next_mod(dev);
1233
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001234 queue_delayed_work(dev->wq, &dev->poll_work,
1235 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001236}
1237
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001238static int pn533_rf_complete(struct pn533 *dev, void *arg,
1239 struct sk_buff *resp)
1240{
1241 int rc = 0;
1242
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001243 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001244
1245 if (IS_ERR(resp)) {
1246 rc = PTR_ERR(resp);
1247
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001248 nfc_err(dev->dev, "RF setting error %d\n", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001249
1250 return rc;
1251 }
1252
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001253 queue_delayed_work(dev->wq, &dev->poll_work,
1254 msecs_to_jiffies(PN533_POLL_INTERVAL));
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001255
1256 dev_kfree_skb(resp);
1257 return rc;
1258}
1259
1260static void pn533_wq_rf(struct work_struct *work)
1261{
1262 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1263 struct sk_buff *skb;
1264 int rc;
1265
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001266 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001267
1268 skb = pn533_alloc_skb(dev, 2);
1269 if (!skb)
1270 return;
1271
1272 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02001273 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001274
1275 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1276 pn533_rf_complete, NULL);
1277 if (rc < 0) {
1278 dev_kfree_skb(skb);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001279 nfc_err(dev->dev, "RF setting error %d\n", rc);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001280 }
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001281}
1282
Samuel Ortiz673088f2013-08-21 15:06:55 +02001283static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1284 struct sk_buff *resp)
1285{
1286 struct pn533_cmd_jump_dep_response *rsp;
1287 struct nfc_target nfc_target;
1288 u8 target_gt_len;
1289 int rc;
1290
1291 if (IS_ERR(resp))
1292 return PTR_ERR(resp);
1293
1294 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1295
1296 rc = rsp->status & PN533_CMD_RET_MASK;
1297 if (rc != PN533_CMD_RET_SUCCESS) {
1298 /* Not target found, turn radio off */
1299 queue_work(dev->wq, &dev->rf_work);
1300
1301 dev_kfree_skb(resp);
1302 return 0;
1303 }
1304
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001305 dev_dbg(dev->dev, "Creating new target");
Samuel Ortiz673088f2013-08-21 15:06:55 +02001306
1307 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1308 nfc_target.nfcid1_len = 10;
1309 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1310 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1311 if (rc)
1312 goto error;
1313
1314 dev->tgt_available_prots = 0;
1315 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1316
1317 /* ATR_RES general bytes are located at offset 17 */
1318 target_gt_len = resp->len - 17;
1319 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1320 rsp->gt, target_gt_len);
1321 if (!rc) {
1322 rc = nfc_dep_link_is_up(dev->nfc_dev,
1323 dev->nfc_dev->targets[0].idx,
1324 0, NFC_RF_INITIATOR);
1325
1326 if (!rc)
1327 pn533_poll_reset_mod_list(dev);
1328 }
1329error:
1330 dev_kfree_skb(resp);
1331 return rc;
1332}
1333
1334#define PASSIVE_DATA_LEN 5
1335static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1336{
1337 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1338 struct sk_buff *skb;
1339 int rc, skb_len;
1340 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1341 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1342
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001343 dev_dbg(dev->dev, "%s", __func__);
Samuel Ortiz673088f2013-08-21 15:06:55 +02001344
1345 if (!dev->gb) {
1346 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1347
1348 if (!dev->gb || !dev->gb_len) {
1349 dev->poll_dep = 0;
1350 queue_work(dev->wq, &dev->rf_work);
1351 }
1352 }
1353
1354 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1355 skb_len += PASSIVE_DATA_LEN;
1356
1357 /* NFCID3 */
1358 skb_len += NFC_NFCID3_MAXSIZE;
1359 nfcid3[0] = 0x1;
1360 nfcid3[1] = 0xfe;
1361 get_random_bytes(nfcid3 + 2, 6);
1362
1363 skb = pn533_alloc_skb(dev, skb_len);
1364 if (!skb)
1365 return -ENOMEM;
1366
1367 *skb_put(skb, 1) = 0x01; /* Active */
1368 *skb_put(skb, 1) = 0x02; /* 424 kbps */
1369
1370 next = skb_put(skb, 1); /* Next */
1371 *next = 0;
1372
1373 /* Copy passive data */
1374 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1375 *next |= 1;
1376
1377 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1378 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1379 NFC_NFCID3_MAXSIZE);
1380 *next |= 2;
1381
1382 memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
1383 *next |= 4; /* We have some Gi */
1384
1385 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1386 pn533_poll_dep_complete, NULL);
1387
1388 if (rc < 0)
1389 dev_kfree_skb(skb);
1390
1391 return rc;
1392}
1393
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001394static int pn533_poll_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001395 struct sk_buff *resp)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001396{
1397 struct pn533_poll_modulations *cur_mod;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001398 int rc;
1399
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001400 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001401
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001402 if (IS_ERR(resp)) {
1403 rc = PTR_ERR(resp);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001404
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001405 nfc_err(dev->dev, "%s Poll complete error %d\n",
Joe Perches073a6252013-04-05 12:27:38 -07001406 __func__, rc);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001407
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001408 if (rc == -ENOENT) {
1409 if (dev->poll_mod_count != 0)
1410 return rc;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001411 goto stop_poll;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001412 } else if (rc < 0) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001413 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001414 "Error %d when running poll\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001415 goto stop_poll;
1416 }
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001417 }
1418
1419 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1420
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001421 if (cur_mod->len == 0) { /* Target mode */
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001422 del_timer(&dev->listen_timer);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001423 rc = pn533_init_target_complete(dev, resp);
1424 goto done;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001425 }
1426
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001427 /* Initiator mode */
1428 rc = pn533_start_poll_complete(dev, resp);
1429 if (!rc)
1430 goto done;
1431
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001432 if (!dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001433 dev_dbg(dev->dev, "Polling has been stopped\n");
Waldemar Rymarkiewicz95cb9e12013-04-03 08:02:00 +02001434 goto done;
1435 }
1436
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001437 pn533_poll_next_mod(dev);
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001438 /* Not target found, turn radio off */
1439 queue_work(dev->wq, &dev->rf_work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001440
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001441done:
1442 dev_kfree_skb(resp);
1443 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001444
1445stop_poll:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001446 nfc_err(dev->dev, "Polling operation has been stopped\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001447
Samuel Ortizad3823c2012-05-30 23:54:55 +02001448 pn533_poll_reset_mod_list(dev);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001449 dev->poll_protocols = 0;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001450 return rc;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001451}
Samuel Ortizad3823c2012-05-30 23:54:55 +02001452
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001453static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1454 struct pn533_poll_modulations *mod)
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001455{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001456 struct sk_buff *skb;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001457
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001458 skb = pn533_alloc_skb(dev, mod->len);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001459 if (!skb)
1460 return NULL;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001461
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001462 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001463
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001464 return skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001465}
1466
1467static int pn533_send_poll_frame(struct pn533 *dev)
1468{
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001469 struct pn533_poll_modulations *mod;
1470 struct sk_buff *skb;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001471 int rc;
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001472 u8 cmd_code;
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001473
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001474 mod = dev->poll_mod_active[dev->poll_mod_curr];
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001475
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001476 dev_dbg(dev->dev, "%s mod len %d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001477 __func__, mod->len);
Samuel Ortizad3823c2012-05-30 23:54:55 +02001478
Michael Thalmeiere997ebb2016-03-25 15:46:51 +01001479 if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) {
Samuel Ortiz673088f2013-08-21 15:06:55 +02001480 dev->poll_dep = 0;
1481 return pn533_poll_dep(dev->nfc_dev);
1482 }
1483
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001484 if (mod->len == 0) { /* Listen mode */
1485 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001486 skb = pn533_alloc_poll_tg_frame(dev);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001487 } else { /* Polling mode */
1488 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001489 skb = pn533_alloc_poll_in_frame(dev, mod);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001490 }
1491
1492 if (!skb) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001493 nfc_err(dev->dev, "Failed to allocate skb\n");
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001494 return -ENOMEM;
1495 }
1496
1497 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1498 NULL);
1499 if (rc < 0) {
1500 dev_kfree_skb(skb);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001501 nfc_err(dev->dev, "Polling loop error %d\n", rc);
Waldemar Rymarkiewiczb5193e52012-12-10 14:42:48 +01001502 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001503
1504 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001505}
1506
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001507static void pn533_wq_poll(struct work_struct *work)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001508{
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001509 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001510 struct pn533_poll_modulations *cur_mod;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001511 int rc;
1512
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001513 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1514
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001515 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001516 "%s cancel_listen %d modulation len %d\n",
1517 __func__, dev->cancel_listen, cur_mod->len);
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001518
1519 if (dev->cancel_listen == 1) {
1520 dev->cancel_listen = 0;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001521 dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001522 }
1523
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001524 rc = pn533_send_poll_frame(dev);
1525 if (rc)
1526 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001527
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001528 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1529 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001530}
1531
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001532static int pn533_start_poll(struct nfc_dev *nfc_dev,
1533 u32 im_protocols, u32 tm_protocols)
1534{
1535 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02001536 struct pn533_poll_modulations *cur_mod;
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02001537 u8 rand_mod;
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02001538 int rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001539
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001540 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001541 "%s: im protocols 0x%x tm protocols 0x%x\n",
1542 __func__, im_protocols, tm_protocols);
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001543
1544 if (dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001545 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001546 "Cannot poll with a target already activated\n");
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001547 return -EBUSY;
1548 }
1549
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001550 if (dev->tgt_mode) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001551 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001552 "Cannot poll while already being activated\n");
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001553 return -EBUSY;
1554 }
1555
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001556 if (tm_protocols) {
1557 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1558 if (dev->gb == NULL)
1559 tm_protocols = 0;
1560 }
Samuel Ortizad3823c2012-05-30 23:54:55 +02001561
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001562 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1563 dev->poll_protocols = im_protocols;
1564 dev->listen_protocols = tm_protocols;
Samuel Ortizad3823c2012-05-30 23:54:55 +02001565
Samuel Ortizdfccd0f2013-07-03 15:14:17 +02001566 /* Do not always start polling from the same modulation */
1567 get_random_bytes(&rand_mod, sizeof(rand_mod));
1568 rand_mod %= dev->poll_mod_count;
1569 dev->poll_mod_curr = rand_mod;
1570
Samuel Ortizcec4b8e2013-08-21 15:12:06 +02001571 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1572
1573 rc = pn533_send_poll_frame(dev);
1574
1575 /* Start listen timer */
1576 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
1577 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1578
1579 return rc;
Samuel Ortizfe7c5802012-05-15 15:57:06 +02001580}
1581
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001582static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1583{
1584 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1585
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001586 del_timer(&dev->listen_timer);
1587
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001588 if (!dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001589 dev_dbg(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001590 "Polling operation was not running\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001591 return;
1592 }
1593
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001594 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz46f793b2013-07-03 14:50:36 +02001595 flush_delayed_work(&dev->poll_work);
Samuel Ortiz7c2a04a932012-05-21 16:20:01 +02001596 pn533_poll_reset_mod_list(dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001597}
1598
1599static int pn533_activate_target_nfcdep(struct pn533 *dev)
1600{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001601 struct pn533_cmd_activate_response *rsp;
Samuel Ortiz541d9202011-12-14 16:43:10 +01001602 u16 gt_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001603 int rc;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001604 struct sk_buff *skb;
1605 struct sk_buff *resp;
1606
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001607 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001608
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001609 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001610 if (!skb)
1611 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001612
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001613 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1614 *skb_put(skb, sizeof(u8)) = 0; /* Next */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001615
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001616 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1617 if (IS_ERR(resp))
1618 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001619
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001620 rsp = (struct pn533_cmd_activate_response *)resp->data;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001621 rc = rsp->status & PN533_CMD_RET_MASK;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001622 if (rc != PN533_CMD_RET_SUCCESS) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001623 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001624 "Target activation failed (error 0x%x)\n", rc);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001625 dev_kfree_skb(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001626 return -EIO;
Waldemar Rymarkiewicz8a0ecfe2012-12-21 10:39:01 +01001627 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001628
Samuel Ortiz541d9202011-12-14 16:43:10 +01001629 /* ATR_RES general bytes are located at offset 16 */
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001630 gt_len = resp->len - 16;
1631 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001632
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001633 dev_kfree_skb(resp);
Samuel Ortiz541d9202011-12-14 16:43:10 +01001634 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001635}
1636
Eric Lapuyade90099432012-05-07 12:31:13 +02001637static int pn533_activate_target(struct nfc_dev *nfc_dev,
1638 struct nfc_target *target, u32 protocol)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001639{
1640 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1641 int rc;
1642
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001643 dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001644
1645 if (dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001646 nfc_err(dev->dev,
Joe Perchesb4834832013-04-05 12:27:37 -07001647 "Cannot activate while polling\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001648 return -EBUSY;
1649 }
1650
1651 if (dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001652 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001653 "There is already an active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001654 return -EBUSY;
1655 }
1656
1657 if (!dev->tgt_available_prots) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001658 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001659 "There is no available target to activate\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001660 return -EINVAL;
1661 }
1662
1663 if (!(dev->tgt_available_prots & (1 << protocol))) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001664 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001665 "Target doesn't support requested proto %u\n",
1666 protocol);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001667 return -EINVAL;
1668 }
1669
1670 if (protocol == NFC_PROTO_NFC_DEP) {
1671 rc = pn533_activate_target_nfcdep(dev);
1672 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001673 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001674 "Activating target with DEP failed %d\n", rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001675 return rc;
1676 }
1677 }
1678
1679 dev->tgt_active_prot = protocol;
1680 dev->tgt_available_prots = 0;
1681
1682 return 0;
1683}
1684
Michael Thalmeier37f895d2016-03-25 15:46:52 +01001685static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1686 struct sk_buff *resp)
1687{
1688 int rc = 0;
1689
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001690 dev_dbg(dev->dev, "%s\n", __func__);
Michael Thalmeier37f895d2016-03-25 15:46:52 +01001691
1692 if (IS_ERR(resp)) {
1693 rc = PTR_ERR(resp);
1694
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001695 nfc_err(dev->dev, "Target release error %d\n", rc);
Michael Thalmeier37f895d2016-03-25 15:46:52 +01001696
1697 return rc;
1698 }
1699
1700 rc = resp->data[0] & PN533_CMD_RET_MASK;
1701 if (rc != PN533_CMD_RET_SUCCESS)
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001702 nfc_err(dev->dev,
Michael Thalmeier37f895d2016-03-25 15:46:52 +01001703 "Error 0x%x when releasing the target\n", rc);
1704
1705 dev_kfree_skb(resp);
1706 return rc;
1707}
1708
Eric Lapuyade90099432012-05-07 12:31:13 +02001709static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
Christophe Ricard96d45812015-10-25 22:54:43 +01001710 struct nfc_target *target, u8 mode)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001711{
1712 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001713 struct sk_buff *skb;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001714 int rc;
1715
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001716 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001717
1718 if (!dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001719 nfc_err(dev->dev, "There is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001720 return;
1721 }
1722
1723 dev->tgt_active_prot = 0;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001724 skb_queue_purge(&dev->resp_q);
1725
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001726 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001727 if (!skb)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001728 return;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001729
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01001730 *skb_put(skb, 1) = 1; /* TG*/
1731
Michael Thalmeier37f895d2016-03-25 15:46:52 +01001732 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
1733 pn533_deactivate_target_complete, NULL);
1734 if (rc < 0) {
1735 dev_kfree_skb(skb);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001736 nfc_err(dev->dev, "Target release error %d\n", rc);
Michael Thalmeier37f895d2016-03-25 15:46:52 +01001737 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001738}
1739
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001740
1741static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001742 struct sk_buff *resp)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001743{
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001744 struct pn533_cmd_jump_dep_response *rsp;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001745 u8 target_gt_len;
1746 int rc;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001747 u8 active = *(u8 *)arg;
Waldemar Rymarkiewicz70418e62012-10-11 14:04:00 +02001748
1749 kfree(arg);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001750
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001751 if (IS_ERR(resp))
1752 return PTR_ERR(resp);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001753
1754 if (dev->tgt_available_prots &&
1755 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001756 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001757 "The target does not support DEP\n");
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001758 rc = -EINVAL;
1759 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001760 }
1761
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001762 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1763
1764 rc = rsp->status & PN533_CMD_RET_MASK;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001765 if (rc != PN533_CMD_RET_SUCCESS) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001766 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001767 "Bringing DEP link up failed (error 0x%x)\n", rc);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001768 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001769 }
1770
1771 if (!dev->tgt_available_prots) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001772 struct nfc_target nfc_target;
1773
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001774 dev_dbg(dev->dev, "Creating new target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001775
1776 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Samuel Ortiz2fbabfa2012-03-05 01:03:47 +01001777 nfc_target.nfcid1_len = 10;
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001778 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001779 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1780 if (rc)
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001781 goto error;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001782
1783 dev->tgt_available_prots = 0;
1784 }
1785
1786 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1787
1788 /* ATR_RES general bytes are located at offset 17 */
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001789 target_gt_len = resp->len - 17;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001790 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001791 rsp->gt, target_gt_len);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001792 if (rc == 0)
1793 rc = nfc_dep_link_is_up(dev->nfc_dev,
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001794 dev->nfc_dev->targets[0].idx,
1795 !active, NFC_RF_INITIATOR);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001796
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001797error:
1798 dev_kfree_skb(resp);
1799 return rc;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001800}
1801
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001802static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
Eric Lapuyade90099432012-05-07 12:31:13 +02001803static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
Waldemar Rymarkiewicz37cf4fc2012-12-10 14:42:50 +01001804 u8 comm_mode, u8 *gb, size_t gb_len)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001805{
1806 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001807 struct sk_buff *skb;
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001808 int rc, skb_len;
1809 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
Samuel Ortizd7f33452012-05-29 21:45:21 +02001810 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001811
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001812 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001813
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001814 if (dev->poll_mod_count) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001815 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001816 "Cannot bring the DEP link up while polling\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001817 return -EBUSY;
1818 }
1819
1820 if (dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001821 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001822 "There is already an active target\n");
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001823 return -EBUSY;
1824 }
1825
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001826 skb_len = 3 + gb_len; /* ActPass + BR + Next */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001827 skb_len += PASSIVE_DATA_LEN;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001828
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001829 /* NFCID3 */
1830 skb_len += NFC_NFCID3_MAXSIZE;
1831 if (target && !target->nfcid2_len) {
1832 nfcid3[0] = 0x1;
1833 nfcid3[1] = 0xfe;
1834 get_random_bytes(nfcid3 + 2, 6);
1835 }
Samuel Ortiz322bce92013-05-27 15:29:11 +02001836
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01001837 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001838 if (!skb)
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001839 return -ENOMEM;
1840
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001841 *skb_put(skb, 1) = !comm_mode; /* ActPass */
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001842 *skb_put(skb, 1) = 0x02; /* 424 kbps */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001843
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001844 next = skb_put(skb, 1); /* Next */
1845 *next = 0;
1846
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001847 /* Copy passive data */
1848 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1849 *next |= 1;
Samuel Ortizd7f33452012-05-29 21:45:21 +02001850
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001851 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1852 if (target && target->nfcid2_len)
Samuel Ortiz322bce92013-05-27 15:29:11 +02001853 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
1854 target->nfcid2_len);
Samuel Ortiz5eef4842013-07-01 10:58:12 +02001855 else
1856 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1857 NFC_NFCID3_MAXSIZE);
1858 *next |= 2;
Samuel Ortiz322bce92013-05-27 15:29:11 +02001859
Samuel Ortiz47807d32012-03-05 01:03:50 +01001860 if (gb != NULL && gb_len > 0) {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001861 memcpy(skb_put(skb, gb_len), gb, gb_len);
1862 *next |= 4; /* We have some Gi */
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001863 } else {
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001864 *next = 0;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001865 }
1866
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001867 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1868 if (!arg) {
1869 dev_kfree_skb(skb);
1870 return -ENOMEM;
1871 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001872
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001873 *arg = !comm_mode;
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001874
Samuel Ortiz17e9d9d2013-06-26 18:16:21 +02001875 pn533_rf_field(dev->nfc_dev, 0);
1876
Waldemar Rymarkiewicz13003642012-12-10 14:42:45 +01001877 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1878 pn533_in_dep_link_up_complete, arg);
1879
1880 if (rc < 0) {
1881 dev_kfree_skb(skb);
1882 kfree(arg);
1883 }
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001884
1885 return rc;
1886}
1887
1888static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1889{
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001890 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1891
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001892 dev_dbg(dev->dev, "%s\n", __func__);
Waldemar Rymarkiewiczfcfafc72012-12-10 14:42:55 +01001893
Samuel Ortiz6fbbdc12012-05-30 17:20:25 +02001894 pn533_poll_reset_mod_list(dev);
1895
Waldemar Rymarkiewicz10cff292013-04-03 08:02:14 +02001896 if (dev->tgt_mode || dev->tgt_active_prot)
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001897 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
Samuel Ortiz51ad3042012-05-31 20:01:32 +02001898
1899 dev->tgt_active_prot = 0;
1900 dev->tgt_mode = 0;
1901
1902 skb_queue_purge(&dev->resp_q);
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01001903
1904 return 0;
1905}
1906
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001907struct pn533_data_exchange_arg {
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001908 data_exchange_cb_t cb;
1909 void *cb_context;
1910};
1911
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001912static struct sk_buff *pn533_build_response(struct pn533 *dev)
1913{
1914 struct sk_buff *skb, *tmp, *t;
1915 unsigned int skb_len = 0, tmp_len = 0;
1916
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001917 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001918
1919 if (skb_queue_empty(&dev->resp_q))
1920 return NULL;
1921
1922 if (skb_queue_len(&dev->resp_q) == 1) {
1923 skb = skb_dequeue(&dev->resp_q);
1924 goto out;
1925 }
1926
1927 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1928 skb_len += tmp->len;
1929
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001930 dev_dbg(dev->dev, "%s total length %d\n",
Joe Perchesb4834832013-04-05 12:27:37 -07001931 __func__, skb_len);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001932
1933 skb = alloc_skb(skb_len, GFP_KERNEL);
1934 if (skb == NULL)
1935 goto out;
1936
1937 skb_put(skb, skb_len);
1938
1939 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1940 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1941 tmp_len += tmp->len;
1942 }
1943
1944out:
1945 skb_queue_purge(&dev->resp_q);
1946
1947 return skb;
1948}
1949
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001950static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001951 struct sk_buff *resp)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001952{
1953 struct pn533_data_exchange_arg *arg = _arg;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001954 struct sk_buff *skb;
1955 int rc = 0;
1956 u8 status, ret, mi;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001957
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001958 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001959
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001960 if (IS_ERR(resp)) {
1961 rc = PTR_ERR(resp);
1962 goto _error;
1963 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001964
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001965 status = resp->data[0];
1966 ret = status & PN533_CMD_RET_MASK;
1967 mi = status & PN533_CMD_MI_MASK;
1968
1969 skb_pull(resp, sizeof(status));
1970
1971 if (ret != PN533_CMD_RET_SUCCESS) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01001972 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07001973 "Exchanging data failed (error 0x%x)\n", ret);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001974 rc = -EIO;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001975 goto error;
1976 }
1977
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001978 skb_queue_tail(&dev->resp_q, resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03001979
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01001980 if (mi) {
1981 dev->cmd_complete_mi_arg = arg;
Olivier Guiter963a82e2013-06-13 13:43:28 +00001982 queue_work(dev->wq, &dev->mi_rx_work);
1983 return -EINPROGRESS;
1984 }
1985
1986 /* Prepare for the next round */
1987 if (skb_queue_len(&dev->fragment_skb) > 0) {
1988 dev->cmd_complete_dep_arg = arg;
1989 queue_work(dev->wq, &dev->mi_tx_work);
1990
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001991 return -EINPROGRESS;
1992 }
1993
1994 skb = pn533_build_response(dev);
Julia Lawall5df848f2015-04-05 14:06:36 +02001995 if (!skb) {
1996 rc = -ENOMEM;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001997 goto error;
Julia Lawall5df848f2015-04-05 14:06:36 +02001998 }
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02001999
2000 arg->cb(arg->cb_context, skb, 0);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002001 kfree(arg);
2002 return 0;
2003
2004error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002005 dev_kfree_skb(resp);
2006_error:
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002007 skb_queue_purge(&dev->resp_q);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002008 arg->cb(arg->cb_context, NULL, rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002009 kfree(arg);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002010 return rc;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002011}
2012
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002013/*
2014 * Receive an incoming pn533 frame. skb contains only header and payload.
2015 * If skb == NULL, it is a notification that the link below is dead.
2016 */
2017void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
2018{
Michael Thalmeierb31d5102016-04-21 16:43:53 +02002019 if (!dev->cmd)
2020 goto sched_wq;
2021
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002022 dev->cmd->status = status;
2023
Michael Thalmeierb31d5102016-04-21 16:43:53 +02002024 if (status != 0) {
2025 dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
2026 goto sched_wq;
2027 }
2028
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002029 if (skb == NULL) {
2030 pr_err("NULL Frame -> link is dead\n");
2031 goto sched_wq;
2032 }
2033
2034 if (pn533_rx_frame_is_ack(skb->data)) {
2035 dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2036 dev_kfree_skb(skb);
2037 return;
2038 }
2039
2040 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2041 dev->ops->rx_frame_size(skb->data), false);
2042
2043 if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2044 nfc_err(dev->dev, "Received an invalid frame\n");
2045 dev->cmd->status = -EIO;
2046 } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2047 nfc_err(dev->dev, "It it not the response to the last command\n");
2048 dev->cmd->status = -EIO;
2049 }
2050
2051 dev->cmd->resp = skb;
2052
2053sched_wq:
2054 queue_work(dev->wq, &dev->cmd_complete_work);
2055}
2056EXPORT_SYMBOL(pn533_recv_frame);
2057
Olivier Guiter963a82e2013-06-13 13:43:28 +00002058/* Split the Tx skb into small chunks */
2059static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2060{
2061 struct sk_buff *frag;
2062 int frag_size;
2063
2064 do {
2065 /* Remaining size */
2066 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2067 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2068 else
2069 frag_size = skb->len;
2070
2071 /* Allocate and reserve */
2072 frag = pn533_alloc_skb(dev, frag_size);
2073 if (!frag) {
2074 skb_queue_purge(&dev->fragment_skb);
2075 break;
2076 }
2077
Olivier Guiter22953f92013-09-23 12:24:37 +02002078 if (!dev->tgt_mode) {
2079 /* Reserve the TG/MI byte */
2080 skb_reserve(frag, 1);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002081
Olivier Guiter22953f92013-09-23 12:24:37 +02002082 /* MI + TG */
2083 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2084 *skb_push(frag, sizeof(u8)) =
2085 (PN533_CMD_MI_MASK | 1);
2086 else
2087 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2088 }
Olivier Guiter963a82e2013-06-13 13:43:28 +00002089
2090 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2091
2092 /* Reduce the size of incoming buffer */
2093 skb_pull(skb, frag_size);
2094
2095 /* Add this to skb_queue */
2096 skb_queue_tail(&dev->fragment_skb, frag);
2097
2098 } while (skb->len > 0);
2099
2100 dev_kfree_skb(skb);
2101
2102 return skb_queue_len(&dev->fragment_skb);
2103}
2104
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002105static int pn533_transceive(struct nfc_dev *nfc_dev,
2106 struct nfc_target *target, struct sk_buff *skb,
2107 data_exchange_cb_t cb, void *cb_context)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002108{
2109 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002110 struct pn533_data_exchange_arg *arg = NULL;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002111 int rc;
2112
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002113 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002114
2115 if (!dev->tgt_active_prot) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002116 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002117 "Can't exchange data if there is no active target\n");
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002118 rc = -EINVAL;
2119 goto error;
2120 }
2121
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002122 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002123 if (!arg) {
2124 rc = -ENOMEM;
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002125 goto error;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002126 }
2127
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002128 arg->cb = cb;
2129 arg->cb_context = cb_context;
2130
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002131 switch (dev->device_type) {
2132 case PN533_DEVICE_PASORI:
2133 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2134 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2135 skb,
2136 pn533_data_exchange_complete,
2137 arg);
2138
2139 break;
2140 }
2141 default:
Olivier Guiter963a82e2013-06-13 13:43:28 +00002142 /* jumbo frame ? */
2143 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2144 rc = pn533_fill_fragment_skbs(dev, skb);
2145 if (rc <= 0)
2146 goto error;
2147
2148 skb = skb_dequeue(&dev->fragment_skb);
2149 if (!skb) {
2150 rc = -EIO;
2151 goto error;
2152 }
2153 } else {
2154 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2155 }
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002156
2157 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2158 skb, pn533_data_exchange_complete,
2159 arg);
2160
2161 break;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002162 }
2163
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002164 if (rc < 0) /* rc from send_async */
2165 goto error;
2166
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002167 return 0;
2168
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002169error:
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002170 kfree(arg);
2171 dev_kfree_skb(skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002172 return rc;
2173}
2174
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002175static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002176 struct sk_buff *resp)
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002177{
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002178 u8 status;
Thierry Escande5b412fd2012-11-15 18:24:28 +01002179
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002180 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002181
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002182 if (IS_ERR(resp))
2183 return PTR_ERR(resp);
Thierry Escande5b412fd2012-11-15 18:24:28 +01002184
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002185 status = resp->data[0];
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002186
Olivier Guiter93ad4202013-09-23 12:24:39 +02002187 /* Prepare for the next round */
2188 if (skb_queue_len(&dev->fragment_skb) > 0) {
2189 queue_work(dev->wq, &dev->mi_tm_tx_work);
2190 return -EINPROGRESS;
2191 }
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002192 dev_kfree_skb(resp);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002193
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002194 if (status != 0) {
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002195 nfc_tm_deactivated(dev->nfc_dev);
2196
Samuel Ortiz51ad3042012-05-31 20:01:32 +02002197 dev->tgt_mode = 0;
2198
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002199 return 0;
2200 }
2201
2202 queue_work(dev->wq, &dev->tg_work);
2203
2204 return 0;
2205}
2206
2207static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2208{
2209 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002210 int rc;
2211
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002212 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002213
Olivier Guiter93ad4202013-09-23 12:24:39 +02002214 /* let's split in multiple chunks if size's too big */
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002215 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
Olivier Guiter93ad4202013-09-23 12:24:39 +02002216 rc = pn533_fill_fragment_skbs(dev, skb);
2217 if (rc <= 0)
2218 goto error;
2219
2220 /* get the first skb */
2221 skb = skb_dequeue(&dev->fragment_skb);
2222 if (!skb) {
2223 rc = -EIO;
2224 goto error;
2225 }
2226
2227 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2228 pn533_tm_send_complete, NULL);
2229 } else {
2230 /* Send th skb */
2231 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2232 pn533_tm_send_complete, NULL);
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002233 }
2234
Olivier Guiter93ad4202013-09-23 12:24:39 +02002235error:
2236 if (rc < 0) {
Waldemar Rymarkiewicze4878822012-12-10 14:42:44 +01002237 dev_kfree_skb(skb);
Olivier Guiter93ad4202013-09-23 12:24:39 +02002238 skb_queue_purge(&dev->fragment_skb);
2239 }
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002240
2241 return rc;
2242}
2243
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002244static void pn533_wq_mi_recv(struct work_struct *work)
2245{
Olivier Guiter963a82e2013-06-13 13:43:28 +00002246 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002247 struct sk_buff *skb;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002248 int rc;
2249
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002250 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002251
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002252 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002253 if (!skb)
2254 goto error;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002255
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002256 switch (dev->device_type) {
2257 case PN533_DEVICE_PASORI:
2258 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2259 rc = pn533_send_cmd_direct_async(dev,
2260 PN533_CMD_IN_COMM_THRU,
2261 skb,
2262 pn533_data_exchange_complete,
2263 dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002264
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002265 break;
2266 }
2267 default:
2268 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002269
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002270 rc = pn533_send_cmd_direct_async(dev,
2271 PN533_CMD_IN_DATA_EXCHANGE,
2272 skb,
2273 pn533_data_exchange_complete,
2274 dev->cmd_complete_mi_arg);
Waldemar Rymarkiewiczb1bb2902012-11-26 14:18:32 +01002275
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002276 break;
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002277 }
2278
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002279 if (rc == 0) /* success */
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002280 return;
2281
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002282 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002283 "Error %d when trying to perform data_exchange\n", rc);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002284
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002285 dev_kfree_skb(skb);
Waldemar Rymarkiewicz140ef7f2013-04-03 08:02:07 +02002286 kfree(dev->cmd_complete_mi_arg);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002287
Waldemar Rymarkiewiczb1e666f2012-12-10 14:42:46 +01002288error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002289 dev->phy_ops->send_ack(dev, GFP_KERNEL);
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002290 queue_work(dev->wq, &dev->cmd_work);
Samuel Ortiz6ff73fd2012-04-10 19:43:18 +02002291}
2292
Olivier Guiter963a82e2013-06-13 13:43:28 +00002293static void pn533_wq_mi_send(struct work_struct *work)
2294{
2295 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2296 struct sk_buff *skb;
2297 int rc;
2298
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002299 dev_dbg(dev->dev, "%s\n", __func__);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002300
2301 /* Grab the first skb in the queue */
2302 skb = skb_dequeue(&dev->fragment_skb);
2303
2304 if (skb == NULL) { /* No more data */
2305 /* Reset the queue for future use */
2306 skb_queue_head_init(&dev->fragment_skb);
2307 goto error;
2308 }
2309
2310 switch (dev->device_type) {
2311 case PN533_DEVICE_PASORI:
2312 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2313 rc = -EIO;
2314 break;
2315 }
2316
2317 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2318 skb,
2319 pn533_data_exchange_complete,
2320 dev->cmd_complete_dep_arg);
2321
2322 break;
2323
2324 default:
2325 /* Still some fragments? */
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002326 rc = pn533_send_cmd_direct_async(dev,
2327 PN533_CMD_IN_DATA_EXCHANGE,
Olivier Guiter963a82e2013-06-13 13:43:28 +00002328 skb,
2329 pn533_data_exchange_complete,
2330 dev->cmd_complete_dep_arg);
2331
2332 break;
2333 }
2334
2335 if (rc == 0) /* success */
2336 return;
2337
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002338 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002339 "Error %d when trying to perform data_exchange\n", rc);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002340
2341 dev_kfree_skb(skb);
2342 kfree(dev->cmd_complete_dep_arg);
2343
2344error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002345 dev->phy_ops->send_ack(dev, GFP_KERNEL);
Olivier Guiter963a82e2013-06-13 13:43:28 +00002346 queue_work(dev->wq, &dev->cmd_work);
2347}
2348
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002349static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2350 u8 cfgdata_len)
2351{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002352 struct sk_buff *skb;
2353 struct sk_buff *resp;
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002354 int skb_len;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002355
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002356 dev_dbg(dev->dev, "%s\n", __func__);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002357
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002358 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002359
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002360 skb = pn533_alloc_skb(dev, skb_len);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002361 if (!skb)
2362 return -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002363
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002364 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2365 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002366
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002367 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2368 if (IS_ERR(resp))
2369 return PTR_ERR(resp);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002370
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002371 dev_kfree_skb(resp);
2372 return 0;
2373}
2374
2375static int pn533_get_firmware_version(struct pn533 *dev,
2376 struct pn533_fw_version *fv)
2377{
2378 struct sk_buff *skb;
2379 struct sk_buff *resp;
2380
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002381 skb = pn533_alloc_skb(dev, 0);
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002382 if (!skb)
2383 return -ENOMEM;
2384
2385 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2386 if (IS_ERR(resp))
2387 return PTR_ERR(resp);
2388
2389 fv->ic = resp->data[0];
2390 fv->ver = resp->data[1];
2391 fv->rev = resp->data[2];
2392 fv->support = resp->data[3];
2393
2394 dev_kfree_skb(resp);
2395 return 0;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002396}
2397
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002398static int pn533_pasori_fw_reset(struct pn533 *dev)
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002399{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002400 struct sk_buff *skb;
2401 struct sk_buff *resp;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002402
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002403 dev_dbg(dev->dev, "%s\n", __func__);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002404
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002405 skb = pn533_alloc_skb(dev, sizeof(u8));
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002406 if (!skb)
2407 return -ENOMEM;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002408
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002409 *skb_put(skb, sizeof(u8)) = 0x1;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002410
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002411 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2412 if (IS_ERR(resp))
2413 return PTR_ERR(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002414
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002415 dev_kfree_skb(resp);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002416
Waldemar Rymarkiewicz94c5c152012-11-26 14:18:36 +01002417 return 0;
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002418}
2419
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002420static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2421{
2422 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2423 u8 rf_field = !!rf;
2424 int rc;
2425
Samuel Ortiz3a8eab32013-07-01 17:26:58 +02002426 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2427
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002428 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2429 (u8 *)&rf_field, 1);
2430 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002431 nfc_err(dev->dev, "Error on setting RF field\n");
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002432 return rc;
2433 }
2434
2435 return rc;
2436}
2437
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002438static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2439{
2440 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2441 struct sk_buff *skb;
2442 struct sk_buff *resp;
2443
2444 skb = pn533_alloc_skb(dev, 1);
2445 if (!skb)
2446 return -ENOMEM;
2447
2448 *skb_put(skb, 1) = 0x01;
2449
2450 resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2451 if (IS_ERR(resp))
2452 return PTR_ERR(resp);
2453
2454 dev_kfree_skb(resp);
2455 return 0;
2456}
2457
Sachin Kamate44666b2013-09-20 14:30:41 +05302458static int pn533_dev_up(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002459{
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002460 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2461
2462 if (dev->device_type == PN533_DEVICE_PN532) {
2463 int rc = pn532_sam_configuration(nfc_dev);
2464
2465 if (rc)
2466 return rc;
2467 }
2468
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002469 return pn533_rf_field(nfc_dev, 1);
2470}
2471
Sachin Kamate44666b2013-09-20 14:30:41 +05302472static int pn533_dev_down(struct nfc_dev *nfc_dev)
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002473{
2474 return pn533_rf_field(nfc_dev, 0);
2475}
2476
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002477static struct nfc_ops pn533_nfc_ops = {
Samuel Ortiz60d9edd52013-04-11 11:45:41 +02002478 .dev_up = pn533_dev_up,
2479 .dev_down = pn533_dev_down,
Samuel Ortiz361f3cb2011-12-14 16:43:11 +01002480 .dep_link_up = pn533_dep_link_up,
2481 .dep_link_down = pn533_dep_link_down,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002482 .start_poll = pn533_start_poll,
2483 .stop_poll = pn533_stop_poll,
2484 .activate_target = pn533_activate_target,
2485 .deactivate_target = pn533_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02002486 .im_transceive = pn533_transceive,
Samuel Ortizdadb06f2012-05-31 00:09:11 +02002487 .tm_send = pn533_tm_send,
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002488};
2489
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002490static int pn533_setup(struct pn533 *dev)
2491{
2492 struct pn533_config_max_retries max_retries;
2493 struct pn533_config_timing timing;
2494 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2495 int rc;
2496
2497 switch (dev->device_type) {
2498 case PN533_DEVICE_STD:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002499 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewicz53cf4832013-04-03 08:02:13 +02002500 case PN533_DEVICE_ACR122U:
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002501 case PN533_DEVICE_PN532:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002502 max_retries.mx_rty_atr = 0x2;
2503 max_retries.mx_rty_psl = 0x1;
2504 max_retries.mx_rty_passive_act =
2505 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2506
2507 timing.rfu = PN533_CONFIG_TIMING_102;
2508 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2509 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2510
2511 break;
2512
2513 default:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002514 nfc_err(dev->dev, "Unknown device type %d\n",
Joe Perches073a6252013-04-05 12:27:38 -07002515 dev->device_type);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002516 return -EINVAL;
2517 }
2518
2519 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2520 (u8 *)&max_retries, sizeof(max_retries));
2521 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002522 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002523 "Error on setting MAX_RETRIES config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002524 return rc;
2525 }
2526
2527
2528 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2529 (u8 *)&timing, sizeof(timing));
2530 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002531 nfc_err(dev->dev, "Error on setting RF timings\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002532 return rc;
2533 }
2534
2535 switch (dev->device_type) {
2536 case PN533_DEVICE_STD:
Michael Thalmeierdd7bedc2016-03-25 15:46:54 +01002537 case PN533_DEVICE_PN532:
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002538 break;
2539
2540 case PN533_DEVICE_PASORI:
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002541 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002542
2543 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2544 pasori_cfg, 3);
2545 if (rc) {
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002546 nfc_err(dev->dev,
Joe Perches073a6252013-04-05 12:27:38 -07002547 "Error while settings PASORI config\n");
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002548 return rc;
2549 }
2550
Waldemar Rymarkiewiczf75c2912013-04-03 08:02:02 +02002551 pn533_pasori_fw_reset(dev);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002552
2553 break;
2554 }
2555
2556 return 0;
2557}
2558
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002559struct pn533 *pn533_register_device(u32 device_type,
2560 u32 protocols,
2561 enum pn533_protocol_type protocol_type,
2562 void *phy,
2563 struct pn533_phy_ops *phy_ops,
2564 struct pn533_frame_ops *fops,
Michael Thalmeierb16931b2016-04-21 16:43:50 +02002565 struct device *dev,
2566 struct device *parent)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002567{
Waldemar Rymarkiewiczcb950d92012-11-26 14:18:38 +01002568 struct pn533_fw_version fw_ver;
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002569 struct pn533 *priv;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002570 int rc = -ENOMEM;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002571
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002572 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2573 if (!priv)
2574 return ERR_PTR(-ENOMEM);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002575
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002576 priv->phy = phy;
2577 priv->phy_ops = phy_ops;
2578 priv->dev = dev;
2579 if (fops != NULL)
2580 priv->ops = fops;
2581 else
2582 priv->ops = &pn533_std_frame_ops;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002583
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002584 priv->protocol_type = protocol_type;
2585 priv->device_type = device_type;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002586
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002587 mutex_init(&priv->cmd_lock);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002588
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002589 INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2590 INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2591 INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2592 INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2593 INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2594 INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2595 INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2596 INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2597 INIT_WORK(&priv->rf_work, pn533_wq_rf);
2598 priv->wq = alloc_ordered_workqueue("pn533", 0);
2599 if (priv->wq == NULL)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002600 goto error;
2601
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002602 init_timer(&priv->listen_timer);
2603 priv->listen_timer.data = (unsigned long) priv;
2604 priv->listen_timer.function = pn533_listen_mode_timer;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002605
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002606 skb_queue_head_init(&priv->resp_q);
2607 skb_queue_head_init(&priv->fragment_skb);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002608
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002609 INIT_LIST_HEAD(&priv->cmd_queue);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002610
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002611 memset(&fw_ver, 0, sizeof(fw_ver));
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002612 rc = pn533_get_firmware_version(priv, &fw_ver);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002613 if (rc < 0)
2614 goto destroy_wq;
2615
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002616 nfc_info(dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
Joe Perches073a6252013-04-05 12:27:38 -07002617 fw_ver.ic, fw_ver.ver, fw_ver.rev);
Waldemar Rymarkiewicz9e2d4932012-12-10 14:43:01 +01002618
2619
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002620 priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2621 priv->ops->tx_header_len +
Samuel Ortize8753042011-08-19 15:47:11 +02002622 PN533_CMD_DATAEXCH_HEAD_LEN,
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002623 priv->ops->tx_tail_len);
2624 if (!priv->nfc_dev) {
Wei Yongjun4674d0f2013-05-07 20:07:52 +08002625 rc = -ENOMEM;
Samuel Ortiz4849f852012-04-10 19:43:17 +02002626 goto destroy_wq;
Wei Yongjun4674d0f2013-05-07 20:07:52 +08002627 }
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002628
Michael Thalmeierb16931b2016-04-21 16:43:50 +02002629 nfc_set_parent_dev(priv->nfc_dev, parent);
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002630 nfc_set_drvdata(priv->nfc_dev, priv);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002631
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002632 rc = nfc_register_device(priv->nfc_dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002633 if (rc)
2634 goto free_nfc_dev;
2635
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002636 rc = pn533_setup(priv);
Samuel Ortiz5c7b0532012-07-02 20:04:01 +02002637 if (rc)
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002638 goto unregister_nfc_dev;
Samuel Ortiz34a85bf2012-05-29 21:34:08 +02002639
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002640 return priv;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002641
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002642unregister_nfc_dev:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002643 nfc_unregister_device(priv->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002644
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002645free_nfc_dev:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002646 nfc_free_device(priv->nfc_dev);
Samuel Ortiz9f2f8ba2012-05-29 21:28:58 +02002647
Samuel Ortiz4849f852012-04-10 19:43:17 +02002648destroy_wq:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002649 destroy_workqueue(priv->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002650error:
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002651 kfree(priv);
2652 return ERR_PTR(rc);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002653}
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002654EXPORT_SYMBOL_GPL(pn533_register_device);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002655
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002656void pn533_unregister_device(struct pn533 *priv)
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002657{
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002658 struct pn533_cmd *cmd, *n;
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002659
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002660 nfc_unregister_device(priv->nfc_dev);
2661 nfc_free_device(priv->nfc_dev);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002662
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002663 flush_delayed_work(&priv->poll_work);
2664 destroy_workqueue(priv->wq);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002665
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002666 skb_queue_purge(&priv->resp_q);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002667
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002668 del_timer(&priv->listen_timer);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002669
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002670 list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
Samuel Ortiz5d50b362012-08-17 23:47:54 +02002671 list_del(&cmd->queue);
2672 kfree(cmd);
2673 }
2674
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002675 kfree(priv);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002676}
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002677EXPORT_SYMBOL_GPL(pn533_unregister_device);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002678
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002679
Waldemar Rymarkiewicze70b96e2013-04-03 08:02:01 +02002680MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2681MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2682MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
Michael Thalmeier9815c7c2016-03-25 15:46:53 +01002683MODULE_DESCRIPTION("PN533 driver ver " VERSION);
Aloisio Almeida Jrc46ee382011-07-01 19:31:37 -03002684MODULE_VERSION(VERSION);
2685MODULE_LICENSE("GPL");