NFC: pn533: reset poll modulation list before calling targets_found
[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;
f87bc9fb 386 int status = cmd->status;
aada17ac 387
4231604b
WR
388 struct sk_buff *req = cmd->req;
389 struct sk_buff *resp = cmd->resp;
aada17ac 390
aada17ac
WR
391 int rc;
392
393 dev_kfree_skb(req);
394
0c33d262 395 if (status < 0) {
4231604b
WR
396 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
397 ERR_PTR(status));
aada17ac 398 dev_kfree_skb(resp);
2c206fb7 399 goto done;
aada17ac
WR
400 }
401
9e2d493e
WR
402 skb_pull(resp, dev->ops->rx_header_len);
403 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
aada17ac 404
4231604b 405 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
aada17ac 406
2c206fb7 407done:
4231604b 408 kfree(cmd);
2c206fb7 409 dev->cmd = NULL;
aada17ac
WR
410 return rc;
411}
412
413static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
9815c7cf 414 struct sk_buff *req,
aada17ac
WR
415 pn533_send_async_complete_t complete_cb,
416 void *complete_cb_context)
417{
418 struct pn533_cmd *cmd;
aada17ac
WR
419 int rc = 0;
420
9815c7cf 421 dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
aada17ac 422
4231604b
WR
423 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
424 if (!cmd)
aada17ac
WR
425 return -ENOMEM;
426
4b2a9532 427 cmd->code = cmd_code;
4231604b 428 cmd->req = req;
4231604b
WR
429 cmd->complete_cb = complete_cb;
430 cmd->complete_cb_context = complete_cb_context;
aada17ac 431
9e2d493e 432 pn533_build_cmd_frame(dev, cmd_code, req);
aada17ac
WR
433
434 mutex_lock(&dev->cmd_lock);
435
436 if (!dev->cmd_pending) {
9815c7cf 437 rc = dev->phy_ops->send_frame(dev, req);
aada17ac
WR
438 if (rc)
439 goto error;
440
441 dev->cmd_pending = 1;
2c206fb7 442 dev->cmd = cmd;
aada17ac
WR
443 goto unlock;
444 }
445
9815c7cf 446 dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
b4834839 447 __func__, cmd_code);
aada17ac 448
aada17ac 449 INIT_LIST_HEAD(&cmd->queue);
aada17ac
WR
450 list_add_tail(&cmd->queue, &dev->cmd_queue);
451
452 goto unlock;
453
454error:
4231604b 455 kfree(cmd);
aada17ac
WR
456unlock:
457 mutex_unlock(&dev->cmd_lock);
458 return rc;
15461aeb
WR
459}
460
461static 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{
15461aeb 466 int rc;
15461aeb 467
9815c7cf 468 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
15461aeb 469 complete_cb_context);
15461aeb
WR
470
471 return rc;
aada17ac
WR
472}
473
474static 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{
aada17ac
WR
479 int rc;
480
9815c7cf 481 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
9e2d493e 482 complete_cb_context);
aada17ac
WR
483
484 return rc;
485}
486
b1e666f5
WR
487/*
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{
4231604b 500 struct pn533_cmd *cmd;
b1e666f5 501 int rc;
b1e666f5 502
4231604b 503 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
9815c7cf 504 if (!cmd)
b1e666f5 505 return -ENOMEM;
b1e666f5 506
4b2a9532 507 cmd->code = cmd_code;
4231604b 508 cmd->req = req;
4231604b
WR
509 cmd->complete_cb = complete_cb;
510 cmd->complete_cb_context = complete_cb_context;
b1e666f5 511
9e2d493e 512 pn533_build_cmd_frame(dev, cmd_code, req);
b1e666f5 513
9815c7cf
MT
514 rc = dev->phy_ops->send_frame(dev, req);
515 if (rc < 0)
4231604b 516 kfree(cmd);
9815c7cf 517 else
2c206fb7 518 dev->cmd = cmd;
b1e666f5
WR
519
520 return rc;
521}
522
c79490e1
WR
523static 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
5d50b364
SO
533static 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;
0ce1fbdd 537 int rc;
5d50b364
SO
538
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
60ad07ab
SJ
549 list_del(&cmd->queue);
550
5d50b364
SO
551 mutex_unlock(&dev->cmd_lock);
552
9815c7cf 553 rc = dev->phy_ops->send_frame(dev, cmd->req);
0ce1fbdd
WR
554 if (rc < 0) {
555 dev_kfree_skb(cmd->req);
4231604b 556 kfree(cmd);
2c206fb7 557 return;
0ce1fbdd 558 }
2c206fb7
WR
559
560 dev->cmd = cmd;
5d50b364
SO
561}
562
c46ee386 563struct pn533_sync_cmd_response {
94c5c156 564 struct sk_buff *resp;
c46ee386
AAJ
565 struct completion done;
566};
567
94c5c156
WR
568static 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
94c5c156
WR
573 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
9815c7cf 587 * as it's been already freed at the beginning of RX path by
94c5c156
WR
588 * 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 *
9815c7cf 595 */
94c5c156
WR
596static 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
94c5c156
WR
602 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
9e2d493e 616static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
d22b2db6
WR
617{
618 struct sk_buff *skb;
619
9e2d493e 620 skb = alloc_skb(dev->ops->tx_header_len +
d22b2db6 621 size +
9e2d493e 622 dev->ops->tx_tail_len, GFP_KERNEL);
d22b2db6
WR
623
624 if (skb)
9e2d493e 625 skb_reserve(skb, dev->ops->tx_header_len);
d22b2db6
WR
626
627 return skb;
628}
629
c46ee386
AAJ
630struct 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
9815c7cf
MT
662 /*
663 * The length check of nfcid[] and ats[] are not being performed because
664 * the values are not being used
665 */
c46ee386
AAJ
666
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 &&
5d467742
WR
672 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))
c46ee386
AAJ
675 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
37cf4fc6 689 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
c46ee386
AAJ
690
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;
c3b1e1e8
SO
712 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
713 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
c46ee386
AAJ
714
715 return 0;
716}
717
718struct pn533_target_felica {
719 u8 pol_res;
720 u8 opcode;
322bce95 721 u8 nfcid2[NFC_NFCID2_MAXSIZE];
c46ee386
AAJ
722 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
37cf4fc6 747 tgt_felica = (struct pn533_target_felica *)tgt_data;
c46ee386
AAJ
748
749 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
750 return -EPROTO;
751
5d467742
WR
752 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
753 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
c46ee386
AAJ
754 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
755 else
756 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
757
7975754f
SO
758 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
759 nfc_tgt->sensf_res_len = 9;
760
322bce95
SO
761 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
762 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
763
c46ee386
AAJ
764 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 &&
5d467742
WR
786 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))
c46ee386
AAJ
789 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
37cf4fc6 799 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
c46ee386
AAJ
800
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);
d8dc1072
SO
806 nfc_tgt->nfcid1_len = 4;
807 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
c46ee386
AAJ
808
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
37cf4fc6 858 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
c46ee386
AAJ
859
860 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
861 return -EPROTO;
862
01d719a2 863 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
c46ee386
AAJ
864
865 return 0;
866}
867
c952f915 868static void pn533_poll_reset_mod_list(struct pn533 *dev);
b5193e5d
WR
869static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
870 int tgdata_len)
c46ee386 871{
c46ee386
AAJ
872 struct nfc_target nfc_tgt;
873 int rc;
874
9815c7cf 875 dev_dbg(dev->dev, "%s: modulation=%d\n",
b4834839 876 __func__, dev->poll_mod_curr);
c46ee386 877
b5193e5d 878 if (tg != 1)
c46ee386
AAJ
879 return -EPROTO;
880
98b3ac1b
SO
881 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
882
c46ee386
AAJ
883 switch (dev->poll_mod_curr) {
884 case PN533_POLL_MOD_106KBPS_A:
b5193e5d 885 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
886 break;
887 case PN533_POLL_MOD_212KBPS_FELICA:
888 case PN533_POLL_MOD_424KBPS_FELICA:
b5193e5d 889 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
890 break;
891 case PN533_POLL_MOD_106KBPS_JEWEL:
b5193e5d 892 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
893 break;
894 case PN533_POLL_MOD_847KBPS_B:
b5193e5d 895 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
c46ee386
AAJ
896 break;
897 default:
9815c7cf 898 nfc_err(dev->dev,
073a625f 899 "Unknown current poll modulation\n");
c46ee386
AAJ
900 return -EPROTO;
901 }
902
903 if (rc)
904 return rc;
905
906 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
9815c7cf 907 dev_dbg(dev->dev,
b4834839 908 "The Tg found doesn't have the desired protocol\n");
c46ee386
AAJ
909 return -EAGAIN;
910 }
911
9815c7cf 912 dev_dbg(dev->dev,
b4834839
JP
913 "Target found - supported protocols: 0x%x\n",
914 nfc_tgt.supported_protocols);
c46ee386
AAJ
915
916 dev->tgt_available_prots = nfc_tgt.supported_protocols;
917
c952f915 918 pn533_poll_reset_mod_list(dev);
c46ee386
AAJ
919 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
920
921 return 0;
922}
923
6fbbdc16
SO
924static 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
c46ee386
AAJ
929static 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] =
37cf4fc6 937 (struct pn533_poll_modulations *)&poll_mod[mod_index];
c46ee386
AAJ
938 dev->poll_mod_count++;
939}
940
6fbbdc16
SO
941static void pn533_poll_create_mod_list(struct pn533 *dev,
942 u32 im_protocols, u32 tm_protocols)
c46ee386
AAJ
943{
944 pn533_poll_reset_mod_list(dev);
945
b08e8603
WR
946 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
947 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
948 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
c46ee386
AAJ
949 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
950
b08e8603
WR
951 if (im_protocols & NFC_PROTO_FELICA_MASK ||
952 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
c46ee386
AAJ
953 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
954 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
955 }
956
6fbbdc16 957 if (im_protocols & NFC_PROTO_JEWEL_MASK)
c46ee386
AAJ
958 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
959
01d719a2 960 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
c46ee386 961 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
c46ee386 962
6fbbdc16
SO
963 if (tm_protocols)
964 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
c46ee386
AAJ
965}
966
b5193e5d 967static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
c46ee386 968{
b5193e5d
WR
969 u8 nbtg, tg, *tgdata;
970 int rc, tgdata_len;
c46ee386 971
673088fb 972 /* Toggle the DEP polling */
e997ebbe
MT
973 if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
974 dev->poll_dep = 1;
673088fb 975
b5193e5d
WR
976 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);
c46ee386
AAJ
983
984 /* We must stop the poll after a valid target found */
c952f915 985 if (rc == 0)
6fbbdc16 986 return 0;
c46ee386
AAJ
987 }
988
6fbbdc16 989 return -EAGAIN;
c46ee386
AAJ
990}
991
9e2d493e 992static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
ad3823ce 993{
b5193e5d
WR
994 struct sk_buff *skb;
995 u8 *felica, *nfcid3, *gb;
996
9e2d493e
WR
997 u8 *gbytes = dev->gb;
998 size_t gbytes_len = dev->gb_len;
999
51d9e803
SO
1000 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 */
b5193e5d 1004
51d9e803
SO
1005 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1006 0x0, 0x0, 0x0,
1007 0x40}; /* SEL_RES for DEP */
ad3823ce 1008
9815c7cf
MT
1009 unsigned int skb_len = 36 + /*
1010 * mode (1), mifare (6),
1011 * felica (18), nfcid3 (10), gb_len (1)
1012 */
b5193e5d
WR
1013 gbytes_len +
1014 1; /* len Tk*/
ad3823ce 1015
9e2d493e 1016 skb = pn533_alloc_skb(dev, skb_len);
b5193e5d
WR
1017 if (!skb)
1018 return NULL;
ad3823ce
SO
1019
1020 /* DEP support only */
52f2eaee 1021 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
b5193e5d
WR
1022
1023 /* MIFARE params */
1024 memcpy(skb_put(skb, 6), mifare_params, 6);
51d9e803
SO
1025
1026 /* Felica params */
b5193e5d
WR
1027 felica = skb_put(skb, 18);
1028 memcpy(felica, felica_params, 18);
1029 get_random_bytes(felica + 2, 6);
51d9e803
SO
1030
1031 /* NFCID3 */
b5193e5d
WR
1032 nfcid3 = skb_put(skb, 10);
1033 memset(nfcid3, 0, 10);
1034 memcpy(nfcid3, felica, 8);
51d9e803
SO
1035
1036 /* General bytes */
b5193e5d 1037 *skb_put(skb, 1) = gbytes_len;
51d9e803 1038
b5193e5d
WR
1039 gb = skb_put(skb, gbytes_len);
1040 memcpy(gb, gbytes, gbytes_len);
ad3823ce 1041
b5193e5d
WR
1042 /* Len Tk */
1043 *skb_put(skb, 1) = 0;
51d9e803 1044
b5193e5d 1045 return skb;
ad3823ce
SO
1046}
1047
3c13b244
OG
1048static void pn533_wq_tm_mi_recv(struct work_struct *work);
1049static struct sk_buff *pn533_build_response(struct pn533 *dev);
1050
103b34cf 1051static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
e4878823 1052 struct sk_buff *resp)
103b34cf 1053{
3c13b244
OG
1054 struct sk_buff *skb;
1055 u8 status, ret, mi;
1056 int rc;
103b34cf 1057
9815c7cf 1058 dev_dbg(dev->dev, "%s\n", __func__);
103b34cf 1059
3c13b244
OG
1060 if (IS_ERR(resp)) {
1061 skb_queue_purge(&dev->resp_q);
e4878823 1062 return PTR_ERR(resp);
3c13b244 1063 }
103b34cf 1064
e4878823 1065 status = resp->data[0];
3c13b244
OG
1066
1067 ret = status & PN533_CMD_RET_MASK;
1068 mi = status & PN533_CMD_MI_MASK;
1069
e4878823 1070 skb_pull(resp, sizeof(status));
103b34cf 1071
3c13b244
OG
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;
103b34cf
SO
1088 }
1089
3c13b244
OG
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
9815c7cf 1107 dev_dbg(dev->dev, "%s\n", __func__);
3c13b244
OG
1108
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);
103b34cf
SO
1121}
1122
93ad4202
OG
1123static 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
9815c7cf 1131 dev_dbg(dev->dev, "%s\n", __func__);
93ad4202
OG
1132
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
9815c7cf 1153 dev_err(dev->dev,
93ad4202
OG
1154 "Error %d when trying to perform set meta data_exchange", rc);
1155
1156 dev_kfree_skb(skb);
1157
1158error:
9815c7cf 1159 dev->phy_ops->send_ack(dev, GFP_KERNEL);
93ad4202
OG
1160 queue_work(dev->wq, &dev->cmd_work);
1161}
1162
103b34cf
SO
1163static void pn533_wq_tg_get_data(struct work_struct *work)
1164{
1165 struct pn533 *dev = container_of(work, struct pn533, tg_work);
e4878823
WR
1166 struct sk_buff *skb;
1167 int rc;
103b34cf 1168
9815c7cf 1169 dev_dbg(dev->dev, "%s\n", __func__);
103b34cf 1170
9e2d493e 1171 skb = pn533_alloc_skb(dev, 0);
e4878823 1172 if (!skb)
103b34cf
SO
1173 return;
1174
e4878823
WR
1175 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1176 pn533_tm_get_data_complete, NULL);
103b34cf 1177
e4878823
WR
1178 if (rc < 0)
1179 dev_kfree_skb(skb);
103b34cf
SO
1180}
1181
fc40a8c1 1182#define ATR_REQ_GB_OFFSET 17
b5193e5d 1183static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
fe7c5800 1184{
b5193e5d 1185 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
fc40a8c1 1186 size_t gb_len;
103b34cf 1187 int rc;
ad3823ce 1188
9815c7cf 1189 dev_dbg(dev->dev, "%s\n", __func__);
ad3823ce 1190
b5193e5d 1191 if (resp->len < ATR_REQ_GB_OFFSET + 1)
fc40a8c1
SO
1192 return -EINVAL;
1193
b5193e5d
WR
1194 mode = resp->data[0];
1195 cmd = &resp->data[1];
ad3823ce 1196
9815c7cf 1197 dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
b4834839 1198 mode, resp->len);
ad3823ce 1199
b5193e5d
WR
1200 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1201 PN533_INIT_TARGET_RESP_ACTIVE)
fc40a8c1
SO
1202 comm_mode = NFC_COMM_ACTIVE;
1203
b5193e5d 1204 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
fc40a8c1
SO
1205 return -EOPNOTSUPP;
1206
b5193e5d
WR
1207 gb = cmd + ATR_REQ_GB_OFFSET;
1208 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
fc40a8c1 1209
103b34cf
SO
1210 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1211 comm_mode, gb, gb_len);
1212 if (rc < 0) {
9815c7cf 1213 nfc_err(dev->dev,
073a625f 1214 "Error when signaling target activation\n");
103b34cf
SO
1215 return rc;
1216 }
1217
51ad304c 1218 dev->tgt_mode = 1;
103b34cf
SO
1219 queue_work(dev->wq, &dev->tg_work);
1220
1221 return 0;
fe7c5800
SO
1222}
1223
6fbbdc16 1224static void pn533_listen_mode_timer(unsigned long data)
ad3823ce 1225{
37cf4fc6 1226 struct pn533 *dev = (struct pn533 *)data;
6fbbdc16 1227
9815c7cf 1228 dev_dbg(dev->dev, "Listen mode timeout\n");
6fbbdc16 1229
6fbbdc16
SO
1230 dev->cancel_listen = 1;
1231
6fbbdc16
SO
1232 pn533_poll_next_mod(dev);
1233
46f793b0
SO
1234 queue_delayed_work(dev->wq, &dev->poll_work,
1235 msecs_to_jiffies(PN533_POLL_INTERVAL));
6fbbdc16
SO
1236}
1237
17e9d9d4
SO
1238static int pn533_rf_complete(struct pn533 *dev, void *arg,
1239 struct sk_buff *resp)
1240{
1241 int rc = 0;
1242
9815c7cf 1243 dev_dbg(dev->dev, "%s\n", __func__);
17e9d9d4
SO
1244
1245 if (IS_ERR(resp)) {
1246 rc = PTR_ERR(resp);
1247
9815c7cf 1248 nfc_err(dev->dev, "RF setting error %d\n", rc);
17e9d9d4
SO
1249
1250 return rc;
1251 }
1252
46f793b0
SO
1253 queue_delayed_work(dev->wq, &dev->poll_work,
1254 msecs_to_jiffies(PN533_POLL_INTERVAL));
17e9d9d4
SO
1255
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
9815c7cf 1266 dev_dbg(dev->dev, "%s\n", __func__);
17e9d9d4
SO
1267
1268 skb = pn533_alloc_skb(dev, 2);
1269 if (!skb)
1270 return;
1271
1272 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
3a8eab39 1273 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
17e9d9d4
SO
1274
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);
9815c7cf 1279 nfc_err(dev->dev, "RF setting error %d\n", rc);
17e9d9d4 1280 }
17e9d9d4
SO
1281}
1282
673088fb
SO
1283static 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
9815c7cf 1305 dev_dbg(dev->dev, "Creating new target");
673088fb
SO
1306
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
9815c7cf 1343 dev_dbg(dev->dev, "%s", __func__);
673088fb
SO
1344
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
6fbbdc16 1394static int pn533_poll_complete(struct pn533 *dev, void *arg,
b5193e5d 1395 struct sk_buff *resp)
6fbbdc16
SO
1396{
1397 struct pn533_poll_modulations *cur_mod;
ad3823ce
SO
1398 int rc;
1399
9815c7cf 1400 dev_dbg(dev->dev, "%s\n", __func__);
ad3823ce 1401
b5193e5d
WR
1402 if (IS_ERR(resp)) {
1403 rc = PTR_ERR(resp);
1404
9815c7cf 1405 nfc_err(dev->dev, "%s Poll complete error %d\n",
073a625f 1406 __func__, rc);
b5193e5d
WR
1407
1408 if (rc == -ENOENT) {
1409 if (dev->poll_mod_count != 0)
1410 return rc;
9815c7cf 1411 goto stop_poll;
b5193e5d 1412 } else if (rc < 0) {
9815c7cf 1413 nfc_err(dev->dev,
073a625f 1414 "Error %d when running poll\n", rc);
b5193e5d
WR
1415 goto stop_poll;
1416 }
6fbbdc16 1417 }
ad3823ce 1418
6fbbdc16
SO
1419 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1420
b5193e5d 1421 if (cur_mod->len == 0) { /* Target mode */
6fbbdc16 1422 del_timer(&dev->listen_timer);
b5193e5d
WR
1423 rc = pn533_init_target_complete(dev, resp);
1424 goto done;
6fbbdc16
SO
1425 }
1426
b5193e5d
WR
1427 /* Initiator mode */
1428 rc = pn533_start_poll_complete(dev, resp);
1429 if (!rc)
1430 goto done;
6fbbdc16 1431
95cb9e10 1432 if (!dev->poll_mod_count) {
9815c7cf 1433 dev_dbg(dev->dev, "Polling has been stopped\n");
95cb9e10
WR
1434 goto done;
1435 }
1436
b5193e5d 1437 pn533_poll_next_mod(dev);
17e9d9d4
SO
1438 /* Not target found, turn radio off */
1439 queue_work(dev->wq, &dev->rf_work);
6fbbdc16 1440
b5193e5d
WR
1441done:
1442 dev_kfree_skb(resp);
1443 return rc;
6fbbdc16
SO
1444
1445stop_poll:
9815c7cf 1446 nfc_err(dev->dev, "Polling operation has been stopped\n");
b5193e5d 1447
6fbbdc16
SO
1448 pn533_poll_reset_mod_list(dev);
1449 dev->poll_protocols = 0;
b5193e5d 1450 return rc;
ad3823ce
SO
1451}
1452
9e2d493e
WR
1453static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1454 struct pn533_poll_modulations *mod)
c46ee386 1455{
b5193e5d 1456 struct sk_buff *skb;
c46ee386 1457
9e2d493e 1458 skb = pn533_alloc_skb(dev, mod->len);
b5193e5d
WR
1459 if (!skb)
1460 return NULL;
c46ee386 1461
b5193e5d 1462 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
c46ee386 1463
b5193e5d 1464 return skb;
6fbbdc16 1465}
c46ee386 1466
6fbbdc16
SO
1467static int pn533_send_poll_frame(struct pn533 *dev)
1468{
b5193e5d
WR
1469 struct pn533_poll_modulations *mod;
1470 struct sk_buff *skb;
6fbbdc16 1471 int rc;
b5193e5d 1472 u8 cmd_code;
c46ee386 1473
b5193e5d 1474 mod = dev->poll_mod_active[dev->poll_mod_curr];
c46ee386 1475
9815c7cf 1476 dev_dbg(dev->dev, "%s mod len %d\n",
b4834839 1477 __func__, mod->len);
c46ee386 1478
e997ebbe 1479 if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) {
673088fb
SO
1480 dev->poll_dep = 0;
1481 return pn533_poll_dep(dev->nfc_dev);
1482 }
1483
b5193e5d
WR
1484 if (mod->len == 0) { /* Listen mode */
1485 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
9e2d493e 1486 skb = pn533_alloc_poll_tg_frame(dev);
b5193e5d
WR
1487 } else { /* Polling mode */
1488 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
9e2d493e 1489 skb = pn533_alloc_poll_in_frame(dev, mod);
b5193e5d
WR
1490 }
1491
1492 if (!skb) {
9815c7cf 1493 nfc_err(dev->dev, "Failed to allocate skb\n");
b5193e5d
WR
1494 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);
9815c7cf 1501 nfc_err(dev->dev, "Polling loop error %d\n", rc);
b5193e5d 1502 }
c46ee386 1503
6fbbdc16
SO
1504 return rc;
1505}
1506
1507static void pn533_wq_poll(struct work_struct *work)
1508{
46f793b0 1509 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
6fbbdc16
SO
1510 struct pn533_poll_modulations *cur_mod;
1511 int rc;
1512
1513 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1514
9815c7cf 1515 dev_dbg(dev->dev,
b4834839
JP
1516 "%s cancel_listen %d modulation len %d\n",
1517 __func__, dev->cancel_listen, cur_mod->len);
6fbbdc16
SO
1518
1519 if (dev->cancel_listen == 1) {
1520 dev->cancel_listen = 0;
9815c7cf 1521 dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
c46ee386
AAJ
1522 }
1523
6fbbdc16
SO
1524 rc = pn533_send_poll_frame(dev);
1525 if (rc)
1526 return;
c46ee386 1527
6fbbdc16
SO
1528 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1529 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
c46ee386
AAJ
1530}
1531
fe7c5800
SO
1532static 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);
cec4b8ed 1536 struct pn533_poll_modulations *cur_mod;
dfccd0f5 1537 u8 rand_mod;
cec4b8ed 1538 int rc;
fe7c5800 1539
9815c7cf 1540 dev_dbg(dev->dev,
b4834839
JP
1541 "%s: im protocols 0x%x tm protocols 0x%x\n",
1542 __func__, im_protocols, tm_protocols);
fe7c5800
SO
1543
1544 if (dev->tgt_active_prot) {
9815c7cf 1545 nfc_err(dev->dev,
073a625f 1546 "Cannot poll with a target already activated\n");
fe7c5800
SO
1547 return -EBUSY;
1548 }
1549
51ad304c 1550 if (dev->tgt_mode) {
9815c7cf 1551 nfc_err(dev->dev,
073a625f 1552 "Cannot poll while already being activated\n");
51ad304c
SO
1553 return -EBUSY;
1554 }
1555
6fbbdc16
SO
1556 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 }
ad3823ce 1561
6fbbdc16
SO
1562 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1563 dev->poll_protocols = im_protocols;
1564 dev->listen_protocols = tm_protocols;
ad3823ce 1565
dfccd0f5
SO
1566 /* 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
cec4b8ed
SO
1571 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;
fe7c5800
SO
1580}
1581
c46ee386
AAJ
1582static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1583{
1584 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1585
6fbbdc16
SO
1586 del_timer(&dev->listen_timer);
1587
c46ee386 1588 if (!dev->poll_mod_count) {
9815c7cf 1589 dev_dbg(dev->dev,
b4834839 1590 "Polling operation was not running\n");
c46ee386
AAJ
1591 return;
1592 }
1593
9815c7cf 1594 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
46f793b0 1595 flush_delayed_work(&dev->poll_work);
7c2a04a9 1596 pn533_poll_reset_mod_list(dev);
c46ee386
AAJ
1597}
1598
1599static int pn533_activate_target_nfcdep(struct pn533 *dev)
1600{
cb950d93 1601 struct pn533_cmd_activate_response *rsp;
541d920b 1602 u16 gt_len;
c46ee386 1603 int rc;
cb950d93
WR
1604 struct sk_buff *skb;
1605 struct sk_buff *resp;
c46ee386 1606
9815c7cf 1607 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386 1608
9e2d493e 1609 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
cb950d93
WR
1610 if (!skb)
1611 return -ENOMEM;
c46ee386 1612
cb950d93
WR
1613 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1614 *skb_put(skb, sizeof(u8)) = 0; /* Next */
c46ee386 1615
cb950d93
WR
1616 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1617 if (IS_ERR(resp))
1618 return PTR_ERR(resp);
c46ee386 1619
37cf4fc6 1620 rsp = (struct pn533_cmd_activate_response *)resp->data;
cb950d93 1621 rc = rsp->status & PN533_CMD_RET_MASK;
8a0ecfe7 1622 if (rc != PN533_CMD_RET_SUCCESS) {
9815c7cf 1623 nfc_err(dev->dev,
073a625f 1624 "Target activation failed (error 0x%x)\n", rc);
cb950d93 1625 dev_kfree_skb(resp);
c46ee386 1626 return -EIO;
8a0ecfe7 1627 }
c46ee386 1628
541d920b 1629 /* ATR_RES general bytes are located at offset 16 */
cb950d93
WR
1630 gt_len = resp->len - 16;
1631 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
541d920b 1632
cb950d93 1633 dev_kfree_skb(resp);
541d920b 1634 return rc;
c46ee386
AAJ
1635}
1636
90099433
EL
1637static int pn533_activate_target(struct nfc_dev *nfc_dev,
1638 struct nfc_target *target, u32 protocol)
c46ee386
AAJ
1639{
1640 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1641 int rc;
1642
9815c7cf 1643 dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
c46ee386
AAJ
1644
1645 if (dev->poll_mod_count) {
9815c7cf 1646 nfc_err(dev->dev,
b4834839 1647 "Cannot activate while polling\n");
c46ee386
AAJ
1648 return -EBUSY;
1649 }
1650
1651 if (dev->tgt_active_prot) {
9815c7cf 1652 nfc_err(dev->dev,
073a625f 1653 "There is already an active target\n");
c46ee386
AAJ
1654 return -EBUSY;
1655 }
1656
1657 if (!dev->tgt_available_prots) {
9815c7cf 1658 nfc_err(dev->dev,
073a625f 1659 "There is no available target to activate\n");
c46ee386
AAJ
1660 return -EINVAL;
1661 }
1662
1663 if (!(dev->tgt_available_prots & (1 << protocol))) {
9815c7cf 1664 nfc_err(dev->dev,
073a625f
JP
1665 "Target doesn't support requested proto %u\n",
1666 protocol);
c46ee386
AAJ
1667 return -EINVAL;
1668 }
1669
1670 if (protocol == NFC_PROTO_NFC_DEP) {
1671 rc = pn533_activate_target_nfcdep(dev);
1672 if (rc) {
9815c7cf 1673 nfc_err(dev->dev,
073a625f 1674 "Activating target with DEP failed %d\n", rc);
c46ee386
AAJ
1675 return rc;
1676 }
1677 }
1678
1679 dev->tgt_active_prot = protocol;
1680 dev->tgt_available_prots = 0;
1681
1682 return 0;
1683}
1684
37f895d7
MT
1685static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1686 struct sk_buff *resp)
1687{
1688 int rc = 0;
1689
9815c7cf 1690 dev_dbg(dev->dev, "%s\n", __func__);
37f895d7
MT
1691
1692 if (IS_ERR(resp)) {
1693 rc = PTR_ERR(resp);
1694
9815c7cf 1695 nfc_err(dev->dev, "Target release error %d\n", rc);
37f895d7
MT
1696
1697 return rc;
1698 }
1699
1700 rc = resp->data[0] & PN533_CMD_RET_MASK;
1701 if (rc != PN533_CMD_RET_SUCCESS)
9815c7cf 1702 nfc_err(dev->dev,
37f895d7
MT
1703 "Error 0x%x when releasing the target\n", rc);
1704
1705 dev_kfree_skb(resp);
1706 return rc;
1707}
1708
90099433 1709static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
96d4581f 1710 struct nfc_target *target, u8 mode)
c46ee386
AAJ
1711{
1712 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
cb950d93 1713 struct sk_buff *skb;
c46ee386
AAJ
1714 int rc;
1715
9815c7cf 1716 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386
AAJ
1717
1718 if (!dev->tgt_active_prot) {
9815c7cf 1719 nfc_err(dev->dev, "There is no active target\n");
c46ee386
AAJ
1720 return;
1721 }
1722
1723 dev->tgt_active_prot = 0;
6ff73fd2
SO
1724 skb_queue_purge(&dev->resp_q);
1725
9e2d493e 1726 skb = pn533_alloc_skb(dev, sizeof(u8));
cb950d93
WR
1727 if (!skb)
1728 return;
c46ee386 1729
cb950d93 1730 *skb_put(skb, 1) = 1; /* TG*/
c46ee386 1731
37f895d7
MT
1732 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);
9815c7cf 1736 nfc_err(dev->dev, "Target release error %d\n", rc);
37f895d7 1737 }
c46ee386
AAJ
1738}
1739
361f3cb7
SO
1740
1741static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
13003649 1742 struct sk_buff *resp)
361f3cb7 1743{
13003649 1744 struct pn533_cmd_jump_dep_response *rsp;
361f3cb7
SO
1745 u8 target_gt_len;
1746 int rc;
13003649 1747 u8 active = *(u8 *)arg;
70418e6e
WR
1748
1749 kfree(arg);
361f3cb7 1750
13003649
WR
1751 if (IS_ERR(resp))
1752 return PTR_ERR(resp);
361f3cb7
SO
1753
1754 if (dev->tgt_available_prots &&
1755 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
9815c7cf 1756 nfc_err(dev->dev,
073a625f 1757 "The target does not support DEP\n");
13003649
WR
1758 rc = -EINVAL;
1759 goto error;
361f3cb7
SO
1760 }
1761
13003649
WR
1762 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1763
1764 rc = rsp->status & PN533_CMD_RET_MASK;
361f3cb7 1765 if (rc != PN533_CMD_RET_SUCCESS) {
9815c7cf 1766 nfc_err(dev->dev,
073a625f 1767 "Bringing DEP link up failed (error 0x%x)\n", rc);
13003649 1768 goto error;
361f3cb7
SO
1769 }
1770
1771 if (!dev->tgt_available_prots) {
13003649
WR
1772 struct nfc_target nfc_target;
1773
9815c7cf 1774 dev_dbg(dev->dev, "Creating new target\n");
361f3cb7
SO
1775
1776 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2fbabfa4 1777 nfc_target.nfcid1_len = 10;
13003649 1778 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
361f3cb7
SO
1779 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1780 if (rc)
13003649 1781 goto error;
361f3cb7
SO
1782
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 */
13003649 1789 target_gt_len = resp->len - 17;
361f3cb7 1790 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
13003649 1791 rsp->gt, target_gt_len);
361f3cb7
SO
1792 if (rc == 0)
1793 rc = nfc_dep_link_is_up(dev->nfc_dev,
13003649
WR
1794 dev->nfc_dev->targets[0].idx,
1795 !active, NFC_RF_INITIATOR);
361f3cb7 1796
13003649
WR
1797error:
1798 dev_kfree_skb(resp);
1799 return rc;
361f3cb7
SO
1800}
1801
17e9d9d4 1802static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
90099433 1803static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
37cf4fc6 1804 u8 comm_mode, u8 *gb, size_t gb_len)
361f3cb7
SO
1805{
1806 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
13003649 1807 struct sk_buff *skb;
5eef4845
SO
1808 int rc, skb_len;
1809 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
d7f3345d 1810 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
361f3cb7 1811
9815c7cf 1812 dev_dbg(dev->dev, "%s\n", __func__);
361f3cb7 1813
361f3cb7 1814 if (dev->poll_mod_count) {
9815c7cf 1815 nfc_err(dev->dev,
073a625f 1816 "Cannot bring the DEP link up while polling\n");
361f3cb7
SO
1817 return -EBUSY;
1818 }
1819
1820 if (dev->tgt_active_prot) {
9815c7cf 1821 nfc_err(dev->dev,
073a625f 1822 "There is already an active target\n");
361f3cb7
SO
1823 return -EBUSY;
1824 }
1825
13003649 1826 skb_len = 3 + gb_len; /* ActPass + BR + Next */
5eef4845 1827 skb_len += PASSIVE_DATA_LEN;
d7f3345d 1828
5eef4845
SO
1829 /* 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 }
322bce95 1836
9e2d493e 1837 skb = pn533_alloc_skb(dev, skb_len);
13003649 1838 if (!skb)
361f3cb7
SO
1839 return -ENOMEM;
1840
13003649 1841 *skb_put(skb, 1) = !comm_mode; /* ActPass */
5eef4845 1842 *skb_put(skb, 1) = 0x02; /* 424 kbps */
13003649
WR
1843
1844 next = skb_put(skb, 1); /* Next */
1845 *next = 0;
361f3cb7 1846
5eef4845
SO
1847 /* Copy passive data */
1848 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1849 *next |= 1;
d7f3345d 1850
5eef4845
SO
1851 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1852 if (target && target->nfcid2_len)
322bce95
SO
1853 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
1854 target->nfcid2_len);
5eef4845
SO
1855 else
1856 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1857 NFC_NFCID3_MAXSIZE);
1858 *next |= 2;
322bce95 1859
47807d3d 1860 if (gb != NULL && gb_len > 0) {
13003649
WR
1861 memcpy(skb_put(skb, gb_len), gb, gb_len);
1862 *next |= 4; /* We have some Gi */
361f3cb7 1863 } else {
13003649 1864 *next = 0;
361f3cb7
SO
1865 }
1866
13003649
WR
1867 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1868 if (!arg) {
1869 dev_kfree_skb(skb);
1870 return -ENOMEM;
1871 }
361f3cb7 1872
13003649 1873 *arg = !comm_mode;
361f3cb7 1874
17e9d9d4
SO
1875 pn533_rf_field(dev->nfc_dev, 0);
1876
13003649
WR
1877 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 }
361f3cb7
SO
1884
1885 return rc;
1886}
1887
1888static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1889{
6fbbdc16
SO
1890 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1891
9815c7cf 1892 dev_dbg(dev->dev, "%s\n", __func__);
fcfafc76 1893
6fbbdc16
SO
1894 pn533_poll_reset_mod_list(dev);
1895
10cff29a 1896 if (dev->tgt_mode || dev->tgt_active_prot)
9815c7cf 1897 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
51ad304c
SO
1898
1899 dev->tgt_active_prot = 0;
1900 dev->tgt_mode = 0;
1901
1902 skb_queue_purge(&dev->resp_q);
361f3cb7
SO
1903
1904 return 0;
1905}
1906
c46ee386 1907struct pn533_data_exchange_arg {
c46ee386
AAJ
1908 data_exchange_cb_t cb;
1909 void *cb_context;
1910};
1911
6ff73fd2
SO
1912static 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
9815c7cf 1917 dev_dbg(dev->dev, "%s\n", __func__);
6ff73fd2
SO
1918
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
9815c7cf 1930 dev_dbg(dev->dev, "%s total length %d\n",
b4834839 1931 __func__, skb_len);
6ff73fd2
SO
1932
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
c46ee386 1950static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
b1e666f5 1951 struct sk_buff *resp)
c46ee386
AAJ
1952{
1953 struct pn533_data_exchange_arg *arg = _arg;
b1e666f5
WR
1954 struct sk_buff *skb;
1955 int rc = 0;
1956 u8 status, ret, mi;
c46ee386 1957
9815c7cf 1958 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386 1959
b1e666f5
WR
1960 if (IS_ERR(resp)) {
1961 rc = PTR_ERR(resp);
1962 goto _error;
c46ee386
AAJ
1963 }
1964
b1e666f5
WR
1965 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));
c46ee386 1970
b1e666f5 1971 if (ret != PN533_CMD_RET_SUCCESS) {
9815c7cf 1972 nfc_err(dev->dev,
073a625f 1973 "Exchanging data failed (error 0x%x)\n", ret);
b1e666f5 1974 rc = -EIO;
c46ee386
AAJ
1975 goto error;
1976 }
1977
b1e666f5 1978 skb_queue_tail(&dev->resp_q, resp);
6ff73fd2 1979
b1e666f5
WR
1980 if (mi) {
1981 dev->cmd_complete_mi_arg = arg;
963a82e0
OG
1982 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
6ff73fd2 1991 return -EINPROGRESS;
c46ee386
AAJ
1992 }
1993
6ff73fd2 1994 skb = pn533_build_response(dev);
5df848f3
JL
1995 if (!skb) {
1996 rc = -ENOMEM;
6ff73fd2 1997 goto error;
5df848f3 1998 }
c46ee386 1999
6ff73fd2 2000 arg->cb(arg->cb_context, skb, 0);
c46ee386
AAJ
2001 kfree(arg);
2002 return 0;
2003
2004error:
b1e666f5
WR
2005 dev_kfree_skb(resp);
2006_error:
6ff73fd2 2007 skb_queue_purge(&dev->resp_q);
b1e666f5 2008 arg->cb(arg->cb_context, NULL, rc);
c46ee386 2009 kfree(arg);
b1e666f5 2010 return rc;
c46ee386
AAJ
2011}
2012
9815c7cf
MT
2013/*
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{
2019 dev->cmd->status = status;
2020
2021 if (skb == NULL) {
2022 pr_err("NULL Frame -> link is dead\n");
2023 goto sched_wq;
2024 }
2025
2026 if (pn533_rx_frame_is_ack(skb->data)) {
2027 dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2028 dev_kfree_skb(skb);
2029 return;
2030 }
2031
2032 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2033 dev->ops->rx_frame_size(skb->data), false);
2034
2035 if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2036 nfc_err(dev->dev, "Received an invalid frame\n");
2037 dev->cmd->status = -EIO;
2038 } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2039 nfc_err(dev->dev, "It it not the response to the last command\n");
2040 dev->cmd->status = -EIO;
2041 }
2042
2043 dev->cmd->resp = skb;
2044
2045sched_wq:
2046 queue_work(dev->wq, &dev->cmd_complete_work);
2047}
2048EXPORT_SYMBOL(pn533_recv_frame);
2049
963a82e0
OG
2050/* Split the Tx skb into small chunks */
2051static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2052{
2053 struct sk_buff *frag;
2054 int frag_size;
2055
2056 do {
2057 /* Remaining size */
2058 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2059 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2060 else
2061 frag_size = skb->len;
2062
2063 /* Allocate and reserve */
2064 frag = pn533_alloc_skb(dev, frag_size);
2065 if (!frag) {
2066 skb_queue_purge(&dev->fragment_skb);
2067 break;
2068 }
2069
22953f93
OG
2070 if (!dev->tgt_mode) {
2071 /* Reserve the TG/MI byte */
2072 skb_reserve(frag, 1);
2073
2074 /* MI + TG */
2075 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2076 *skb_push(frag, sizeof(u8)) =
2077 (PN533_CMD_MI_MASK | 1);
2078 else
2079 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2080 }
963a82e0
OG
2081
2082 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2083
2084 /* Reduce the size of incoming buffer */
2085 skb_pull(skb, frag_size);
2086
2087 /* Add this to skb_queue */
2088 skb_queue_tail(&dev->fragment_skb, frag);
2089
2090 } while (skb->len > 0);
2091
2092 dev_kfree_skb(skb);
2093
2094 return skb_queue_len(&dev->fragment_skb);
2095}
2096
be9ae4ce
SO
2097static int pn533_transceive(struct nfc_dev *nfc_dev,
2098 struct nfc_target *target, struct sk_buff *skb,
2099 data_exchange_cb_t cb, void *cb_context)
c46ee386
AAJ
2100{
2101 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
b1e666f5 2102 struct pn533_data_exchange_arg *arg = NULL;
c46ee386
AAJ
2103 int rc;
2104
9815c7cf 2105 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386
AAJ
2106
2107 if (!dev->tgt_active_prot) {
9815c7cf 2108 nfc_err(dev->dev,
073a625f 2109 "Can't exchange data if there is no active target\n");
c46ee386
AAJ
2110 rc = -EINVAL;
2111 goto error;
2112 }
2113
b1e666f5 2114 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
c46ee386
AAJ
2115 if (!arg) {
2116 rc = -ENOMEM;
b1e666f5 2117 goto error;
c46ee386
AAJ
2118 }
2119
c46ee386
AAJ
2120 arg->cb = cb;
2121 arg->cb_context = cb_context;
2122
b1e666f5
WR
2123 switch (dev->device_type) {
2124 case PN533_DEVICE_PASORI:
2125 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2126 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2127 skb,
2128 pn533_data_exchange_complete,
2129 arg);
2130
2131 break;
2132 }
2133 default:
963a82e0
OG
2134 /* jumbo frame ? */
2135 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2136 rc = pn533_fill_fragment_skbs(dev, skb);
2137 if (rc <= 0)
2138 goto error;
2139
2140 skb = skb_dequeue(&dev->fragment_skb);
2141 if (!skb) {
2142 rc = -EIO;
2143 goto error;
2144 }
2145 } else {
2146 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2147 }
b1e666f5
WR
2148
2149 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2150 skb, pn533_data_exchange_complete,
2151 arg);
2152
2153 break;
c46ee386
AAJ
2154 }
2155
b1e666f5
WR
2156 if (rc < 0) /* rc from send_async */
2157 goto error;
2158
c46ee386
AAJ
2159 return 0;
2160
c46ee386 2161error:
b1e666f5
WR
2162 kfree(arg);
2163 dev_kfree_skb(skb);
c46ee386
AAJ
2164 return rc;
2165}
2166
dadb06f2 2167static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
e4878823 2168 struct sk_buff *resp)
dadb06f2 2169{
e4878823 2170 u8 status;
5b412fd1 2171
9815c7cf 2172 dev_dbg(dev->dev, "%s\n", __func__);
dadb06f2 2173
e4878823
WR
2174 if (IS_ERR(resp))
2175 return PTR_ERR(resp);
5b412fd1 2176
e4878823 2177 status = resp->data[0];
dadb06f2 2178
93ad4202
OG
2179 /* Prepare for the next round */
2180 if (skb_queue_len(&dev->fragment_skb) > 0) {
2181 queue_work(dev->wq, &dev->mi_tm_tx_work);
2182 return -EINPROGRESS;
2183 }
e4878823 2184 dev_kfree_skb(resp);
dadb06f2 2185
e4878823 2186 if (status != 0) {
dadb06f2
SO
2187 nfc_tm_deactivated(dev->nfc_dev);
2188
51ad304c
SO
2189 dev->tgt_mode = 0;
2190
dadb06f2
SO
2191 return 0;
2192 }
2193
2194 queue_work(dev->wq, &dev->tg_work);
2195
2196 return 0;
2197}
2198
2199static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2200{
2201 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
dadb06f2
SO
2202 int rc;
2203
9815c7cf 2204 dev_dbg(dev->dev, "%s\n", __func__);
dadb06f2 2205
93ad4202 2206 /* let's split in multiple chunks if size's too big */
e4878823 2207 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
93ad4202
OG
2208 rc = pn533_fill_fragment_skbs(dev, skb);
2209 if (rc <= 0)
2210 goto error;
2211
2212 /* get the first skb */
2213 skb = skb_dequeue(&dev->fragment_skb);
2214 if (!skb) {
2215 rc = -EIO;
2216 goto error;
2217 }
2218
2219 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2220 pn533_tm_send_complete, NULL);
2221 } else {
2222 /* Send th skb */
2223 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2224 pn533_tm_send_complete, NULL);
dadb06f2
SO
2225 }
2226
93ad4202
OG
2227error:
2228 if (rc < 0) {
e4878823 2229 dev_kfree_skb(skb);
93ad4202
OG
2230 skb_queue_purge(&dev->fragment_skb);
2231 }
dadb06f2
SO
2232
2233 return rc;
2234}
2235
6ff73fd2
SO
2236static void pn533_wq_mi_recv(struct work_struct *work)
2237{
963a82e0 2238 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
b1e666f5 2239 struct sk_buff *skb;
6ff73fd2
SO
2240 int rc;
2241
9815c7cf 2242 dev_dbg(dev->dev, "%s\n", __func__);
6ff73fd2 2243
9e2d493e 2244 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
b1e666f5
WR
2245 if (!skb)
2246 goto error;
6ff73fd2 2247
b1e666f5
WR
2248 switch (dev->device_type) {
2249 case PN533_DEVICE_PASORI:
2250 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2251 rc = pn533_send_cmd_direct_async(dev,
2252 PN533_CMD_IN_COMM_THRU,
2253 skb,
2254 pn533_data_exchange_complete,
2255 dev->cmd_complete_mi_arg);
6ff73fd2 2256
b1e666f5
WR
2257 break;
2258 }
2259 default:
2260 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
6ff73fd2 2261
b1e666f5
WR
2262 rc = pn533_send_cmd_direct_async(dev,
2263 PN533_CMD_IN_DATA_EXCHANGE,
2264 skb,
2265 pn533_data_exchange_complete,
2266 dev->cmd_complete_mi_arg);
b1bb290a 2267
b1e666f5 2268 break;
6ff73fd2
SO
2269 }
2270
b1e666f5 2271 if (rc == 0) /* success */
6ff73fd2
SO
2272 return;
2273
9815c7cf 2274 nfc_err(dev->dev,
073a625f 2275 "Error %d when trying to perform data_exchange\n", rc);
6ff73fd2 2276
b1e666f5 2277 dev_kfree_skb(skb);
140ef7f6 2278 kfree(dev->cmd_complete_mi_arg);
6ff73fd2 2279
b1e666f5 2280error:
9815c7cf 2281 dev->phy_ops->send_ack(dev, GFP_KERNEL);
5d50b364 2282 queue_work(dev->wq, &dev->cmd_work);
6ff73fd2
SO
2283}
2284
963a82e0
OG
2285static void pn533_wq_mi_send(struct work_struct *work)
2286{
2287 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2288 struct sk_buff *skb;
2289 int rc;
2290
9815c7cf 2291 dev_dbg(dev->dev, "%s\n", __func__);
963a82e0
OG
2292
2293 /* Grab the first skb in the queue */
2294 skb = skb_dequeue(&dev->fragment_skb);
2295
2296 if (skb == NULL) { /* No more data */
2297 /* Reset the queue for future use */
2298 skb_queue_head_init(&dev->fragment_skb);
2299 goto error;
2300 }
2301
2302 switch (dev->device_type) {
2303 case PN533_DEVICE_PASORI:
2304 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2305 rc = -EIO;
2306 break;
2307 }
2308
2309 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2310 skb,
2311 pn533_data_exchange_complete,
2312 dev->cmd_complete_dep_arg);
2313
2314 break;
2315
2316 default:
2317 /* Still some fragments? */
9815c7cf
MT
2318 rc = pn533_send_cmd_direct_async(dev,
2319 PN533_CMD_IN_DATA_EXCHANGE,
963a82e0
OG
2320 skb,
2321 pn533_data_exchange_complete,
2322 dev->cmd_complete_dep_arg);
2323
2324 break;
2325 }
2326
2327 if (rc == 0) /* success */
2328 return;
2329
9815c7cf 2330 nfc_err(dev->dev,
073a625f 2331 "Error %d when trying to perform data_exchange\n", rc);
963a82e0
OG
2332
2333 dev_kfree_skb(skb);
2334 kfree(dev->cmd_complete_dep_arg);
2335
2336error:
9815c7cf 2337 dev->phy_ops->send_ack(dev, GFP_KERNEL);
963a82e0
OG
2338 queue_work(dev->wq, &dev->cmd_work);
2339}
2340
c46ee386
AAJ
2341static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2342 u8 cfgdata_len)
2343{
cb950d93
WR
2344 struct sk_buff *skb;
2345 struct sk_buff *resp;
cb950d93 2346 int skb_len;
c46ee386 2347
9815c7cf 2348 dev_dbg(dev->dev, "%s\n", __func__);
c46ee386 2349
cb950d93 2350 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
c46ee386 2351
9e2d493e 2352 skb = pn533_alloc_skb(dev, skb_len);
cb950d93
WR
2353 if (!skb)
2354 return -ENOMEM;
c46ee386 2355
cb950d93
WR
2356 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2357 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
c46ee386 2358
cb950d93
WR
2359 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2360 if (IS_ERR(resp))
2361 return PTR_ERR(resp);
c46ee386 2362
cb950d93
WR
2363 dev_kfree_skb(resp);
2364 return 0;
2365}
2366
2367static int pn533_get_firmware_version(struct pn533 *dev,
2368 struct pn533_fw_version *fv)
2369{
2370 struct sk_buff *skb;
2371 struct sk_buff *resp;
2372
9e2d493e 2373 skb = pn533_alloc_skb(dev, 0);
cb950d93
WR
2374 if (!skb)
2375 return -ENOMEM;
2376
2377 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2378 if (IS_ERR(resp))
2379 return PTR_ERR(resp);
2380
2381 fv->ic = resp->data[0];
2382 fv->ver = resp->data[1];
2383 fv->rev = resp->data[2];
2384 fv->support = resp->data[3];
2385
2386 dev_kfree_skb(resp);
2387 return 0;
c46ee386
AAJ
2388}
2389
f75c2913 2390static int pn533_pasori_fw_reset(struct pn533 *dev)
5c7b0531 2391{
cb950d93
WR
2392 struct sk_buff *skb;
2393 struct sk_buff *resp;
5c7b0531 2394
9815c7cf 2395 dev_dbg(dev->dev, "%s\n", __func__);
5c7b0531 2396
9e2d493e 2397 skb = pn533_alloc_skb(dev, sizeof(u8));
cb950d93
WR
2398 if (!skb)
2399 return -ENOMEM;
5c7b0531 2400
cb950d93 2401 *skb_put(skb, sizeof(u8)) = 0x1;
5c7b0531 2402
cb950d93
WR
2403 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2404 if (IS_ERR(resp))
2405 return PTR_ERR(resp);
5c7b0531 2406
cb950d93 2407 dev_kfree_skb(resp);
5c7b0531 2408
94c5c156 2409 return 0;
5c7b0531
SO
2410}
2411
60d9edd5
SO
2412static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2413{
2414 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2415 u8 rf_field = !!rf;
2416 int rc;
2417
3a8eab39
SO
2418 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2419
60d9edd5
SO
2420 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2421 (u8 *)&rf_field, 1);
2422 if (rc) {
9815c7cf 2423 nfc_err(dev->dev, "Error on setting RF field\n");
60d9edd5
SO
2424 return rc;
2425 }
2426
2427 return rc;
2428}
2429
dd7bedcd
MT
2430static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2431{
2432 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2433 struct sk_buff *skb;
2434 struct sk_buff *resp;
2435
2436 skb = pn533_alloc_skb(dev, 1);
2437 if (!skb)
2438 return -ENOMEM;
2439
2440 *skb_put(skb, 1) = 0x01;
2441
2442 resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2443 if (IS_ERR(resp))
2444 return PTR_ERR(resp);
2445
2446 dev_kfree_skb(resp);
2447 return 0;
2448}
2449
e44666b9 2450static int pn533_dev_up(struct nfc_dev *nfc_dev)
60d9edd5 2451{
dd7bedcd
MT
2452 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2453
2454 if (dev->device_type == PN533_DEVICE_PN532) {
2455 int rc = pn532_sam_configuration(nfc_dev);
2456
2457 if (rc)
2458 return rc;
2459 }
2460
60d9edd5
SO
2461 return pn533_rf_field(nfc_dev, 1);
2462}
2463
e44666b9 2464static int pn533_dev_down(struct nfc_dev *nfc_dev)
60d9edd5
SO
2465{
2466 return pn533_rf_field(nfc_dev, 0);
2467}
2468
5c7b0531 2469static struct nfc_ops pn533_nfc_ops = {
60d9edd5
SO
2470 .dev_up = pn533_dev_up,
2471 .dev_down = pn533_dev_down,
361f3cb7
SO
2472 .dep_link_up = pn533_dep_link_up,
2473 .dep_link_down = pn533_dep_link_down,
c46ee386
AAJ
2474 .start_poll = pn533_start_poll,
2475 .stop_poll = pn533_stop_poll,
2476 .activate_target = pn533_activate_target,
2477 .deactivate_target = pn533_deactivate_target,
be9ae4ce 2478 .im_transceive = pn533_transceive,
dadb06f2 2479 .tm_send = pn533_tm_send,
c46ee386
AAJ
2480};
2481
5c7b0531
SO
2482static int pn533_setup(struct pn533 *dev)
2483{
2484 struct pn533_config_max_retries max_retries;
2485 struct pn533_config_timing timing;
2486 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2487 int rc;
2488
2489 switch (dev->device_type) {
2490 case PN533_DEVICE_STD:
5c7b0531 2491 case PN533_DEVICE_PASORI:
53cf4839 2492 case PN533_DEVICE_ACR122U:
dd7bedcd 2493 case PN533_DEVICE_PN532:
5c7b0531
SO
2494 max_retries.mx_rty_atr = 0x2;
2495 max_retries.mx_rty_psl = 0x1;
2496 max_retries.mx_rty_passive_act =
2497 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2498
2499 timing.rfu = PN533_CONFIG_TIMING_102;
2500 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2501 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2502
2503 break;
2504
2505 default:
9815c7cf 2506 nfc_err(dev->dev, "Unknown device type %d\n",
073a625f 2507 dev->device_type);
5c7b0531
SO
2508 return -EINVAL;
2509 }
2510
2511 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2512 (u8 *)&max_retries, sizeof(max_retries));
2513 if (rc) {
9815c7cf 2514 nfc_err(dev->dev,
073a625f 2515 "Error on setting MAX_RETRIES config\n");
5c7b0531
SO
2516 return rc;
2517 }
2518
2519
2520 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2521 (u8 *)&timing, sizeof(timing));
2522 if (rc) {
9815c7cf 2523 nfc_err(dev->dev, "Error on setting RF timings\n");
5c7b0531
SO
2524 return rc;
2525 }
2526
2527 switch (dev->device_type) {
2528 case PN533_DEVICE_STD:
dd7bedcd 2529 case PN533_DEVICE_PN532:
5c7b0531
SO
2530 break;
2531
2532 case PN533_DEVICE_PASORI:
f75c2913 2533 pn533_pasori_fw_reset(dev);
5c7b0531
SO
2534
2535 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2536 pasori_cfg, 3);
2537 if (rc) {
9815c7cf 2538 nfc_err(dev->dev,
073a625f 2539 "Error while settings PASORI config\n");
5c7b0531
SO
2540 return rc;
2541 }
2542
f75c2913 2543 pn533_pasori_fw_reset(dev);
5c7b0531
SO
2544
2545 break;
2546 }
2547
2548 return 0;
2549}
2550
9815c7cf
MT
2551struct pn533 *pn533_register_device(u32 device_type,
2552 u32 protocols,
2553 enum pn533_protocol_type protocol_type,
2554 void *phy,
2555 struct pn533_phy_ops *phy_ops,
2556 struct pn533_frame_ops *fops,
b16931b1
MT
2557 struct device *dev,
2558 struct device *parent)
c46ee386 2559{
cb950d93 2560 struct pn533_fw_version fw_ver;
9815c7cf 2561 struct pn533 *priv;
c46ee386 2562 int rc = -ENOMEM;
c46ee386 2563
9815c7cf
MT
2564 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2565 if (!priv)
2566 return ERR_PTR(-ENOMEM);
c46ee386 2567
9815c7cf
MT
2568 priv->phy = phy;
2569 priv->phy_ops = phy_ops;
2570 priv->dev = dev;
2571 if (fops != NULL)
2572 priv->ops = fops;
2573 else
2574 priv->ops = &pn533_std_frame_ops;
2575
2576 priv->protocol_type = protocol_type;
2577 priv->device_type = device_type;
2578
2579 mutex_init(&priv->cmd_lock);
2580
2581 INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2582 INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2583 INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2584 INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2585 INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2586 INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2587 INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2588 INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2589 INIT_WORK(&priv->rf_work, pn533_wq_rf);
2590 priv->wq = alloc_ordered_workqueue("pn533", 0);
2591 if (priv->wq == NULL)
4849f85e 2592 goto error;
c46ee386 2593
9815c7cf
MT
2594 init_timer(&priv->listen_timer);
2595 priv->listen_timer.data = (unsigned long) priv;
2596 priv->listen_timer.function = pn533_listen_mode_timer;
5c7b0531 2597
9815c7cf
MT
2598 skb_queue_head_init(&priv->resp_q);
2599 skb_queue_head_init(&priv->fragment_skb);
53cf4839 2600
9815c7cf 2601 INIT_LIST_HEAD(&priv->cmd_queue);
c46ee386 2602
9e2d493e 2603 memset(&fw_ver, 0, sizeof(fw_ver));
9815c7cf 2604 rc = pn533_get_firmware_version(priv, &fw_ver);
9e2d493e
WR
2605 if (rc < 0)
2606 goto destroy_wq;
2607
9815c7cf 2608 nfc_info(dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
073a625f 2609 fw_ver.ic, fw_ver.ver, fw_ver.rev);
9e2d493e
WR
2610
2611
9815c7cf
MT
2612 priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2613 priv->ops->tx_header_len +
e8753043 2614 PN533_CMD_DATAEXCH_HEAD_LEN,
9815c7cf
MT
2615 priv->ops->tx_tail_len);
2616 if (!priv->nfc_dev) {
4674d0fe 2617 rc = -ENOMEM;
4849f85e 2618 goto destroy_wq;
4674d0fe 2619 }
c46ee386 2620
b16931b1 2621 nfc_set_parent_dev(priv->nfc_dev, parent);
9815c7cf 2622 nfc_set_drvdata(priv->nfc_dev, priv);
c46ee386 2623
9815c7cf 2624 rc = nfc_register_device(priv->nfc_dev);
c46ee386
AAJ
2625 if (rc)
2626 goto free_nfc_dev;
2627
9815c7cf 2628 rc = pn533_setup(priv);
5c7b0531 2629 if (rc)
34a85bfc 2630 goto unregister_nfc_dev;
34a85bfc 2631
9815c7cf 2632 return priv;
c46ee386 2633
9f2f8ba1 2634unregister_nfc_dev:
9815c7cf 2635 nfc_unregister_device(priv->nfc_dev);
9f2f8ba1 2636
c46ee386 2637free_nfc_dev:
9815c7cf 2638 nfc_free_device(priv->nfc_dev);
9f2f8ba1 2639
4849f85e 2640destroy_wq:
9815c7cf 2641 destroy_workqueue(priv->wq);
c46ee386 2642error:
9815c7cf
MT
2643 kfree(priv);
2644 return ERR_PTR(rc);
c46ee386 2645}
9815c7cf 2646EXPORT_SYMBOL_GPL(pn533_register_device);
c46ee386 2647
9815c7cf 2648void pn533_unregister_device(struct pn533 *priv)
c46ee386 2649{
5d50b364 2650 struct pn533_cmd *cmd, *n;
c46ee386 2651
9815c7cf
MT
2652 nfc_unregister_device(priv->nfc_dev);
2653 nfc_free_device(priv->nfc_dev);
c46ee386 2654
9815c7cf
MT
2655 flush_delayed_work(&priv->poll_work);
2656 destroy_workqueue(priv->wq);
c46ee386 2657
9815c7cf 2658 skb_queue_purge(&priv->resp_q);
c46ee386 2659
9815c7cf 2660 del_timer(&priv->listen_timer);
c46ee386 2661
9815c7cf 2662 list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
5d50b364
SO
2663 list_del(&cmd->queue);
2664 kfree(cmd);
2665 }
2666
9815c7cf 2667 kfree(priv);
c46ee386 2668}
9815c7cf 2669EXPORT_SYMBOL_GPL(pn533_unregister_device);
c46ee386 2670
c46ee386 2671
e70b96e9
WR
2672MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2673MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2674MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
9815c7cf 2675MODULE_DESCRIPTION("PN533 driver ver " VERSION);
c46ee386
AAJ
2676MODULE_VERSION(VERSION);
2677MODULE_LICENSE("GPL");