networking: use skb_put_zero()
[linux-2.6-block.git] / drivers / nfc / pn533 / pn533.c
CommitLineData
c46ee386 1/*
9815c7cf
MT
2 * Driver for NXP PN533 NFC Chip - core functions
3 *
c46ee386 4 * Copyright (C) 2011 Instituto Nokia de Tecnologia
e70b96e9 5 * Copyright (C) 2012-2013 Tieto Poland
c46ee386
AAJ
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
98b32dec 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c46ee386
AAJ
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
c46ee386
AAJ
25#include <linux/nfc.h>
26#include <linux/netdevice.h>
55eb94f9 27#include <net/nfc/nfc.h>
9815c7cf 28#include "pn533.h"
c46ee386 29
9815c7cf 30#define VERSION "0.3"
c46ee386 31
6fbbdc16
SO
32/* How much time we spend listening for initiators */
33#define PN533_LISTEN_TIME 2
46f793b0
SO
34/* Delay between each poll frame (ms) */
35#define PN533_POLL_INTERVAL 10
6fbbdc16 36
c46ee386
AAJ
37/* 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 */
60d9edd5
SO
48#define PN533_CFGITEM_RF_FIELD 0x01
49#define PN533_CFGITEM_TIMING 0x02
c46ee386 50#define PN533_CFGITEM_MAX_RETRIES 0x05
60d9edd5
SO
51#define PN533_CFGITEM_PASORI 0x82
52
3a8eab39
SO
53#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
54#define PN533_CFGITEM_RF_FIELD_ON 0x1
55#define PN533_CFGITEM_RF_FIELD_OFF 0x0
c46ee386 56
34a85bfc
SO
57#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
c46ee386
AAJ
62#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
34a85bfc
SO
71struct pn533_config_timing {
72 u8 rfu;
73 u8 atr_res_timeout;
74 u8 dep_timeout;
75} __packed;
76
c46ee386
AAJ
77/* 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
c46ee386
AAJ
106struct 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
ef3d56e1 115static const struct pn533_poll_modulations poll_mod[] = {
c46ee386
AAJ
116 [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,
a94e10f7 130 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
31c44464 131 .tsn = 0x03,
c46ee386
AAJ
132 },
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,
a94e10f7 143 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
31c44464 144 .tsn = 0x03,
c46ee386
AAJ
145 },
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 },
6fbbdc16
SO
168 [PN533_LISTEN_MOD] = {
169 .len = 0,
170 },
c46ee386
AAJ
171};
172
173/* PN533_CMD_IN_ATR */
174
c46ee386
AAJ
175struct 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
361f3cb7
SO
187struct 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;
c46ee386 199
ad3823ce
SO
200
201/* PN533_TG_INIT_AS_TARGET */
202#define PN533_INIT_TARGET_PASSIVE 0x1
203#define PN533_INIT_TARGET_DEP 0x2
204
fc40a8c1
SO
205#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
1575b9d8
OG
209/* 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
c46ee386 215/* The rule: value + checksum = 0 */
63123108 216static inline u8 pn533_std_checksum(u8 value)
c46ee386
AAJ
217{
218 return ~value + 1;
219}
220
221/* The rule: sum(data elements) + checksum = 0 */
63123108 222static u8 pn533_std_data_checksum(u8 *data, int datalen)
c46ee386
AAJ
223{
224 u8 sum = 0;
225 int i;
226
227 for (i = 0; i < datalen; i++)
228 sum += data[i];
229
63123108 230 return pn533_std_checksum(sum);
c46ee386
AAJ
231}
232
63123108 233static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
c46ee386 234{
63123108 235 struct pn533_std_frame *frame = _frame;
9e2d493e 236
c46ee386 237 frame->preamble = 0;
63123108
WR
238 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
239 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
1575b9d8 240 PN533_FRAME_CMD(frame) = cmd_code;
c46ee386
AAJ
241 frame->datalen = 2;
242}
243
63123108 244static void pn533_std_tx_frame_finish(void *_frame)
c46ee386 245{
63123108 246 struct pn533_std_frame *frame = _frame;
9e2d493e 247
63123108 248 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
c46ee386 249
63123108
WR
250 PN533_STD_FRAME_CHECKSUM(frame) =
251 pn533_std_data_checksum(frame->data, frame->datalen);
c46ee386 252
63123108 253 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
c46ee386
AAJ
254}
255
63123108 256static void pn533_std_tx_update_payload_len(void *_frame, int len)
9e2d493e 257{
63123108 258 struct pn533_std_frame *frame = _frame;
9e2d493e
WR
259
260 frame->datalen += len;
261}
262
56a63c82 263static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
c46ee386
AAJ
264{
265 u8 checksum;
1575b9d8 266 struct pn533_std_frame *stdf = _frame;
c46ee386 267
1575b9d8 268 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
c46ee386
AAJ
269 return false;
270
1575b9d8
OG
271 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
272 /* Standard frame code */
56a63c82 273 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
1575b9d8
OG
274
275 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
56a63c82
OG
286 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
287
1575b9d8
OG
288 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 }
c46ee386
AAJ
298
299 return true;
300}
301
9815c7cf 302bool pn533_rx_frame_is_ack(void *_frame)
c46ee386 303{
9815c7cf
MT
304 struct pn533_std_frame *frame = _frame;
305
63123108 306 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
c46ee386
AAJ
307 return false;
308
309 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
310 return false;
311
312 return true;
313}
9815c7cf 314EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
c46ee386 315
63123108 316static inline int pn533_std_rx_frame_size(void *frame)
9e2d493e 317{
63123108 318 struct pn533_std_frame *f = frame;
9e2d493e 319
1575b9d8
OG
320 /* 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
63123108
WR
328 return sizeof(struct pn533_std_frame) + f->datalen +
329 PN533_STD_FRAME_TAIL_LEN;
9e2d493e
WR
330}
331
63123108 332static u8 pn533_std_get_cmd_code(void *frame)
9e2d493e 333{
63123108 334 struct pn533_std_frame *f = frame;
1575b9d8 335 struct pn533_ext_frame *eif = frame;
9e2d493e 336
1575b9d8
OG
337 if (PN533_STD_IS_EXTENDED(f))
338 return PN533_FRAME_CMD(eif);
339 else
340 return PN533_FRAME_CMD(f);
9e2d493e
WR
341}
342
9815c7cf
MT
343bool 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
ef3d56e1 351static struct pn533_frame_ops pn533_std_frame_ops = {
63123108
WR
352 .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,
357
358 .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,
362
363 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
364 .get_cmd_code = pn533_std_get_cmd_code,
9e2d493e
WR
365};
366
9e2d493e
WR
367static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
368 struct sk_buff *skb)
aada17ac 369{
aada17ac
WR
370 /* payload is already there, just update datalen */
371 int payload_len = skb->len;
9e2d493e 372 struct pn533_frame_ops *ops = dev->ops;
aada17ac 373
aada17ac 374
9e2d493e
WR
375 skb_push(skb, ops->tx_header_len);
376 skb_put(skb, ops->tx_tail_len);
aada17ac 377
9e2d493e
WR
378 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);
aada17ac
WR
381}
382
ddf19d20 383static int pn533_send_async_complete(struct pn533 *dev)
aada17ac 384{
ddf19d20 385 struct pn533_cmd *cmd = dev->cmd;
5dd9c1bd
AR
386 struct sk_buff *resp;
387 int status, rc = 0;
aada17ac 388
5dd9c1bd
AR
389 if (!cmd) {
390 dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
391 goto done;
392 }
aada17ac 393
5dd9c1bd 394 dev_kfree_skb(cmd->req);
aada17ac 395
5dd9c1bd
AR
396 status = cmd->status;
397 resp = cmd->resp;
aada17ac 398
0c33d262 399 if (status < 0) {
4231604b
WR
400 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
401 ERR_PTR(status));
aada17ac 402 dev_kfree_skb(resp);
2c206fb7 403 goto done;
aada17ac
WR
404 }
405
5dd9c1bd
AR
406 /* when no response is set we got interrupted */
407 if (!resp)
408 resp = ERR_PTR(-EINTR);
409
410 if (!IS_ERR(resp)) {
411 skb_pull(resp, dev->ops->rx_header_len);
412 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
413 }
aada17ac 414
4231604b 415 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
aada17ac 416
2c206fb7 417done:
4231604b 418 kfree(cmd);
2c206fb7 419 dev->cmd = NULL;
aada17ac
WR
420 return rc;
421}
422
423static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
9815c7cf 424 struct sk_buff *req,
aada17ac
WR
425 pn533_send_async_complete_t complete_cb,
426 void *complete_cb_context)
427{
428 struct pn533_cmd *cmd;
aada17ac
WR
429 int rc = 0;
430
9815c7cf 431 dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
aada17ac 432
4231604b
WR
433 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
434 if (!cmd)
aada17ac
WR
435 return -ENOMEM;
436
4b2a9532 437 cmd->code = cmd_code;
4231604b 438 cmd->req = req;
4231604b
WR
439 cmd->complete_cb = complete_cb;
440 cmd->complete_cb_context = complete_cb_context;
aada17ac 441
9e2d493e 442 pn533_build_cmd_frame(dev, cmd_code, req);
aada17ac
WR
443
444 mutex_lock(&dev->cmd_lock);
445
446 if (!dev->cmd_pending) {
5dd9c1bd 447 dev->cmd = cmd;
9815c7cf 448 rc = dev->phy_ops->send_frame(dev, req);
5dd9c1bd
AR
449 if (rc) {
450 dev->cmd = NULL;
aada17ac 451 goto error;
5dd9c1bd 452 }
aada17ac
WR
453
454 dev->cmd_pending = 1;
455 goto unlock;
456 }
457
9815c7cf 458 dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
b4834839 459 __func__, cmd_code);
aada17ac 460
aada17ac 461 INIT_LIST_HEAD(&cmd->queue);
aada17ac
WR
462 list_add_tail(&cmd->queue, &dev->cmd_queue);
463
464 goto unlock;
465
466error:
4231604b 467 kfree(cmd);
aada17ac
WR
468unlock:
469 mutex_unlock(&dev->cmd_lock);
470 return rc;
15461aeb
WR
471}
472
473static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
474 struct sk_buff *req,
475 pn533_send_async_complete_t complete_cb,
476 void *complete_cb_context)
477{
15461aeb 478 int rc;
15461aeb 479
9815c7cf 480 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
15461aeb 481 complete_cb_context);
15461aeb
WR
482
483 return rc;
aada17ac
WR
484}
485
486static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
487 struct sk_buff *req,
488 pn533_send_async_complete_t complete_cb,
489 void *complete_cb_context)
490{
aada17ac
WR
491 int rc;
492
9815c7cf 493 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
9e2d493e 494 complete_cb_context);
aada17ac
WR
495
496 return rc;
497}
498
b1e666f5
WR
499/*
500 * pn533_send_cmd_direct_async
501 *
0cf2a848 502 * The function sends a piority cmd directly to the chip omitting the cmd
b1e666f5
WR
503 * queue. It's intended to be used by chaining mechanism of received responses
504 * where the host has to request every single chunk of data before scheduling
505 * next cmd from the queue.
506 */
507static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
508 struct sk_buff *req,
509 pn533_send_async_complete_t complete_cb,
510 void *complete_cb_context)
511{
4231604b 512 struct pn533_cmd *cmd;
b1e666f5 513 int rc;
b1e666f5 514
4231604b 515 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
9815c7cf 516 if (!cmd)
b1e666f5 517 return -ENOMEM;
b1e666f5 518
4b2a9532 519 cmd->code = cmd_code;
4231604b 520 cmd->req = req;
4231604b
WR
521 cmd->complete_cb = complete_cb;
522 cmd->complete_cb_context = complete_cb_context;
b1e666f5 523
9e2d493e 524 pn533_build_cmd_frame(dev, cmd_code, req);
b1e666f5 525
5dd9c1bd 526 dev->cmd = cmd;
9815c7cf 527 rc = dev->phy_ops->send_frame(dev, req);
5dd9c1bd
AR
528 if (rc < 0) {
529 dev->cmd = NULL;
4231604b 530 kfree(cmd);
5dd9c1bd 531 }
b1e666f5
WR
532
533 return rc;
534}
535
c79490e1
WR
536static void pn533_wq_cmd_complete(struct work_struct *work)
537{
538 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
539 int rc;
540
541 rc = pn533_send_async_complete(dev);
542 if (rc != -EINPROGRESS)
543 queue_work(dev->wq, &dev->cmd_work);
544}
545
5d50b364
SO
546static void pn533_wq_cmd(struct work_struct *work)
547{
548 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
549 struct pn533_cmd *cmd;
0ce1fbdd 550 int rc;
5d50b364
SO
551
552 mutex_lock(&dev->cmd_lock);
553
554 if (list_empty(&dev->cmd_queue)) {
555 dev->cmd_pending = 0;
556 mutex_unlock(&dev->cmd_lock);
557 return;
558 }
559
560 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
561
60ad07ab
SJ
562 list_del(&cmd->queue);
563
5d50b364
SO
564 mutex_unlock(&dev->cmd_lock);
565
5dd9c1bd 566 dev->cmd = cmd;
9815c7cf 567 rc = dev->phy_ops->send_frame(dev, cmd->req);
0ce1fbdd 568 if (rc < 0) {
5dd9c1bd 569 dev->cmd = NULL;
0ce1fbdd 570 dev_kfree_skb(cmd->req);
4231604b 571 kfree(cmd);
2c206fb7 572 return;
0ce1fbdd 573 }
2c206fb7 574
5d50b364
SO
575}
576
c46ee386 577struct pn533_sync_cmd_response {
94c5c156 578 struct sk_buff *resp;
c46ee386
AAJ
579 struct completion done;
580};
581
94c5c156
WR
582static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
583 struct sk_buff *resp)
584{
585 struct pn533_sync_cmd_response *arg = _arg;
586
94c5c156
WR
587 arg->resp = resp;
588 complete(&arg->done);
589
590 return 0;
591}
592
593/* pn533_send_cmd_sync
594 *
595 * Please note the req parameter is freed inside the function to
596 * limit a number of return value interpretations by the caller.
597 *
598 * 1. negative in case of error during TX path -> req should be freed
599 *
600 * 2. negative in case of error during RX path -> req should not be freed
9815c7cf 601 * as it's been already freed at the beginning of RX path by
94c5c156
WR
602 * async_complete_cb.
603 *
604 * 3. valid pointer in case of succesfult RX path
605 *
606 * A caller has to check a return value with IS_ERR macro. If the test pass,
607 * the returned pointer is valid.
608 *
9815c7cf 609 */
94c5c156
WR
610static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
611 struct sk_buff *req)
612{
613 int rc;
614 struct pn533_sync_cmd_response arg;
615
94c5c156
WR
616 init_completion(&arg.done);
617
618 rc = pn533_send_cmd_async(dev, cmd_code, req,
619 pn533_send_sync_complete, &arg);
620 if (rc) {
621 dev_kfree_skb(req);
622 return ERR_PTR(rc);
623 }
624
625 wait_for_completion(&arg.done);
626
627 return arg.resp;
628}
629
9e2d493e 630static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
d22b2db6
WR
631{
632 struct sk_buff *skb;
633
9e2d493e 634 skb = alloc_skb(dev->ops->tx_header_len +
d22b2db6 635 size +
9e2d493e 636 dev->ops->tx_tail_len, GFP_KERNEL);
d22b2db6
WR
637
638 if (skb)
9e2d493e 639 skb_reserve(skb, dev->ops->tx_header_len);
d22b2db6
WR
640
641 return skb;
642}
643
c46ee386
AAJ
644struct pn533_target_type_a {
645 __be16 sens_res;
646 u8 sel_res;
647 u8 nfcid_len;
648 u8 nfcid_data[];
649} __packed;
650
651
652#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
653#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
654#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
655
656#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
657#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
658
659#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
660#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
661
662#define PN533_TYPE_A_SEL_PROT_MIFARE 0
663#define PN533_TYPE_A_SEL_PROT_ISO14443 1
664#define PN533_TYPE_A_SEL_PROT_DEP 2
665#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
666
667static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
668 int target_data_len)
669{
670 u8 ssd;
671 u8 platconf;
672
673 if (target_data_len < sizeof(struct pn533_target_type_a))
674 return false;
675
9815c7cf
MT
676 /*
677 * The length check of nfcid[] and ats[] are not being performed because
678 * the values are not being used
679 */
c46ee386
AAJ
680
681 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
682 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
683 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
684
685 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
5d467742
WR
686 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
687 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
688 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
c46ee386
AAJ
689 return false;
690
691 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
692 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
693 return false;
694
695 return true;
696}
697
698static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
699 int tgt_data_len)
700{
701 struct pn533_target_type_a *tgt_type_a;
702
37cf4fc6 703 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
c46ee386
AAJ
704
705 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
706 return -EPROTO;
707
708 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
709 case PN533_TYPE_A_SEL_PROT_MIFARE:
710 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
711 break;
712 case PN533_TYPE_A_SEL_PROT_ISO14443:
713 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
714 break;
715 case PN533_TYPE_A_SEL_PROT_DEP:
716 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
717 break;
718 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
719 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
720 NFC_PROTO_NFC_DEP_MASK;
721 break;
722 }
723
724 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
725 nfc_tgt->sel_res = tgt_type_a->sel_res;
c3b1e1e8
SO
726 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
727 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
c46ee386
AAJ
728
729 return 0;
730}
731
732struct pn533_target_felica {
733 u8 pol_res;
734 u8 opcode;
322bce95 735 u8 nfcid2[NFC_NFCID2_MAXSIZE];
c46ee386
AAJ
736 u8 pad[8];
737 /* optional */
738 u8 syst_code[];
739} __packed;
740
741#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
742#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
743
744static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
745 int target_data_len)
746{
747 if (target_data_len < sizeof(struct pn533_target_felica))
748 return false;
749
750 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
751 return false;
752
753 return true;
754}
755
756static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
757 int tgt_data_len)
758{
759 struct pn533_target_felica *tgt_felica;
760
37cf4fc6 761 tgt_felica = (struct pn533_target_felica *)tgt_data;
c46ee386
AAJ
762
763 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
764 return -EPROTO;
765
5d467742
WR
766 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
767 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
c46ee386
AAJ
768 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
769 else
770 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
771
7975754f
SO
772 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
773 nfc_tgt->sensf_res_len = 9;
774
322bce95
SO
775 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
776 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
777
c46ee386
AAJ
778 return 0;
779}
780
781struct pn533_target_jewel {
782 __be16 sens_res;
783 u8 jewelid[4];
784} __packed;
785
786static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
787 int target_data_len)
788{
789 u8 ssd;
790 u8 platconf;
791
792 if (target_data_len < sizeof(struct pn533_target_jewel))
793 return false;
794
795 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
796 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
797 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
798
799 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
5d467742
WR
800 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
801 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
802 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
c46ee386
AAJ
803 return false;
804
805 return true;
806}
807
808static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
809 int tgt_data_len)
810{
811 struct pn533_target_jewel *tgt_jewel;
812
37cf4fc6 813 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
c46ee386
AAJ
814
815 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
816 return -EPROTO;
817
818 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
819 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
d8dc1072
SO
820 nfc_tgt->nfcid1_len = 4;
821 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
c46ee386
AAJ
822
823 return 0;
824}
825
826struct pn533_type_b_prot_info {
827 u8 bitrate;
828 u8 fsci_type;
829 u8 fwi_adc_fo;
830} __packed;
831
832#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
833#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
834#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
835
836struct pn533_type_b_sens_res {
837 u8 opcode;
838 u8 nfcid[4];
839 u8 appdata[4];
840 struct pn533_type_b_prot_info prot_info;
841} __packed;
842
843#define PN533_TYPE_B_OPC_SENSB_RES 0x50
844
845struct pn533_target_type_b {
846 struct pn533_type_b_sens_res sensb_res;
847 u8 attrib_res_len;
848 u8 attrib_res[];
849} __packed;
850
851static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
852 int target_data_len)
853{
854 if (target_data_len < sizeof(struct pn533_target_type_b))
855 return false;
856
857 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
858 return false;
859
860 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
861 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
862 return false;
863
864 return true;
865}
866
867static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
868 int tgt_data_len)
869{
870 struct pn533_target_type_b *tgt_type_b;
871
37cf4fc6 872 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
c46ee386
AAJ
873
874 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
875 return -EPROTO;
876
01d719a2 877 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
c46ee386
AAJ
878
879 return 0;
880}
881
c952f915 882static void pn533_poll_reset_mod_list(struct pn533 *dev);
b5193e5d
WR
883static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
884 int tgdata_len)
c46ee386 885{
c46ee386
AAJ
886 struct nfc_target nfc_tgt;
887 int rc;
888
9815c7cf 889 dev_dbg(dev->dev, "%s: modulation=%d\n",
b4834839 890 __func__, dev->poll_mod_curr);
c46ee386 891
b5193e5d 892 if (tg != 1)
c46ee386
AAJ
893 return -EPROTO;
894
98b3ac1b
SO
895 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
896
c46ee386
AAJ
897 switch (dev->poll_mod_curr) {
898 case PN533_POLL_MOD_106KBPS_A:
b5193e5d 899 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
900 break;
901 case PN533_POLL_MOD_212KBPS_FELICA:
902 case PN533_POLL_MOD_424KBPS_FELICA:
b5193e5d 903 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
904 break;
905 case PN533_POLL_MOD_106KBPS_JEWEL:
b5193e5d 906 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
907 break;
908 case PN533_POLL_MOD_847KBPS_B:
b5193e5d 909 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
910 break;
911 default:
9815c7cf 912 nfc_err(dev->dev,
073a625f 913 "Unknown current poll modulation\n");
c46ee386
AAJ
914 return -EPROTO;
915 }
916
917 if (rc)
918 return rc;
919
920 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
9815c7cf 921 dev_dbg(dev->dev,
b4834839 922 "The Tg found doesn't have the desired protocol\n");
c46ee386
AAJ
923 return -EAGAIN;
924 }
925
9815c7cf 926 dev_dbg(dev->dev,
b4834839
JP
927 "Target found - supported protocols: 0x%x\n",
928 nfc_tgt.supported_protocols);
c46ee386
AAJ
929
930 dev->tgt_available_prots = nfc_tgt.supported_protocols;
931
c952f915 932 pn533_poll_reset_mod_list(dev);
c46ee386
AAJ
933 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
934
935 return 0;
936}
937
6fbbdc16
SO
938static inline void pn533_poll_next_mod(struct pn533 *dev)
939{
940 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
941}
942
c46ee386
AAJ
943static void pn533_poll_reset_mod_list(struct pn533 *dev)
944{
945 dev->poll_mod_count = 0;
946}
947
948static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
949{
950 dev->poll_mod_active[dev->poll_mod_count] =
37cf4fc6 951 (struct pn533_poll_modulations *)&poll_mod[mod_index];
c46ee386
AAJ
952 dev->poll_mod_count++;
953}
954
6fbbdc16
SO
955static void pn533_poll_create_mod_list(struct pn533 *dev,
956 u32 im_protocols, u32 tm_protocols)
c46ee386
AAJ
957{
958 pn533_poll_reset_mod_list(dev);
959
b08e8603
WR
960 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
961 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
962 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
c46ee386
AAJ
963 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
964
b08e8603
WR
965 if (im_protocols & NFC_PROTO_FELICA_MASK ||
966 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
c46ee386
AAJ
967 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
968 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
969 }
970
6fbbdc16 971 if (im_protocols & NFC_PROTO_JEWEL_MASK)
c46ee386
AAJ
972 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
973
01d719a2 974 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
c46ee386 975 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
c46ee386 976
6fbbdc16
SO
977 if (tm_protocols)
978 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
c46ee386
AAJ
979}
980
b5193e5d 981static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
c46ee386 982{
b5193e5d
WR
983 u8 nbtg, tg, *tgdata;
984 int rc, tgdata_len;
c46ee386 985
673088fb 986 /* Toggle the DEP polling */
e997ebbe
MT
987 if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
988 dev->poll_dep = 1;
673088fb 989
b5193e5d
WR
990 nbtg = resp->data[0];
991 tg = resp->data[1];
992 tgdata = &resp->data[2];
993 tgdata_len = resp->len - 2; /* nbtg + tg */
994
995 if (nbtg) {
996 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
c46ee386
AAJ
997
998 /* We must stop the poll after a valid target found */
c952f915 999 if (rc == 0)
6fbbdc16 1000 return 0;
c46ee386
AAJ
1001 }
1002
6fbbdc16 1003 return -EAGAIN;
c46ee386
AAJ
1004}
1005
9e2d493e 1006static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
ad3823ce 1007{
b5193e5d
WR
1008 struct sk_buff *skb;
1009 u8 *felica, *nfcid3, *gb;
1010
9e2d493e
WR
1011 u8 *gbytes = dev->gb;
1012 size_t gbytes_len = dev->gb_len;
1013
51d9e803
SO
1014 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1015 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1016 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1017 0xff, 0xff}; /* System code */
b5193e5d 1018
51d9e803
SO
1019 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1020 0x0, 0x0, 0x0,
1021 0x40}; /* SEL_RES for DEP */
ad3823ce 1022
9815c7cf
MT
1023 unsigned int skb_len = 36 + /*
1024 * mode (1), mifare (6),
1025 * felica (18), nfcid3 (10), gb_len (1)
1026 */
b5193e5d
WR
1027 gbytes_len +
1028 1; /* len Tk*/
ad3823ce 1029
9e2d493e 1030 skb = pn533_alloc_skb(dev, skb_len);
b5193e5d
WR
1031 if (!skb)
1032 return NULL;
ad3823ce
SO
1033
1034 /* DEP support only */
52f2eaee 1035 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
b5193e5d
WR
1036
1037 /* MIFARE params */
1038 memcpy(skb_put(skb, 6), mifare_params, 6);
51d9e803
SO
1039
1040 /* Felica params */
b5193e5d
WR
1041 felica = skb_put(skb, 18);
1042 memcpy(felica, felica_params, 18);
1043 get_random_bytes(felica + 2, 6);
51d9e803
SO
1044
1045 /* NFCID3 */
aa9f979c 1046 nfcid3 = skb_put_zero(skb, 10);
b5193e5d 1047 memcpy(nfcid3, felica, 8);
51d9e803
SO
1048
1049 /* General bytes */
b5193e5d 1050 *skb_put(skb, 1) = gbytes_len;
51d9e803 1051
b5193e5d
WR
1052 gb = skb_put(skb, gbytes_len);
1053 memcpy(gb, gbytes, gbytes_len);
ad3823ce 1054
b5193e5d
WR
1055 /* Len Tk */
1056 *skb_put(skb, 1) = 0;
51d9e803 1057
b5193e5d 1058 return skb;
ad3823ce
SO
1059}
1060
3c13b244
OG
1061static void pn533_wq_tm_mi_recv(struct work_struct *work);
1062static struct sk_buff *pn533_build_response(struct pn533 *dev);
1063
103b34cf 1064static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
e4878823 1065 struct sk_buff *resp)
103b34cf 1066{
3c13b244
OG
1067 struct sk_buff *skb;
1068 u8 status, ret, mi;
1069 int rc;
103b34cf 1070
9815c7cf 1071 dev_dbg(dev->dev, "%s\n", __func__);
103b34cf 1072
3c13b244
OG
1073 if (IS_ERR(resp)) {
1074 skb_queue_purge(&dev->resp_q);
e4878823 1075 return PTR_ERR(resp);
3c13b244 1076 }
103b34cf 1077
e4878823 1078 status = resp->data[0];
3c13b244
OG
1079
1080 ret = status & PN533_CMD_RET_MASK;
1081 mi = status & PN533_CMD_MI_MASK;
1082
e4878823 1083 skb_pull(resp, sizeof(status));
103b34cf 1084
3c13b244
OG
1085 if (ret != PN533_CMD_RET_SUCCESS) {
1086 rc = -EIO;
1087 goto error;
1088 }
1089
1090 skb_queue_tail(&dev->resp_q, resp);
1091
1092 if (mi) {
1093 queue_work(dev->wq, &dev->mi_tm_rx_work);
1094 return -EINPROGRESS;
1095 }
1096
1097 skb = pn533_build_response(dev);
1098 if (!skb) {
1099 rc = -EIO;
1100 goto error;
103b34cf
SO
1101 }
1102
3c13b244
OG
1103 return nfc_tm_data_received(dev->nfc_dev, skb);
1104
1105error:
1106 nfc_tm_deactivated(dev->nfc_dev);
1107 dev->tgt_mode = 0;
1108 skb_queue_purge(&dev->resp_q);
1109 dev_kfree_skb(resp);
1110
1111 return rc;
1112}
1113
1114static void pn533_wq_tm_mi_recv(struct work_struct *work)
1115{
1116 struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1117 struct sk_buff *skb;
1118 int rc;
1119
9815c7cf 1120 dev_dbg(dev->dev, "%s\n", __func__);
3c13b244
OG
1121
1122 skb = pn533_alloc_skb(dev, 0);
1123 if (!skb)
1124 return;
1125
1126 rc = pn533_send_cmd_direct_async(dev,
1127 PN533_CMD_TG_GET_DATA,
1128 skb,
1129 pn533_tm_get_data_complete,
1130 NULL);
1131
1132 if (rc < 0)
1133 dev_kfree_skb(skb);
103b34cf
SO
1134}
1135
93ad4202
OG
1136static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1137 struct sk_buff *resp);
1138static void pn533_wq_tm_mi_send(struct work_struct *work)
1139{
1140 struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1141 struct sk_buff *skb;
1142 int rc;
1143
9815c7cf 1144 dev_dbg(dev->dev, "%s\n", __func__);
93ad4202
OG
1145
1146 /* Grab the first skb in the queue */
1147 skb = skb_dequeue(&dev->fragment_skb);
1148 if (skb == NULL) { /* No more data */
1149 /* Reset the queue for future use */
1150 skb_queue_head_init(&dev->fragment_skb);
1151 goto error;
1152 }
1153
1154 /* last entry - remove MI bit */
1155 if (skb_queue_len(&dev->fragment_skb) == 0) {
1156 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1157 skb, pn533_tm_send_complete, NULL);
1158 } else
1159 rc = pn533_send_cmd_direct_async(dev,
1160 PN533_CMD_TG_SET_META_DATA,
1161 skb, pn533_tm_send_complete, NULL);
1162
1163 if (rc == 0) /* success */
1164 return;
1165
9815c7cf 1166 dev_err(dev->dev,
93ad4202
OG
1167 "Error %d when trying to perform set meta data_exchange", rc);
1168
1169 dev_kfree_skb(skb);
1170
1171error:
9815c7cf 1172 dev->phy_ops->send_ack(dev, GFP_KERNEL);
93ad4202
OG
1173 queue_work(dev->wq, &dev->cmd_work);
1174}
1175
103b34cf
SO
1176static void pn533_wq_tg_get_data(struct work_struct *work)
1177{
1178 struct pn533 *dev = container_of(work, struct pn533, tg_work);
e4878823
WR
1179 struct sk_buff *skb;
1180 int rc;
103b34cf 1181
9815c7cf 1182 dev_dbg(dev->dev, "%s\n", __func__);
103b34cf 1183
9e2d493e 1184 skb = pn533_alloc_skb(dev, 0);
e4878823 1185 if (!skb)
103b34cf
SO
1186 return;
1187
e4878823
WR
1188 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1189 pn533_tm_get_data_complete, NULL);
103b34cf 1190
e4878823
WR
1191 if (rc < 0)
1192 dev_kfree_skb(skb);
103b34cf
SO
1193}
1194
fc40a8c1 1195#define ATR_REQ_GB_OFFSET 17
b5193e5d 1196static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
fe7c5800 1197{
b5193e5d 1198 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
fc40a8c1 1199 size_t gb_len;
103b34cf 1200 int rc;
ad3823ce 1201
9815c7cf 1202 dev_dbg(dev->dev, "%s\n", __func__);
ad3823ce 1203
b5193e5d 1204 if (resp->len < ATR_REQ_GB_OFFSET + 1)
fc40a8c1
SO
1205 return -EINVAL;
1206
b5193e5d
WR
1207 mode = resp->data[0];
1208 cmd = &resp->data[1];
ad3823ce 1209
9815c7cf 1210 dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
b4834839 1211 mode, resp->len);
ad3823ce 1212
b5193e5d
WR
1213 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1214 PN533_INIT_TARGET_RESP_ACTIVE)
fc40a8c1
SO
1215 comm_mode = NFC_COMM_ACTIVE;
1216
b5193e5d 1217 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
fc40a8c1
SO
1218 return -EOPNOTSUPP;
1219
b5193e5d
WR
1220 gb = cmd + ATR_REQ_GB_OFFSET;
1221 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
fc40a8c1 1222
103b34cf
SO
1223 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1224 comm_mode, gb, gb_len);
1225 if (rc < 0) {
9815c7cf 1226 nfc_err(dev->dev,
073a625f 1227 "Error when signaling target activation\n");
103b34cf
SO
1228 return rc;
1229 }
1230
51ad304c 1231 dev->tgt_mode = 1;
103b34cf
SO
1232 queue_work(dev->wq, &dev->tg_work);
1233
1234 return 0;
fe7c5800
SO
1235}
1236
6fbbdc16 1237static void pn533_listen_mode_timer(unsigned long data)
ad3823ce 1238{
37cf4fc6 1239 struct pn533 *dev = (struct pn533 *)data;
6fbbdc16 1240
9815c7cf 1241 dev_dbg(dev->dev, "Listen mode timeout\n");
6fbbdc16 1242
6fbbdc16
SO
1243 dev->cancel_listen = 1;
1244
6fbbdc16
SO
1245 pn533_poll_next_mod(dev);
1246
46f793b0
SO
1247 queue_delayed_work(dev->wq, &dev->poll_work,
1248 msecs_to_jiffies(PN533_POLL_INTERVAL));
6fbbdc16
SO
1249}
1250
17e9d9d4
SO
1251static int pn533_rf_complete(struct pn533 *dev, void *arg,
1252 struct sk_buff *resp)
1253{
1254 int rc = 0;
1255
9815c7cf 1256 dev_dbg(dev->dev, "%s\n", __func__);
17e9d9d4
SO
1257
1258 if (IS_ERR(resp)) {
1259 rc = PTR_ERR(resp);
1260
9815c7cf 1261 nfc_err(dev->dev, "RF setting error %d\n", rc);
17e9d9d4
SO
1262
1263 return rc;
1264 }
1265
46f793b0
SO
1266 queue_delayed_work(dev->wq, &dev->poll_work,
1267 msecs_to_jiffies(PN533_POLL_INTERVAL));
17e9d9d4
SO
1268
1269 dev_kfree_skb(resp);
1270 return rc;
1271}
1272
1273static void pn533_wq_rf(struct work_struct *work)
1274{
1275 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1276 struct sk_buff *skb;
1277 int rc;
1278
9815c7cf 1279 dev_dbg(dev->dev, "%s\n", __func__);
17e9d9d4
SO
1280
1281 skb = pn533_alloc_skb(dev, 2);
1282 if (!skb)
1283 return;
1284
1285 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
3a8eab39 1286 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
17e9d9d4
SO
1287
1288 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1289 pn533_rf_complete, NULL);
1290 if (rc < 0) {
1291 dev_kfree_skb(skb);
9815c7cf 1292 nfc_err(dev->dev, "RF setting error %d\n", rc);
17e9d9d4 1293 }
17e9d9d4
SO
1294}
1295
673088fb
SO
1296static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1297 struct sk_buff *resp)
1298{
1299 struct pn533_cmd_jump_dep_response *rsp;
1300 struct nfc_target nfc_target;
1301 u8 target_gt_len;
1302 int rc;
1303
1304 if (IS_ERR(resp))
1305 return PTR_ERR(resp);
1306
1307 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1308
1309 rc = rsp->status & PN533_CMD_RET_MASK;
1310 if (rc != PN533_CMD_RET_SUCCESS) {
1311 /* Not target found, turn radio off */
1312 queue_work(dev->wq, &dev->rf_work);
1313
1314 dev_kfree_skb(resp);
1315 return 0;
1316 }
1317
9815c7cf 1318 dev_dbg(dev->dev, "Creating new target");
673088fb
SO
1319
1320 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1321 nfc_target.nfcid1_len = 10;
1322 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1323 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1324 if (rc)
1325 goto error;
1326
1327 dev->tgt_available_prots = 0;
1328 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1329
1330 /* ATR_RES general bytes are located at offset 17 */
1331 target_gt_len = resp->len - 17;
1332 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1333 rsp->gt, target_gt_len);
1334 if (!rc) {
1335 rc = nfc_dep_link_is_up(dev->nfc_dev,
1336 dev->nfc_dev->targets[0].idx,
1337 0, NFC_RF_INITIATOR);
1338
1339 if (!rc)
1340 pn533_poll_reset_mod_list(dev);
1341 }
1342error:
1343 dev_kfree_skb(resp);
1344 return rc;
1345}
1346
1347#define PASSIVE_DATA_LEN 5
1348static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1349{
1350 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1351 struct sk_buff *skb;
1352 int rc, skb_len;
1353 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1354 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1355
9815c7cf 1356 dev_dbg(dev->dev, "%s", __func__);
673088fb
SO
1357
1358 if (!dev->gb) {
1359 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1360
1361 if (!dev->gb || !dev->gb_len) {
1362 dev->poll_dep = 0;
1363 queue_work(dev->wq, &dev->rf_work);
1364 }
1365 }
1366
1367 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1368 skb_len += PASSIVE_DATA_LEN;
1369
1370 /* NFCID3 */
1371 skb_len += NFC_NFCID3_MAXSIZE;
1372 nfcid3[0] = 0x1;
1373 nfcid3[1] = 0xfe;
1374 get_random_bytes(nfcid3 + 2, 6);
1375
1376 skb = pn533_alloc_skb(dev, skb_len);
1377 if (!skb)
1378 return -ENOMEM;
1379
1380 *skb_put(skb, 1) = 0x01; /* Active */
1381 *skb_put(skb, 1) = 0x02; /* 424 kbps */
1382
1383 next = skb_put(skb, 1); /* Next */
1384 *next = 0;
1385
1386 /* Copy passive data */
1387 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1388 *next |= 1;
1389
1390 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1391 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1392 NFC_NFCID3_MAXSIZE);
1393 *next |= 2;
1394
1395 memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
1396 *next |= 4; /* We have some Gi */
1397
1398 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1399 pn533_poll_dep_complete, NULL);
1400
1401 if (rc < 0)
1402 dev_kfree_skb(skb);
1403
1404 return rc;
1405}
1406
6fbbdc16 1407static int pn533_poll_complete(struct pn533 *dev, void *arg,
b5193e5d 1408 struct sk_buff *resp)
6fbbdc16
SO
1409{
1410 struct pn533_poll_modulations *cur_mod;
ad3823ce
SO
1411 int rc;
1412
9815c7cf 1413 dev_dbg(dev->dev, "%s\n", __func__);
ad3823ce 1414
b5193e5d
WR
1415 if (IS_ERR(resp)) {
1416 rc = PTR_ERR(resp);
1417
9815c7cf 1418 nfc_err(dev->dev, "%s Poll complete error %d\n",
073a625f 1419 __func__, rc);
b5193e5d
WR
1420
1421 if (rc == -ENOENT) {
1422 if (dev->poll_mod_count != 0)
1423 return rc;
9815c7cf 1424 goto stop_poll;
b5193e5d 1425 } else if (rc < 0) {
9815c7cf 1426 nfc_err(dev->dev,
073a625f 1427 "Error %d when running poll\n", rc);
b5193e5d
WR
1428 goto stop_poll;
1429 }
6fbbdc16 1430 }
ad3823ce 1431
6fbbdc16
SO
1432 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1433
b5193e5d 1434 if (cur_mod->len == 0) { /* Target mode */
6fbbdc16 1435 del_timer(&dev->listen_timer);
b5193e5d
WR
1436 rc = pn533_init_target_complete(dev, resp);
1437 goto done;
6fbbdc16
SO
1438 }
1439
b5193e5d
WR
1440 /* Initiator mode */
1441 rc = pn533_start_poll_complete(dev, resp);
1442 if (!rc)
1443 goto done;
6fbbdc16 1444
95cb9e10 1445 if (!dev->poll_mod_count) {
9815c7cf 1446 dev_dbg(dev->dev, "Polling has been stopped\n");
95cb9e10
WR
1447 goto done;
1448 }
1449
b5193e5d 1450 pn533_poll_next_mod(dev);
17e9d9d4
SO
1451 /* Not target found, turn radio off */
1452 queue_work(dev->wq, &dev->rf_work);
6fbbdc16 1453
b5193e5d
WR
1454done:
1455 dev_kfree_skb(resp);
1456 return rc;
6fbbdc16
SO
1457
1458stop_poll:
9815c7cf 1459 nfc_err(dev->dev, "Polling operation has been stopped\n");
b5193e5d 1460
6fbbdc16
SO
1461 pn533_poll_reset_mod_list(dev);
1462 dev->poll_protocols = 0;
b5193e5d 1463 return rc;
ad3823ce
SO
1464}
1465
9e2d493e
WR
1466static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1467 struct pn533_poll_modulations *mod)
c46ee386 1468{
b5193e5d 1469 struct sk_buff *skb;
c46ee386 1470
9e2d493e 1471 skb = pn533_alloc_skb(dev, mod->len);
b5193e5d
WR
1472 if (!skb)
1473 return NULL;
c46ee386 1474
b5193e5d 1475 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
c46ee386 1476
b5193e5d 1477 return skb;
6fbbdc16 1478}
c46ee386 1479
6fbbdc16
SO
1480static int pn533_send_poll_frame(struct pn533 *dev)
1481{
b5193e5d
WR
1482 struct pn533_poll_modulations *mod;
1483 struct sk_buff *skb;
6fbbdc16 1484 int rc;
b5193e5d 1485 u8 cmd_code;
c46ee386 1486
b5193e5d 1487 mod = dev->poll_mod_active[dev->poll_mod_curr];
c46ee386 1488
9815c7cf 1489 dev_dbg(dev->dev, "%s mod len %d\n",
b4834839 1490 __func__, mod->len);
c46ee386 1491
e997ebbe 1492 if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) {
673088fb
SO
1493 dev->poll_dep = 0;
1494 return pn533_poll_dep(dev->nfc_dev);
1495 }
1496
b5193e5d
WR
1497 if (mod->len == 0) { /* Listen mode */
1498 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
9e2d493e 1499 skb = pn533_alloc_poll_tg_frame(dev);
b5193e5d
WR
1500 } else { /* Polling mode */
1501 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
9e2d493e 1502 skb = pn533_alloc_poll_in_frame(dev, mod);
b5193e5d
WR
1503 }
1504
1505 if (!skb) {
9815c7cf 1506 nfc_err(dev->dev, "Failed to allocate skb\n");
b5193e5d
WR
1507 return -ENOMEM;
1508 }
1509
1510 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1511 NULL);
1512 if (rc < 0) {
1513 dev_kfree_skb(skb);
9815c7cf 1514 nfc_err(dev->dev, "Polling loop error %d\n", rc);
b5193e5d 1515 }
c46ee386 1516
6fbbdc16
SO
1517 return rc;
1518}
1519
1520static void pn533_wq_poll(struct work_struct *work)
1521{
46f793b0 1522 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
6fbbdc16
SO
1523 struct pn533_poll_modulations *cur_mod;
1524 int rc;
1525
1526 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1527
9815c7cf 1528 dev_dbg(dev->dev,
b4834839
JP
1529 "%s cancel_listen %d modulation len %d\n",
1530 __func__, dev->cancel_listen, cur_mod->len);
6fbbdc16
SO
1531
1532 if (dev->cancel_listen == 1) {
1533 dev->cancel_listen = 0;
9815c7cf 1534 dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
c46ee386
AAJ
1535 }
1536
6fbbdc16
SO
1537 rc = pn533_send_poll_frame(dev);
1538 if (rc)
1539 return;
c46ee386 1540
6fbbdc16
SO
1541 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1542 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
c46ee386
AAJ
1543}
1544
fe7c5800
SO
1545static int pn533_start_poll(struct nfc_dev *nfc_dev,
1546 u32 im_protocols, u32 tm_protocols)
1547{
1548 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
cec4b8ed 1549 struct pn533_poll_modulations *cur_mod;
dfccd0f5 1550 u8 rand_mod;
cec4b8ed 1551 int rc;
fe7c5800 1552
9815c7cf 1553 dev_dbg(dev->dev,
b4834839
JP
1554 "%s: im protocols 0x%x tm protocols 0x%x\n",
1555 __func__, im_protocols, tm_protocols);
fe7c5800
SO
1556
1557 if (dev->tgt_active_prot) {
9815c7cf 1558 nfc_err(dev->dev,
073a625f 1559 "Cannot poll with a target already activated\n");
fe7c5800
SO
1560 return -EBUSY;
1561 }
1562
51ad304c 1563 if (dev->tgt_mode) {
9815c7cf 1564 nfc_err(dev->dev,
073a625f 1565 "Cannot poll while already being activated\n");
51ad304c
SO
1566 return -EBUSY;
1567 }
1568
6fbbdc16
SO
1569 if (tm_protocols) {
1570 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1571 if (dev->gb == NULL)
1572 tm_protocols = 0;
1573 }
ad3823ce 1574
6fbbdc16
SO
1575 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1576 dev->poll_protocols = im_protocols;
1577 dev->listen_protocols = tm_protocols;
ad3823ce 1578
dfccd0f5
SO
1579 /* Do not always start polling from the same modulation */
1580 get_random_bytes(&rand_mod, sizeof(rand_mod));
1581 rand_mod %= dev->poll_mod_count;
1582 dev->poll_mod_curr = rand_mod;
1583
cec4b8ed
SO
1584 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1585
1586 rc = pn533_send_poll_frame(dev);
1587
1588 /* Start listen timer */
1589 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
1590 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1591
1592 return rc;
fe7c5800
SO
1593}
1594
c46ee386
AAJ
1595static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1596{
1597 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1598
6fbbdc16
SO
1599 del_timer(&dev->listen_timer);
1600
c46ee386 1601 if (!dev->poll_mod_count) {
9815c7cf 1602 dev_dbg(dev->dev,
b4834839 1603 "Polling operation was not running\n");
c46ee386
AAJ
1604 return;
1605 }
1606
9815c7cf 1607 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
46f793b0 1608 flush_delayed_work(&dev->poll_work);
7c2a04a9 1609 pn533_poll_reset_mod_list(dev);
c46ee386
AAJ
1610}
1611
1612static int pn533_activate_target_nfcdep(struct pn533 *dev)
1613{
cb950d93 1614 struct pn533_cmd_activate_response *rsp;
541d920b 1615 u16 gt_len;
c46ee386 1616 int rc;
cb950d93
WR
1617 struct sk_buff *skb;
1618 struct sk_buff *resp;
c46ee386 1619
9815c7cf 1620 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386 1621
9e2d493e 1622 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
cb950d93
WR
1623 if (!skb)
1624 return -ENOMEM;
c46ee386 1625
cb950d93
WR
1626 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1627 *skb_put(skb, sizeof(u8)) = 0; /* Next */
c46ee386 1628
cb950d93
WR
1629 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1630 if (IS_ERR(resp))
1631 return PTR_ERR(resp);
c46ee386 1632
37cf4fc6 1633 rsp = (struct pn533_cmd_activate_response *)resp->data;
cb950d93 1634 rc = rsp->status & PN533_CMD_RET_MASK;
8a0ecfe7 1635 if (rc != PN533_CMD_RET_SUCCESS) {
9815c7cf 1636 nfc_err(dev->dev,
073a625f 1637 "Target activation failed (error 0x%x)\n", rc);
cb950d93 1638 dev_kfree_skb(resp);
c46ee386 1639 return -EIO;
8a0ecfe7 1640 }
c46ee386 1641
541d920b 1642 /* ATR_RES general bytes are located at offset 16 */
cb950d93
WR
1643 gt_len = resp->len - 16;
1644 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
541d920b 1645
cb950d93 1646 dev_kfree_skb(resp);
541d920b 1647 return rc;
c46ee386
AAJ
1648}
1649
90099433
EL
1650static int pn533_activate_target(struct nfc_dev *nfc_dev,
1651 struct nfc_target *target, u32 protocol)
c46ee386
AAJ
1652{
1653 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1654 int rc;
1655
9815c7cf 1656 dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
c46ee386
AAJ
1657
1658 if (dev->poll_mod_count) {
9815c7cf 1659 nfc_err(dev->dev,
b4834839 1660 "Cannot activate while polling\n");
c46ee386
AAJ
1661 return -EBUSY;
1662 }
1663
1664 if (dev->tgt_active_prot) {
9815c7cf 1665 nfc_err(dev->dev,
073a625f 1666 "There is already an active target\n");
c46ee386
AAJ
1667 return -EBUSY;
1668 }
1669
1670 if (!dev->tgt_available_prots) {
9815c7cf 1671 nfc_err(dev->dev,
073a625f 1672 "There is no available target to activate\n");
c46ee386
AAJ
1673 return -EINVAL;
1674 }
1675
1676 if (!(dev->tgt_available_prots & (1 << protocol))) {
9815c7cf 1677 nfc_err(dev->dev,
073a625f
JP
1678 "Target doesn't support requested proto %u\n",
1679 protocol);
c46ee386
AAJ
1680 return -EINVAL;
1681 }
1682
1683 if (protocol == NFC_PROTO_NFC_DEP) {
1684 rc = pn533_activate_target_nfcdep(dev);
1685 if (rc) {
9815c7cf 1686 nfc_err(dev->dev,
073a625f 1687 "Activating target with DEP failed %d\n", rc);
c46ee386
AAJ
1688 return rc;
1689 }
1690 }
1691
1692 dev->tgt_active_prot = protocol;
1693 dev->tgt_available_prots = 0;
1694
1695 return 0;
1696}
1697
37f895d7
MT
1698static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1699 struct sk_buff *resp)
1700{
1701 int rc = 0;
1702
9815c7cf 1703 dev_dbg(dev->dev, "%s\n", __func__);
37f895d7
MT
1704
1705 if (IS_ERR(resp)) {
1706 rc = PTR_ERR(resp);
1707
9815c7cf 1708 nfc_err(dev->dev, "Target release error %d\n", rc);
37f895d7
MT
1709
1710 return rc;
1711 }
1712
1713 rc = resp->data[0] & PN533_CMD_RET_MASK;
1714 if (rc != PN533_CMD_RET_SUCCESS)
9815c7cf 1715 nfc_err(dev->dev,
37f895d7
MT
1716 "Error 0x%x when releasing the target\n", rc);
1717
1718 dev_kfree_skb(resp);
1719 return rc;
1720}
1721
90099433 1722static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
96d4581f 1723 struct nfc_target *target, u8 mode)
c46ee386
AAJ
1724{
1725 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
cb950d93 1726 struct sk_buff *skb;
c46ee386
AAJ
1727 int rc;
1728
9815c7cf 1729 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386
AAJ
1730
1731 if (!dev->tgt_active_prot) {
9815c7cf 1732 nfc_err(dev->dev, "There is no active target\n");
c46ee386
AAJ
1733 return;
1734 }
1735
1736 dev->tgt_active_prot = 0;
6ff73fd2
SO
1737 skb_queue_purge(&dev->resp_q);
1738
9e2d493e 1739 skb = pn533_alloc_skb(dev, sizeof(u8));
cb950d93
WR
1740 if (!skb)
1741 return;
c46ee386 1742
cb950d93 1743 *skb_put(skb, 1) = 1; /* TG*/
c46ee386 1744
37f895d7
MT
1745 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
1746 pn533_deactivate_target_complete, NULL);
1747 if (rc < 0) {
1748 dev_kfree_skb(skb);
9815c7cf 1749 nfc_err(dev->dev, "Target release error %d\n", rc);
37f895d7 1750 }
c46ee386
AAJ
1751}
1752
361f3cb7
SO
1753
1754static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
13003649 1755 struct sk_buff *resp)
361f3cb7 1756{
13003649 1757 struct pn533_cmd_jump_dep_response *rsp;
361f3cb7
SO
1758 u8 target_gt_len;
1759 int rc;
13003649 1760 u8 active = *(u8 *)arg;
70418e6e
WR
1761
1762 kfree(arg);
361f3cb7 1763
13003649
WR
1764 if (IS_ERR(resp))
1765 return PTR_ERR(resp);
361f3cb7
SO
1766
1767 if (dev->tgt_available_prots &&
1768 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
9815c7cf 1769 nfc_err(dev->dev,
073a625f 1770 "The target does not support DEP\n");
13003649
WR
1771 rc = -EINVAL;
1772 goto error;
361f3cb7
SO
1773 }
1774
13003649
WR
1775 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1776
1777 rc = rsp->status & PN533_CMD_RET_MASK;
361f3cb7 1778 if (rc != PN533_CMD_RET_SUCCESS) {
9815c7cf 1779 nfc_err(dev->dev,
073a625f 1780 "Bringing DEP link up failed (error 0x%x)\n", rc);
13003649 1781 goto error;
361f3cb7
SO
1782 }
1783
1784 if (!dev->tgt_available_prots) {
13003649
WR
1785 struct nfc_target nfc_target;
1786
9815c7cf 1787 dev_dbg(dev->dev, "Creating new target\n");
361f3cb7
SO
1788
1789 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2fbabfa4 1790 nfc_target.nfcid1_len = 10;
13003649 1791 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
361f3cb7
SO
1792 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1793 if (rc)
13003649 1794 goto error;
361f3cb7
SO
1795
1796 dev->tgt_available_prots = 0;
1797 }
1798
1799 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1800
1801 /* ATR_RES general bytes are located at offset 17 */
13003649 1802 target_gt_len = resp->len - 17;
361f3cb7 1803 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
13003649 1804 rsp->gt, target_gt_len);
361f3cb7
SO
1805 if (rc == 0)
1806 rc = nfc_dep_link_is_up(dev->nfc_dev,
13003649
WR
1807 dev->nfc_dev->targets[0].idx,
1808 !active, NFC_RF_INITIATOR);
361f3cb7 1809
13003649
WR
1810error:
1811 dev_kfree_skb(resp);
1812 return rc;
361f3cb7
SO
1813}
1814
17e9d9d4 1815static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
90099433 1816static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
37cf4fc6 1817 u8 comm_mode, u8 *gb, size_t gb_len)
361f3cb7
SO
1818{
1819 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
13003649 1820 struct sk_buff *skb;
5eef4845
SO
1821 int rc, skb_len;
1822 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
d7f3345d 1823 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
361f3cb7 1824
9815c7cf 1825 dev_dbg(dev->dev, "%s\n", __func__);
361f3cb7 1826
361f3cb7 1827 if (dev->poll_mod_count) {
9815c7cf 1828 nfc_err(dev->dev,
073a625f 1829 "Cannot bring the DEP link up while polling\n");
361f3cb7
SO
1830 return -EBUSY;
1831 }
1832
1833 if (dev->tgt_active_prot) {
9815c7cf 1834 nfc_err(dev->dev,
073a625f 1835 "There is already an active target\n");
361f3cb7
SO
1836 return -EBUSY;
1837 }
1838
13003649 1839 skb_len = 3 + gb_len; /* ActPass + BR + Next */
5eef4845 1840 skb_len += PASSIVE_DATA_LEN;
d7f3345d 1841
5eef4845
SO
1842 /* NFCID3 */
1843 skb_len += NFC_NFCID3_MAXSIZE;
1844 if (target && !target->nfcid2_len) {
1845 nfcid3[0] = 0x1;
1846 nfcid3[1] = 0xfe;
1847 get_random_bytes(nfcid3 + 2, 6);
1848 }
322bce95 1849
9e2d493e 1850 skb = pn533_alloc_skb(dev, skb_len);
13003649 1851 if (!skb)
361f3cb7
SO
1852 return -ENOMEM;
1853
13003649 1854 *skb_put(skb, 1) = !comm_mode; /* ActPass */
5eef4845 1855 *skb_put(skb, 1) = 0x02; /* 424 kbps */
13003649
WR
1856
1857 next = skb_put(skb, 1); /* Next */
1858 *next = 0;
361f3cb7 1859
5eef4845
SO
1860 /* Copy passive data */
1861 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1862 *next |= 1;
d7f3345d 1863
5eef4845
SO
1864 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1865 if (target && target->nfcid2_len)
322bce95
SO
1866 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
1867 target->nfcid2_len);
5eef4845
SO
1868 else
1869 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1870 NFC_NFCID3_MAXSIZE);
1871 *next |= 2;
322bce95 1872
47807d3d 1873 if (gb != NULL && gb_len > 0) {
13003649
WR
1874 memcpy(skb_put(skb, gb_len), gb, gb_len);
1875 *next |= 4; /* We have some Gi */
361f3cb7 1876 } else {
13003649 1877 *next = 0;
361f3cb7
SO
1878 }
1879
13003649
WR
1880 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1881 if (!arg) {
1882 dev_kfree_skb(skb);
1883 return -ENOMEM;
1884 }
361f3cb7 1885
13003649 1886 *arg = !comm_mode;
361f3cb7 1887
17e9d9d4
SO
1888 pn533_rf_field(dev->nfc_dev, 0);
1889
13003649
WR
1890 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1891 pn533_in_dep_link_up_complete, arg);
1892
1893 if (rc < 0) {
1894 dev_kfree_skb(skb);
1895 kfree(arg);
1896 }
361f3cb7
SO
1897
1898 return rc;
1899}
1900
1901static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1902{
6fbbdc16
SO
1903 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1904
9815c7cf 1905 dev_dbg(dev->dev, "%s\n", __func__);
fcfafc76 1906
6fbbdc16
SO
1907 pn533_poll_reset_mod_list(dev);
1908
10cff29a 1909 if (dev->tgt_mode || dev->tgt_active_prot)
9815c7cf 1910 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
51ad304c
SO
1911
1912 dev->tgt_active_prot = 0;
1913 dev->tgt_mode = 0;
1914
1915 skb_queue_purge(&dev->resp_q);
361f3cb7
SO
1916
1917 return 0;
1918}
1919
c46ee386 1920struct pn533_data_exchange_arg {
c46ee386
AAJ
1921 data_exchange_cb_t cb;
1922 void *cb_context;
1923};
1924
6ff73fd2
SO
1925static struct sk_buff *pn533_build_response(struct pn533 *dev)
1926{
1927 struct sk_buff *skb, *tmp, *t;
1928 unsigned int skb_len = 0, tmp_len = 0;
1929
9815c7cf 1930 dev_dbg(dev->dev, "%s\n", __func__);
6ff73fd2
SO
1931
1932 if (skb_queue_empty(&dev->resp_q))
1933 return NULL;
1934
1935 if (skb_queue_len(&dev->resp_q) == 1) {
1936 skb = skb_dequeue(&dev->resp_q);
1937 goto out;
1938 }
1939
1940 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1941 skb_len += tmp->len;
1942
9815c7cf 1943 dev_dbg(dev->dev, "%s total length %d\n",
b4834839 1944 __func__, skb_len);
6ff73fd2
SO
1945
1946 skb = alloc_skb(skb_len, GFP_KERNEL);
1947 if (skb == NULL)
1948 goto out;
1949
1950 skb_put(skb, skb_len);
1951
1952 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1953 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1954 tmp_len += tmp->len;
1955 }
1956
1957out:
1958 skb_queue_purge(&dev->resp_q);
1959
1960 return skb;
1961}
1962
c46ee386 1963static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
b1e666f5 1964 struct sk_buff *resp)
c46ee386
AAJ
1965{
1966 struct pn533_data_exchange_arg *arg = _arg;
b1e666f5
WR
1967 struct sk_buff *skb;
1968 int rc = 0;
1969 u8 status, ret, mi;
c46ee386 1970
9815c7cf 1971 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386 1972
b1e666f5
WR
1973 if (IS_ERR(resp)) {
1974 rc = PTR_ERR(resp);
1975 goto _error;
c46ee386
AAJ
1976 }
1977
b1e666f5
WR
1978 status = resp->data[0];
1979 ret = status & PN533_CMD_RET_MASK;
1980 mi = status & PN533_CMD_MI_MASK;
1981
1982 skb_pull(resp, sizeof(status));
c46ee386 1983
b1e666f5 1984 if (ret != PN533_CMD_RET_SUCCESS) {
9815c7cf 1985 nfc_err(dev->dev,
073a625f 1986 "Exchanging data failed (error 0x%x)\n", ret);
b1e666f5 1987 rc = -EIO;
c46ee386
AAJ
1988 goto error;
1989 }
1990
b1e666f5 1991 skb_queue_tail(&dev->resp_q, resp);
6ff73fd2 1992
b1e666f5
WR
1993 if (mi) {
1994 dev->cmd_complete_mi_arg = arg;
963a82e0
OG
1995 queue_work(dev->wq, &dev->mi_rx_work);
1996 return -EINPROGRESS;
1997 }
1998
1999 /* Prepare for the next round */
2000 if (skb_queue_len(&dev->fragment_skb) > 0) {
2001 dev->cmd_complete_dep_arg = arg;
2002 queue_work(dev->wq, &dev->mi_tx_work);
2003
6ff73fd2 2004 return -EINPROGRESS;
c46ee386
AAJ
2005 }
2006
6ff73fd2 2007 skb = pn533_build_response(dev);
5df848f3
JL
2008 if (!skb) {
2009 rc = -ENOMEM;
6ff73fd2 2010 goto error;
5df848f3 2011 }
c46ee386 2012
6ff73fd2 2013 arg->cb(arg->cb_context, skb, 0);
c46ee386
AAJ
2014 kfree(arg);
2015 return 0;
2016
2017error:
b1e666f5
WR
2018 dev_kfree_skb(resp);
2019_error:
6ff73fd2 2020 skb_queue_purge(&dev->resp_q);
b1e666f5 2021 arg->cb(arg->cb_context, NULL, rc);
c46ee386 2022 kfree(arg);
b1e666f5 2023 return rc;
c46ee386
AAJ
2024}
2025
9815c7cf
MT
2026/*
2027 * Receive an incoming pn533 frame. skb contains only header and payload.
2028 * If skb == NULL, it is a notification that the link below is dead.
2029 */
2030void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
2031{
b31d5103
MT
2032 if (!dev->cmd)
2033 goto sched_wq;
2034
9815c7cf
MT
2035 dev->cmd->status = status;
2036
b31d5103
MT
2037 if (status != 0) {
2038 dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
2039 goto sched_wq;
2040 }
2041
9815c7cf
MT
2042 if (skb == NULL) {
2043 pr_err("NULL Frame -> link is dead\n");
2044 goto sched_wq;
2045 }
2046
2047 if (pn533_rx_frame_is_ack(skb->data)) {
2048 dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2049 dev_kfree_skb(skb);
2050 return;
2051 }
2052
2053 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2054 dev->ops->rx_frame_size(skb->data), false);
2055
2056 if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2057 nfc_err(dev->dev, "Received an invalid frame\n");
2058 dev->cmd->status = -EIO;
2059 } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2060 nfc_err(dev->dev, "It it not the response to the last command\n");
2061 dev->cmd->status = -EIO;
2062 }
2063
2064 dev->cmd->resp = skb;
2065
2066sched_wq:
2067 queue_work(dev->wq, &dev->cmd_complete_work);
2068}
2069EXPORT_SYMBOL(pn533_recv_frame);
2070
963a82e0
OG
2071/* Split the Tx skb into small chunks */
2072static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2073{
2074 struct sk_buff *frag;
2075 int frag_size;
2076
2077 do {
2078 /* Remaining size */
2079 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2080 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2081 else
2082 frag_size = skb->len;
2083
2084 /* Allocate and reserve */
2085 frag = pn533_alloc_skb(dev, frag_size);
2086 if (!frag) {
2087 skb_queue_purge(&dev->fragment_skb);
2088 break;
2089 }
2090
22953f93
OG
2091 if (!dev->tgt_mode) {
2092 /* Reserve the TG/MI byte */
2093 skb_reserve(frag, 1);
2094
2095 /* MI + TG */
2096 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2097 *skb_push(frag, sizeof(u8)) =
2098 (PN533_CMD_MI_MASK | 1);
2099 else
2100 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2101 }
963a82e0
OG
2102
2103 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2104
2105 /* Reduce the size of incoming buffer */
2106 skb_pull(skb, frag_size);
2107
2108 /* Add this to skb_queue */
2109 skb_queue_tail(&dev->fragment_skb, frag);
2110
2111 } while (skb->len > 0);
2112
2113 dev_kfree_skb(skb);
2114
2115 return skb_queue_len(&dev->fragment_skb);
2116}
2117
be9ae4ce
SO
2118static int pn533_transceive(struct nfc_dev *nfc_dev,
2119 struct nfc_target *target, struct sk_buff *skb,
2120 data_exchange_cb_t cb, void *cb_context)
c46ee386
AAJ
2121{
2122 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
b1e666f5 2123 struct pn533_data_exchange_arg *arg = NULL;
c46ee386
AAJ
2124 int rc;
2125
9815c7cf 2126 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386
AAJ
2127
2128 if (!dev->tgt_active_prot) {
9815c7cf 2129 nfc_err(dev->dev,
073a625f 2130 "Can't exchange data if there is no active target\n");
c46ee386
AAJ
2131 rc = -EINVAL;
2132 goto error;
2133 }
2134
b1e666f5 2135 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
c46ee386
AAJ
2136 if (!arg) {
2137 rc = -ENOMEM;
b1e666f5 2138 goto error;
c46ee386
AAJ
2139 }
2140
c46ee386
AAJ
2141 arg->cb = cb;
2142 arg->cb_context = cb_context;
2143
b1e666f5
WR
2144 switch (dev->device_type) {
2145 case PN533_DEVICE_PASORI:
2146 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2147 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2148 skb,
2149 pn533_data_exchange_complete,
2150 arg);
2151
2152 break;
2153 }
2154 default:
963a82e0
OG
2155 /* jumbo frame ? */
2156 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2157 rc = pn533_fill_fragment_skbs(dev, skb);
2158 if (rc <= 0)
2159 goto error;
2160
2161 skb = skb_dequeue(&dev->fragment_skb);
2162 if (!skb) {
2163 rc = -EIO;
2164 goto error;
2165 }
2166 } else {
2167 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2168 }
b1e666f5
WR
2169
2170 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2171 skb, pn533_data_exchange_complete,
2172 arg);
2173
2174 break;
c46ee386
AAJ
2175 }
2176
b1e666f5
WR
2177 if (rc < 0) /* rc from send_async */
2178 goto error;
2179
c46ee386
AAJ
2180 return 0;
2181
c46ee386 2182error:
b1e666f5
WR
2183 kfree(arg);
2184 dev_kfree_skb(skb);
c46ee386
AAJ
2185 return rc;
2186}
2187
dadb06f2 2188static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
e4878823 2189 struct sk_buff *resp)
dadb06f2 2190{
e4878823 2191 u8 status;
5b412fd1 2192
9815c7cf 2193 dev_dbg(dev->dev, "%s\n", __func__);
dadb06f2 2194
e4878823
WR
2195 if (IS_ERR(resp))
2196 return PTR_ERR(resp);
5b412fd1 2197
e4878823 2198 status = resp->data[0];
dadb06f2 2199
93ad4202
OG
2200 /* Prepare for the next round */
2201 if (skb_queue_len(&dev->fragment_skb) > 0) {
2202 queue_work(dev->wq, &dev->mi_tm_tx_work);
2203 return -EINPROGRESS;
2204 }
e4878823 2205 dev_kfree_skb(resp);
dadb06f2 2206
e4878823 2207 if (status != 0) {
dadb06f2
SO
2208 nfc_tm_deactivated(dev->nfc_dev);
2209
51ad304c
SO
2210 dev->tgt_mode = 0;
2211
dadb06f2
SO
2212 return 0;
2213 }
2214
2215 queue_work(dev->wq, &dev->tg_work);
2216
2217 return 0;
2218}
2219
2220static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2221{
2222 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
dadb06f2
SO
2223 int rc;
2224
9815c7cf 2225 dev_dbg(dev->dev, "%s\n", __func__);
dadb06f2 2226
93ad4202 2227 /* let's split in multiple chunks if size's too big */
e4878823 2228 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
93ad4202
OG
2229 rc = pn533_fill_fragment_skbs(dev, skb);
2230 if (rc <= 0)
2231 goto error;
2232
2233 /* get the first skb */
2234 skb = skb_dequeue(&dev->fragment_skb);
2235 if (!skb) {
2236 rc = -EIO;
2237 goto error;
2238 }
2239
2240 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2241 pn533_tm_send_complete, NULL);
2242 } else {
2243 /* Send th skb */
2244 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2245 pn533_tm_send_complete, NULL);
dadb06f2
SO
2246 }
2247
93ad4202
OG
2248error:
2249 if (rc < 0) {
e4878823 2250 dev_kfree_skb(skb);
93ad4202
OG
2251 skb_queue_purge(&dev->fragment_skb);
2252 }
dadb06f2
SO
2253
2254 return rc;
2255}
2256
6ff73fd2
SO
2257static void pn533_wq_mi_recv(struct work_struct *work)
2258{
963a82e0 2259 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
b1e666f5 2260 struct sk_buff *skb;
6ff73fd2
SO
2261 int rc;
2262
9815c7cf 2263 dev_dbg(dev->dev, "%s\n", __func__);
6ff73fd2 2264
9e2d493e 2265 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
b1e666f5
WR
2266 if (!skb)
2267 goto error;
6ff73fd2 2268
b1e666f5
WR
2269 switch (dev->device_type) {
2270 case PN533_DEVICE_PASORI:
2271 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2272 rc = pn533_send_cmd_direct_async(dev,
2273 PN533_CMD_IN_COMM_THRU,
2274 skb,
2275 pn533_data_exchange_complete,
2276 dev->cmd_complete_mi_arg);
6ff73fd2 2277
b1e666f5
WR
2278 break;
2279 }
2280 default:
2281 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
6ff73fd2 2282
b1e666f5
WR
2283 rc = pn533_send_cmd_direct_async(dev,
2284 PN533_CMD_IN_DATA_EXCHANGE,
2285 skb,
2286 pn533_data_exchange_complete,
2287 dev->cmd_complete_mi_arg);
b1bb290a 2288
b1e666f5 2289 break;
6ff73fd2
SO
2290 }
2291
b1e666f5 2292 if (rc == 0) /* success */
6ff73fd2
SO
2293 return;
2294
9815c7cf 2295 nfc_err(dev->dev,
073a625f 2296 "Error %d when trying to perform data_exchange\n", rc);
6ff73fd2 2297
b1e666f5 2298 dev_kfree_skb(skb);
140ef7f6 2299 kfree(dev->cmd_complete_mi_arg);
6ff73fd2 2300
b1e666f5 2301error:
9815c7cf 2302 dev->phy_ops->send_ack(dev, GFP_KERNEL);
5d50b364 2303 queue_work(dev->wq, &dev->cmd_work);
6ff73fd2
SO
2304}
2305
963a82e0
OG
2306static void pn533_wq_mi_send(struct work_struct *work)
2307{
2308 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2309 struct sk_buff *skb;
2310 int rc;
2311
9815c7cf 2312 dev_dbg(dev->dev, "%s\n", __func__);
963a82e0
OG
2313
2314 /* Grab the first skb in the queue */
2315 skb = skb_dequeue(&dev->fragment_skb);
2316
2317 if (skb == NULL) { /* No more data */
2318 /* Reset the queue for future use */
2319 skb_queue_head_init(&dev->fragment_skb);
2320 goto error;
2321 }
2322
2323 switch (dev->device_type) {
2324 case PN533_DEVICE_PASORI:
2325 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2326 rc = -EIO;
2327 break;
2328 }
2329
2330 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2331 skb,
2332 pn533_data_exchange_complete,
2333 dev->cmd_complete_dep_arg);
2334
2335 break;
2336
2337 default:
2338 /* Still some fragments? */
9815c7cf
MT
2339 rc = pn533_send_cmd_direct_async(dev,
2340 PN533_CMD_IN_DATA_EXCHANGE,
963a82e0
OG
2341 skb,
2342 pn533_data_exchange_complete,
2343 dev->cmd_complete_dep_arg);
2344
2345 break;
2346 }
2347
2348 if (rc == 0) /* success */
2349 return;
2350
9815c7cf 2351 nfc_err(dev->dev,
073a625f 2352 "Error %d when trying to perform data_exchange\n", rc);
963a82e0
OG
2353
2354 dev_kfree_skb(skb);
2355 kfree(dev->cmd_complete_dep_arg);
2356
2357error:
9815c7cf 2358 dev->phy_ops->send_ack(dev, GFP_KERNEL);
963a82e0
OG
2359 queue_work(dev->wq, &dev->cmd_work);
2360}
2361
c46ee386
AAJ
2362static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2363 u8 cfgdata_len)
2364{
cb950d93
WR
2365 struct sk_buff *skb;
2366 struct sk_buff *resp;
cb950d93 2367 int skb_len;
c46ee386 2368
9815c7cf 2369 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386 2370
cb950d93 2371 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
c46ee386 2372
9e2d493e 2373 skb = pn533_alloc_skb(dev, skb_len);
cb950d93
WR
2374 if (!skb)
2375 return -ENOMEM;
c46ee386 2376
cb950d93
WR
2377 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2378 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
c46ee386 2379
cb950d93
WR
2380 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2381 if (IS_ERR(resp))
2382 return PTR_ERR(resp);
c46ee386 2383
cb950d93
WR
2384 dev_kfree_skb(resp);
2385 return 0;
2386}
2387
2388static int pn533_get_firmware_version(struct pn533 *dev,
2389 struct pn533_fw_version *fv)
2390{
2391 struct sk_buff *skb;
2392 struct sk_buff *resp;
2393
9e2d493e 2394 skb = pn533_alloc_skb(dev, 0);
cb950d93
WR
2395 if (!skb)
2396 return -ENOMEM;
2397
2398 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2399 if (IS_ERR(resp))
2400 return PTR_ERR(resp);
2401
2402 fv->ic = resp->data[0];
2403 fv->ver = resp->data[1];
2404 fv->rev = resp->data[2];
2405 fv->support = resp->data[3];
2406
2407 dev_kfree_skb(resp);
2408 return 0;
c46ee386
AAJ
2409}
2410
f75c2913 2411static int pn533_pasori_fw_reset(struct pn533 *dev)
5c7b0531 2412{
cb950d93
WR
2413 struct sk_buff *skb;
2414 struct sk_buff *resp;
5c7b0531 2415
9815c7cf 2416 dev_dbg(dev->dev, "%s\n", __func__);
5c7b0531 2417
9e2d493e 2418 skb = pn533_alloc_skb(dev, sizeof(u8));
cb950d93
WR
2419 if (!skb)
2420 return -ENOMEM;
5c7b0531 2421
cb950d93 2422 *skb_put(skb, sizeof(u8)) = 0x1;
5c7b0531 2423
cb950d93
WR
2424 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2425 if (IS_ERR(resp))
2426 return PTR_ERR(resp);
5c7b0531 2427
cb950d93 2428 dev_kfree_skb(resp);
5c7b0531 2429
94c5c156 2430 return 0;
5c7b0531
SO
2431}
2432
60d9edd5
SO
2433static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2434{
2435 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2436 u8 rf_field = !!rf;
2437 int rc;
2438
3a8eab39
SO
2439 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2440
60d9edd5
SO
2441 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2442 (u8 *)&rf_field, 1);
2443 if (rc) {
9815c7cf 2444 nfc_err(dev->dev, "Error on setting RF field\n");
60d9edd5
SO
2445 return rc;
2446 }
2447
2448 return rc;
2449}
2450
dd7bedcd
MT
2451static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2452{
2453 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2454 struct sk_buff *skb;
2455 struct sk_buff *resp;
2456
2457 skb = pn533_alloc_skb(dev, 1);
2458 if (!skb)
2459 return -ENOMEM;
2460
2461 *skb_put(skb, 1) = 0x01;
2462
2463 resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2464 if (IS_ERR(resp))
2465 return PTR_ERR(resp);
2466
2467 dev_kfree_skb(resp);
2468 return 0;
2469}
2470
e44666b9 2471static int pn533_dev_up(struct nfc_dev *nfc_dev)
60d9edd5 2472{
dd7bedcd
MT
2473 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2474
2475 if (dev->device_type == PN533_DEVICE_PN532) {
2476 int rc = pn532_sam_configuration(nfc_dev);
2477
2478 if (rc)
2479 return rc;
2480 }
2481
60d9edd5
SO
2482 return pn533_rf_field(nfc_dev, 1);
2483}
2484
e44666b9 2485static int pn533_dev_down(struct nfc_dev *nfc_dev)
60d9edd5
SO
2486{
2487 return pn533_rf_field(nfc_dev, 0);
2488}
2489
5c7b0531 2490static struct nfc_ops pn533_nfc_ops = {
60d9edd5
SO
2491 .dev_up = pn533_dev_up,
2492 .dev_down = pn533_dev_down,
361f3cb7
SO
2493 .dep_link_up = pn533_dep_link_up,
2494 .dep_link_down = pn533_dep_link_down,
c46ee386
AAJ
2495 .start_poll = pn533_start_poll,
2496 .stop_poll = pn533_stop_poll,
2497 .activate_target = pn533_activate_target,
2498 .deactivate_target = pn533_deactivate_target,
be9ae4ce 2499 .im_transceive = pn533_transceive,
dadb06f2 2500 .tm_send = pn533_tm_send,
c46ee386
AAJ
2501};
2502
5c7b0531
SO
2503static int pn533_setup(struct pn533 *dev)
2504{
2505 struct pn533_config_max_retries max_retries;
2506 struct pn533_config_timing timing;
2507 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2508 int rc;
2509
2510 switch (dev->device_type) {
2511 case PN533_DEVICE_STD:
5c7b0531 2512 case PN533_DEVICE_PASORI:
53cf4839 2513 case PN533_DEVICE_ACR122U:
dd7bedcd 2514 case PN533_DEVICE_PN532:
5c7b0531
SO
2515 max_retries.mx_rty_atr = 0x2;
2516 max_retries.mx_rty_psl = 0x1;
2517 max_retries.mx_rty_passive_act =
2518 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2519
2520 timing.rfu = PN533_CONFIG_TIMING_102;
2521 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2522 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2523
2524 break;
2525
2526 default:
9815c7cf 2527 nfc_err(dev->dev, "Unknown device type %d\n",
073a625f 2528 dev->device_type);
5c7b0531
SO
2529 return -EINVAL;
2530 }
2531
2532 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2533 (u8 *)&max_retries, sizeof(max_retries));
2534 if (rc) {
9815c7cf 2535 nfc_err(dev->dev,
073a625f 2536 "Error on setting MAX_RETRIES config\n");
5c7b0531
SO
2537 return rc;
2538 }
2539
2540
2541 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2542 (u8 *)&timing, sizeof(timing));
2543 if (rc) {
9815c7cf 2544 nfc_err(dev->dev, "Error on setting RF timings\n");
5c7b0531
SO
2545 return rc;
2546 }
2547
2548 switch (dev->device_type) {
2549 case PN533_DEVICE_STD:
dd7bedcd 2550 case PN533_DEVICE_PN532:
5c7b0531
SO
2551 break;
2552
2553 case PN533_DEVICE_PASORI:
f75c2913 2554 pn533_pasori_fw_reset(dev);
5c7b0531
SO
2555
2556 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2557 pasori_cfg, 3);
2558 if (rc) {
9815c7cf 2559 nfc_err(dev->dev,
073a625f 2560 "Error while settings PASORI config\n");
5c7b0531
SO
2561 return rc;
2562 }
2563
f75c2913 2564 pn533_pasori_fw_reset(dev);
5c7b0531
SO
2565
2566 break;
2567 }
2568
2569 return 0;
2570}
2571
32ecc75d
AR
2572int pn533_finalize_setup(struct pn533 *dev)
2573{
2574
2575 struct pn533_fw_version fw_ver;
2576 int rc;
2577
2578 memset(&fw_ver, 0, sizeof(fw_ver));
2579
2580 rc = pn533_get_firmware_version(dev, &fw_ver);
2581 if (rc) {
2582 nfc_err(dev->dev, "Unable to get FW version\n");
2583 return rc;
2584 }
2585
2586 nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
2587 fw_ver.ic, fw_ver.ver, fw_ver.rev);
2588
2589 rc = pn533_setup(dev);
2590 if (rc)
2591 return rc;
2592
2593 return 0;
2594}
2595EXPORT_SYMBOL_GPL(pn533_finalize_setup);
2596
9815c7cf
MT
2597struct pn533 *pn533_register_device(u32 device_type,
2598 u32 protocols,
2599 enum pn533_protocol_type protocol_type,
2600 void *phy,
2601 struct pn533_phy_ops *phy_ops,
2602 struct pn533_frame_ops *fops,
b16931b1
MT
2603 struct device *dev,
2604 struct device *parent)
c46ee386 2605{
9815c7cf 2606 struct pn533 *priv;
c46ee386 2607 int rc = -ENOMEM;
c46ee386 2608
9815c7cf
MT
2609 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2610 if (!priv)
2611 return ERR_PTR(-ENOMEM);
c46ee386 2612
9815c7cf
MT
2613 priv->phy = phy;
2614 priv->phy_ops = phy_ops;
2615 priv->dev = dev;
2616 if (fops != NULL)
2617 priv->ops = fops;
2618 else
2619 priv->ops = &pn533_std_frame_ops;
2620
2621 priv->protocol_type = protocol_type;
2622 priv->device_type = device_type;
2623
2624 mutex_init(&priv->cmd_lock);
2625
2626 INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2627 INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2628 INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2629 INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2630 INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2631 INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2632 INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2633 INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2634 INIT_WORK(&priv->rf_work, pn533_wq_rf);
2635 priv->wq = alloc_ordered_workqueue("pn533", 0);
2636 if (priv->wq == NULL)
4849f85e 2637 goto error;
c46ee386 2638
9815c7cf
MT
2639 init_timer(&priv->listen_timer);
2640 priv->listen_timer.data = (unsigned long) priv;
2641 priv->listen_timer.function = pn533_listen_mode_timer;
5c7b0531 2642
9815c7cf
MT
2643 skb_queue_head_init(&priv->resp_q);
2644 skb_queue_head_init(&priv->fragment_skb);
53cf4839 2645
9815c7cf 2646 INIT_LIST_HEAD(&priv->cmd_queue);
c46ee386 2647
9815c7cf
MT
2648 priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2649 priv->ops->tx_header_len +
e8753043 2650 PN533_CMD_DATAEXCH_HEAD_LEN,
9815c7cf
MT
2651 priv->ops->tx_tail_len);
2652 if (!priv->nfc_dev) {
4674d0fe 2653 rc = -ENOMEM;
4849f85e 2654 goto destroy_wq;
4674d0fe 2655 }
c46ee386 2656
b16931b1 2657 nfc_set_parent_dev(priv->nfc_dev, parent);
9815c7cf 2658 nfc_set_drvdata(priv->nfc_dev, priv);
c46ee386 2659
9815c7cf 2660 rc = nfc_register_device(priv->nfc_dev);
c46ee386
AAJ
2661 if (rc)
2662 goto free_nfc_dev;
2663
9815c7cf 2664 return priv;
c46ee386
AAJ
2665
2666free_nfc_dev:
9815c7cf 2667 nfc_free_device(priv->nfc_dev);
9f2f8ba1 2668
4849f85e 2669destroy_wq:
9815c7cf 2670 destroy_workqueue(priv->wq);
c46ee386 2671error:
9815c7cf
MT
2672 kfree(priv);
2673 return ERR_PTR(rc);
c46ee386 2674}
9815c7cf 2675EXPORT_SYMBOL_GPL(pn533_register_device);
c46ee386 2676
9815c7cf 2677void pn533_unregister_device(struct pn533 *priv)
c46ee386 2678{
5d50b364 2679 struct pn533_cmd *cmd, *n;
c46ee386 2680
9815c7cf
MT
2681 nfc_unregister_device(priv->nfc_dev);
2682 nfc_free_device(priv->nfc_dev);
c46ee386 2683
9815c7cf
MT
2684 flush_delayed_work(&priv->poll_work);
2685 destroy_workqueue(priv->wq);
c46ee386 2686
9815c7cf 2687 skb_queue_purge(&priv->resp_q);
c46ee386 2688
9815c7cf 2689 del_timer(&priv->listen_timer);
c46ee386 2690
9815c7cf 2691 list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
5d50b364
SO
2692 list_del(&cmd->queue);
2693 kfree(cmd);
2694 }
2695
9815c7cf 2696 kfree(priv);
c46ee386 2697}
9815c7cf 2698EXPORT_SYMBOL_GPL(pn533_unregister_device);
c46ee386 2699
c46ee386 2700
e70b96e9
WR
2701MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2702MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2703MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
9815c7cf 2704MODULE_DESCRIPTION("PN533 driver ver " VERSION);
c46ee386
AAJ
2705MODULE_VERSION(VERSION);
2706MODULE_LICENSE("GPL");