Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / nfc / pn533 / pn533.c
bloba0cc1cc452927b54e84d749d053fe99e63463ca2
1 /*
2 * Driver for NXP PN533 NFC Chip - core functions
4 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Copyright (C) 2012-2013 Tieto Poland
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/nfc.h>
26 #include <linux/netdevice.h>
27 #include <net/nfc/nfc.h>
28 #include "pn533.h"
30 #define VERSION "0.3"
32 /* How much time we spend listening for initiators */
33 #define PN533_LISTEN_TIME 2
34 /* Delay between each poll frame (ms) */
35 #define PN533_POLL_INTERVAL 10
37 /* structs for pn533 commands */
39 /* PN533_CMD_GET_FIRMWARE_VERSION */
40 struct pn533_fw_version {
41 u8 ic;
42 u8 ver;
43 u8 rev;
44 u8 support;
47 /* PN533_CMD_RF_CONFIGURATION */
48 #define PN533_CFGITEM_RF_FIELD 0x01
49 #define PN533_CFGITEM_TIMING 0x02
50 #define PN533_CFGITEM_MAX_RETRIES 0x05
51 #define PN533_CFGITEM_PASORI 0x82
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
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
62 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
63 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
65 struct pn533_config_max_retries {
66 u8 mx_rty_atr;
67 u8 mx_rty_psl;
68 u8 mx_rty_passive_act;
69 } __packed;
71 struct pn533_config_timing {
72 u8 rfu;
73 u8 atr_res_timeout;
74 u8 dep_timeout;
75 } __packed;
77 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
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
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
93 union 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;
106 struct 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;
115 static const struct pn533_poll_modulations poll_mod[] = {
116 [PN533_POLL_MOD_106KBPS_A] = {
117 .data = {
118 .maxtg = 1,
119 .brty = 0,
121 .len = 2,
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,
130 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
131 .tsn = 0x03,
134 .len = 7,
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,
143 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
144 .tsn = 0x03,
147 .len = 7,
149 [PN533_POLL_MOD_106KBPS_JEWEL] = {
150 .data = {
151 .maxtg = 1,
152 .brty = 4,
154 .len = 2,
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,
166 .len = 3,
168 [PN533_LISTEN_MOD] = {
169 .len = 0,
173 /* PN533_CMD_IN_ATR */
175 struct 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;
187 struct 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;
201 /* PN533_TG_INIT_AS_TARGET */
202 #define PN533_INIT_TARGET_PASSIVE 0x1
203 #define PN533_INIT_TARGET_DEP 0x2
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
209 /* The rule: value(high byte) + value(low byte) + checksum = 0 */
210 static inline u8 pn533_ext_checksum(u16 value)
212 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
215 /* The rule: value + checksum = 0 */
216 static inline u8 pn533_std_checksum(u8 value)
218 return ~value + 1;
221 /* The rule: sum(data elements) + checksum = 0 */
222 static u8 pn533_std_data_checksum(u8 *data, int datalen)
224 u8 sum = 0;
225 int i;
227 for (i = 0; i < datalen; i++)
228 sum += data[i];
230 return pn533_std_checksum(sum);
233 static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
235 struct pn533_std_frame *frame = _frame;
237 frame->preamble = 0;
238 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
239 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
240 PN533_FRAME_CMD(frame) = cmd_code;
241 frame->datalen = 2;
244 static void pn533_std_tx_frame_finish(void *_frame)
246 struct pn533_std_frame *frame = _frame;
248 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
250 PN533_STD_FRAME_CHECKSUM(frame) =
251 pn533_std_data_checksum(frame->data, frame->datalen);
253 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
256 static void pn533_std_tx_update_payload_len(void *_frame, int len)
258 struct pn533_std_frame *frame = _frame;
260 frame->datalen += len;
263 static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
265 u8 checksum;
266 struct pn533_std_frame *stdf = _frame;
268 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
269 return false;
271 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
272 /* Standard frame code */
273 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
275 checksum = pn533_std_checksum(stdf->datalen);
276 if (checksum != stdf->datalen_checksum)
277 return false;
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;
286 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
288 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
289 if (checksum != eif->datalen_checksum)
290 return false;
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;
299 return true;
302 bool pn533_rx_frame_is_ack(void *_frame)
304 struct pn533_std_frame *frame = _frame;
306 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
307 return false;
309 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
310 return false;
312 return true;
314 EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
316 static inline int pn533_std_rx_frame_size(void *frame)
318 struct pn533_std_frame *f = frame;
320 /* check for Extended Information frame */
321 if (PN533_STD_IS_EXTENDED(f)) {
322 struct pn533_ext_frame *eif = frame;
324 return sizeof(struct pn533_ext_frame)
325 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
328 return sizeof(struct pn533_std_frame) + f->datalen +
329 PN533_STD_FRAME_TAIL_LEN;
332 static u8 pn533_std_get_cmd_code(void *frame)
334 struct pn533_std_frame *f = frame;
335 struct pn533_ext_frame *eif = frame;
337 if (PN533_STD_IS_EXTENDED(f))
338 return PN533_FRAME_CMD(eif);
339 else
340 return PN533_FRAME_CMD(f);
343 bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
345 return (dev->ops->get_cmd_code(frame) ==
346 PN533_CMD_RESPONSE(dev->cmd->code));
348 EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);
351 static struct pn533_frame_ops pn533_std_frame_ops = {
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,
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,
363 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
364 .get_cmd_code = pn533_std_get_cmd_code,
367 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
368 struct sk_buff *skb)
370 /* payload is already there, just update datalen */
371 int payload_len = skb->len;
372 struct pn533_frame_ops *ops = dev->ops;
375 skb_push(skb, ops->tx_header_len);
376 skb_put(skb, ops->tx_tail_len);
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);
383 static int pn533_send_async_complete(struct pn533 *dev)
385 struct pn533_cmd *cmd = dev->cmd;
386 struct sk_buff *resp;
387 int status, rc = 0;
389 if (!cmd) {
390 dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
391 goto done;
394 dev_kfree_skb(cmd->req);
396 status = cmd->status;
397 resp = cmd->resp;
399 if (status < 0) {
400 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
401 ERR_PTR(status));
402 dev_kfree_skb(resp);
403 goto done;
406 /* when no response is set we got interrupted */
407 if (!resp)
408 resp = ERR_PTR(-EINTR);
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);
415 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
417 done:
418 kfree(cmd);
419 dev->cmd = NULL;
420 return rc;
423 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
424 struct sk_buff *req,
425 pn533_send_async_complete_t complete_cb,
426 void *complete_cb_context)
428 struct pn533_cmd *cmd;
429 int rc = 0;
431 dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
433 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
434 if (!cmd)
435 return -ENOMEM;
437 cmd->code = cmd_code;
438 cmd->req = req;
439 cmd->complete_cb = complete_cb;
440 cmd->complete_cb_context = complete_cb_context;
442 pn533_build_cmd_frame(dev, cmd_code, req);
444 mutex_lock(&dev->cmd_lock);
446 if (!dev->cmd_pending) {
447 dev->cmd = cmd;
448 rc = dev->phy_ops->send_frame(dev, req);
449 if (rc) {
450 dev->cmd = NULL;
451 goto error;
454 dev->cmd_pending = 1;
455 goto unlock;
458 dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
459 __func__, cmd_code);
461 INIT_LIST_HEAD(&cmd->queue);
462 list_add_tail(&cmd->queue, &dev->cmd_queue);
464 goto unlock;
466 error:
467 kfree(cmd);
468 unlock:
469 mutex_unlock(&dev->cmd_lock);
470 return rc;
473 static 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)
478 int rc;
480 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
481 complete_cb_context);
483 return rc;
486 static 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)
491 int rc;
493 rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
494 complete_cb_context);
496 return rc;
500 * pn533_send_cmd_direct_async
502 * The function sends a piority cmd directly to the chip omitting the cmd
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.
507 static 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)
512 struct pn533_cmd *cmd;
513 int rc;
515 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
516 if (!cmd)
517 return -ENOMEM;
519 cmd->code = cmd_code;
520 cmd->req = req;
521 cmd->complete_cb = complete_cb;
522 cmd->complete_cb_context = complete_cb_context;
524 pn533_build_cmd_frame(dev, cmd_code, req);
526 dev->cmd = cmd;
527 rc = dev->phy_ops->send_frame(dev, req);
528 if (rc < 0) {
529 dev->cmd = NULL;
530 kfree(cmd);
533 return rc;
536 static void pn533_wq_cmd_complete(struct work_struct *work)
538 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
539 int rc;
541 rc = pn533_send_async_complete(dev);
542 if (rc != -EINPROGRESS)
543 queue_work(dev->wq, &dev->cmd_work);
546 static void pn533_wq_cmd(struct work_struct *work)
548 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
549 struct pn533_cmd *cmd;
550 int rc;
552 mutex_lock(&dev->cmd_lock);
554 if (list_empty(&dev->cmd_queue)) {
555 dev->cmd_pending = 0;
556 mutex_unlock(&dev->cmd_lock);
557 return;
560 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
562 list_del(&cmd->queue);
564 mutex_unlock(&dev->cmd_lock);
566 dev->cmd = cmd;
567 rc = dev->phy_ops->send_frame(dev, cmd->req);
568 if (rc < 0) {
569 dev->cmd = NULL;
570 dev_kfree_skb(cmd->req);
571 kfree(cmd);
572 return;
577 struct pn533_sync_cmd_response {
578 struct sk_buff *resp;
579 struct completion done;
582 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
583 struct sk_buff *resp)
585 struct pn533_sync_cmd_response *arg = _arg;
587 arg->resp = resp;
588 complete(&arg->done);
590 return 0;
593 /* pn533_send_cmd_sync
595 * Please note the req parameter is freed inside the function to
596 * limit a number of return value interpretations by the caller.
598 * 1. negative in case of error during TX path -> req should be freed
600 * 2. negative in case of error during RX path -> req should not be freed
601 * as it's been already freed at the beginning of RX path by
602 * async_complete_cb.
604 * 3. valid pointer in case of succesfult RX path
606 * A caller has to check a return value with IS_ERR macro. If the test pass,
607 * the returned pointer is valid.
610 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
611 struct sk_buff *req)
613 int rc;
614 struct pn533_sync_cmd_response arg;
616 init_completion(&arg.done);
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);
625 wait_for_completion(&arg.done);
627 return arg.resp;
630 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
632 struct sk_buff *skb;
634 skb = alloc_skb(dev->ops->tx_header_len +
635 size +
636 dev->ops->tx_tail_len, GFP_KERNEL);
638 if (skb)
639 skb_reserve(skb, dev->ops->tx_header_len);
641 return skb;
644 struct pn533_target_type_a {
645 __be16 sens_res;
646 u8 sel_res;
647 u8 nfcid_len;
648 u8 nfcid_data[];
649 } __packed;
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))
656 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
657 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
659 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
660 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
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
667 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
668 int target_data_len)
670 u8 ssd;
671 u8 platconf;
673 if (target_data_len < sizeof(struct pn533_target_type_a))
674 return false;
677 * The length check of nfcid[] and ats[] are not being performed because
678 * the values are not being used
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);
685 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
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))
689 return false;
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;
695 return true;
698 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
699 int tgt_data_len)
701 struct pn533_target_type_a *tgt_type_a;
703 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
705 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
706 return -EPROTO;
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;
724 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
725 nfc_tgt->sel_res = tgt_type_a->sel_res;
726 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
727 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
729 return 0;
732 struct pn533_target_felica {
733 u8 pol_res;
734 u8 opcode;
735 u8 nfcid2[NFC_NFCID2_MAXSIZE];
736 u8 pad[8];
737 /* optional */
738 u8 syst_code[];
739 } __packed;
741 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
742 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
744 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
745 int target_data_len)
747 if (target_data_len < sizeof(struct pn533_target_felica))
748 return false;
750 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
751 return false;
753 return true;
756 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
757 int tgt_data_len)
759 struct pn533_target_felica *tgt_felica;
761 tgt_felica = (struct pn533_target_felica *)tgt_data;
763 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
764 return -EPROTO;
766 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
767 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
768 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
769 else
770 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
772 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
773 nfc_tgt->sensf_res_len = 9;
775 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
776 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
778 return 0;
781 struct pn533_target_jewel {
782 __be16 sens_res;
783 u8 jewelid[4];
784 } __packed;
786 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
787 int target_data_len)
789 u8 ssd;
790 u8 platconf;
792 if (target_data_len < sizeof(struct pn533_target_jewel))
793 return false;
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);
799 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
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))
803 return false;
805 return true;
808 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
809 int tgt_data_len)
811 struct pn533_target_jewel *tgt_jewel;
813 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
815 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
816 return -EPROTO;
818 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
819 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
820 nfc_tgt->nfcid1_len = 4;
821 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
823 return 0;
826 struct pn533_type_b_prot_info {
827 u8 bitrate;
828 u8 fsci_type;
829 u8 fwi_adc_fo;
830 } __packed;
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
836 struct 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;
843 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
845 struct pn533_target_type_b {
846 struct pn533_type_b_sens_res sensb_res;
847 u8 attrib_res_len;
848 u8 attrib_res[];
849 } __packed;
851 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
852 int target_data_len)
854 if (target_data_len < sizeof(struct pn533_target_type_b))
855 return false;
857 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
858 return false;
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;
864 return true;
867 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
868 int tgt_data_len)
870 struct pn533_target_type_b *tgt_type_b;
872 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
874 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
875 return -EPROTO;
877 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
879 return 0;
882 static void pn533_poll_reset_mod_list(struct pn533 *dev);
883 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
884 int tgdata_len)
886 struct nfc_target nfc_tgt;
887 int rc;
889 dev_dbg(dev->dev, "%s: modulation=%d\n",
890 __func__, dev->poll_mod_curr);
892 if (tg != 1)
893 return -EPROTO;
895 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
897 switch (dev->poll_mod_curr) {
898 case PN533_POLL_MOD_106KBPS_A:
899 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
900 break;
901 case PN533_POLL_MOD_212KBPS_FELICA:
902 case PN533_POLL_MOD_424KBPS_FELICA:
903 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
904 break;
905 case PN533_POLL_MOD_106KBPS_JEWEL:
906 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
907 break;
908 case PN533_POLL_MOD_847KBPS_B:
909 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
910 break;
911 default:
912 nfc_err(dev->dev,
913 "Unknown current poll modulation\n");
914 return -EPROTO;
917 if (rc)
918 return rc;
920 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
921 dev_dbg(dev->dev,
922 "The Tg found doesn't have the desired protocol\n");
923 return -EAGAIN;
926 dev_dbg(dev->dev,
927 "Target found - supported protocols: 0x%x\n",
928 nfc_tgt.supported_protocols);
930 dev->tgt_available_prots = nfc_tgt.supported_protocols;
932 pn533_poll_reset_mod_list(dev);
933 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
935 return 0;
938 static inline void pn533_poll_next_mod(struct pn533 *dev)
940 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
943 static void pn533_poll_reset_mod_list(struct pn533 *dev)
945 dev->poll_mod_count = 0;
948 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
950 dev->poll_mod_active[dev->poll_mod_count] =
951 (struct pn533_poll_modulations *)&poll_mod[mod_index];
952 dev->poll_mod_count++;
955 static void pn533_poll_create_mod_list(struct pn533 *dev,
956 u32 im_protocols, u32 tm_protocols)
958 pn533_poll_reset_mod_list(dev);
960 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
961 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
962 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
963 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
965 if (im_protocols & NFC_PROTO_FELICA_MASK ||
966 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
967 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
968 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
971 if (im_protocols & NFC_PROTO_JEWEL_MASK)
972 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
974 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
975 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
977 if (tm_protocols)
978 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
981 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
983 u8 nbtg, tg, *tgdata;
984 int rc, tgdata_len;
986 /* Toggle the DEP polling */
987 if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
988 dev->poll_dep = 1;
990 nbtg = resp->data[0];
991 tg = resp->data[1];
992 tgdata = &resp->data[2];
993 tgdata_len = resp->len - 2; /* nbtg + tg */
995 if (nbtg) {
996 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
998 /* We must stop the poll after a valid target found */
999 if (rc == 0)
1000 return 0;
1003 return -EAGAIN;
1006 static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1008 struct sk_buff *skb;
1009 u8 *felica, *nfcid3;
1011 u8 *gbytes = dev->gb;
1012 size_t gbytes_len = dev->gb_len;
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 */
1019 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1020 0x0, 0x0, 0x0,
1021 0x40}; /* SEL_RES for DEP */
1023 unsigned int skb_len = 36 + /*
1024 * mode (1), mifare (6),
1025 * felica (18), nfcid3 (10), gb_len (1)
1027 gbytes_len +
1028 1; /* len Tk*/
1030 skb = pn533_alloc_skb(dev, skb_len);
1031 if (!skb)
1032 return NULL;
1034 /* DEP support only */
1035 skb_put_u8(skb, PN533_INIT_TARGET_DEP);
1037 /* MIFARE params */
1038 skb_put_data(skb, mifare_params, 6);
1040 /* Felica params */
1041 felica = skb_put_data(skb, felica_params, 18);
1042 get_random_bytes(felica + 2, 6);
1044 /* NFCID3 */
1045 nfcid3 = skb_put_zero(skb, 10);
1046 memcpy(nfcid3, felica, 8);
1048 /* General bytes */
1049 skb_put_u8(skb, gbytes_len);
1051 skb_put_data(skb, gbytes, gbytes_len);
1053 /* Len Tk */
1054 skb_put_u8(skb, 0);
1056 return skb;
1059 static void pn533_wq_tm_mi_recv(struct work_struct *work);
1060 static struct sk_buff *pn533_build_response(struct pn533 *dev);
1062 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1063 struct sk_buff *resp)
1065 struct sk_buff *skb;
1066 u8 status, ret, mi;
1067 int rc;
1069 dev_dbg(dev->dev, "%s\n", __func__);
1071 if (IS_ERR(resp)) {
1072 skb_queue_purge(&dev->resp_q);
1073 return PTR_ERR(resp);
1076 status = resp->data[0];
1078 ret = status & PN533_CMD_RET_MASK;
1079 mi = status & PN533_CMD_MI_MASK;
1081 skb_pull(resp, sizeof(status));
1083 if (ret != PN533_CMD_RET_SUCCESS) {
1084 rc = -EIO;
1085 goto error;
1088 skb_queue_tail(&dev->resp_q, resp);
1090 if (mi) {
1091 queue_work(dev->wq, &dev->mi_tm_rx_work);
1092 return -EINPROGRESS;
1095 skb = pn533_build_response(dev);
1096 if (!skb) {
1097 rc = -EIO;
1098 goto error;
1101 return nfc_tm_data_received(dev->nfc_dev, skb);
1103 error:
1104 nfc_tm_deactivated(dev->nfc_dev);
1105 dev->tgt_mode = 0;
1106 skb_queue_purge(&dev->resp_q);
1107 dev_kfree_skb(resp);
1109 return rc;
1112 static void pn533_wq_tm_mi_recv(struct work_struct *work)
1114 struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1115 struct sk_buff *skb;
1116 int rc;
1118 dev_dbg(dev->dev, "%s\n", __func__);
1120 skb = pn533_alloc_skb(dev, 0);
1121 if (!skb)
1122 return;
1124 rc = pn533_send_cmd_direct_async(dev,
1125 PN533_CMD_TG_GET_DATA,
1126 skb,
1127 pn533_tm_get_data_complete,
1128 NULL);
1130 if (rc < 0)
1131 dev_kfree_skb(skb);
1134 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1135 struct sk_buff *resp);
1136 static void pn533_wq_tm_mi_send(struct work_struct *work)
1138 struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1139 struct sk_buff *skb;
1140 int rc;
1142 dev_dbg(dev->dev, "%s\n", __func__);
1144 /* Grab the first skb in the queue */
1145 skb = skb_dequeue(&dev->fragment_skb);
1146 if (skb == NULL) { /* No more data */
1147 /* Reset the queue for future use */
1148 skb_queue_head_init(&dev->fragment_skb);
1149 goto error;
1152 /* last entry - remove MI bit */
1153 if (skb_queue_len(&dev->fragment_skb) == 0) {
1154 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1155 skb, pn533_tm_send_complete, NULL);
1156 } else
1157 rc = pn533_send_cmd_direct_async(dev,
1158 PN533_CMD_TG_SET_META_DATA,
1159 skb, pn533_tm_send_complete, NULL);
1161 if (rc == 0) /* success */
1162 return;
1164 dev_err(dev->dev,
1165 "Error %d when trying to perform set meta data_exchange", rc);
1167 dev_kfree_skb(skb);
1169 error:
1170 dev->phy_ops->send_ack(dev, GFP_KERNEL);
1171 queue_work(dev->wq, &dev->cmd_work);
1174 static void pn533_wq_tg_get_data(struct work_struct *work)
1176 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1177 struct sk_buff *skb;
1178 int rc;
1180 dev_dbg(dev->dev, "%s\n", __func__);
1182 skb = pn533_alloc_skb(dev, 0);
1183 if (!skb)
1184 return;
1186 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1187 pn533_tm_get_data_complete, NULL);
1189 if (rc < 0)
1190 dev_kfree_skb(skb);
1193 #define ATR_REQ_GB_OFFSET 17
1194 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1196 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1197 size_t gb_len;
1198 int rc;
1200 dev_dbg(dev->dev, "%s\n", __func__);
1202 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1203 return -EINVAL;
1205 mode = resp->data[0];
1206 cmd = &resp->data[1];
1208 dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
1209 mode, resp->len);
1211 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1212 PN533_INIT_TARGET_RESP_ACTIVE)
1213 comm_mode = NFC_COMM_ACTIVE;
1215 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1216 return -EOPNOTSUPP;
1218 gb = cmd + ATR_REQ_GB_OFFSET;
1219 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1221 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1222 comm_mode, gb, gb_len);
1223 if (rc < 0) {
1224 nfc_err(dev->dev,
1225 "Error when signaling target activation\n");
1226 return rc;
1229 dev->tgt_mode = 1;
1230 queue_work(dev->wq, &dev->tg_work);
1232 return 0;
1235 static void pn533_listen_mode_timer(struct timer_list *t)
1237 struct pn533 *dev = from_timer(dev, t, listen_timer);
1239 dev_dbg(dev->dev, "Listen mode timeout\n");
1241 dev->cancel_listen = 1;
1243 pn533_poll_next_mod(dev);
1245 queue_delayed_work(dev->wq, &dev->poll_work,
1246 msecs_to_jiffies(PN533_POLL_INTERVAL));
1249 static int pn533_rf_complete(struct pn533 *dev, void *arg,
1250 struct sk_buff *resp)
1252 int rc = 0;
1254 dev_dbg(dev->dev, "%s\n", __func__);
1256 if (IS_ERR(resp)) {
1257 rc = PTR_ERR(resp);
1259 nfc_err(dev->dev, "RF setting error %d\n", rc);
1261 return rc;
1264 queue_delayed_work(dev->wq, &dev->poll_work,
1265 msecs_to_jiffies(PN533_POLL_INTERVAL));
1267 dev_kfree_skb(resp);
1268 return rc;
1271 static void pn533_wq_rf(struct work_struct *work)
1273 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1274 struct sk_buff *skb;
1275 int rc;
1277 dev_dbg(dev->dev, "%s\n", __func__);
1279 skb = pn533_alloc_skb(dev, 2);
1280 if (!skb)
1281 return;
1283 skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
1284 skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
1286 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1287 pn533_rf_complete, NULL);
1288 if (rc < 0) {
1289 dev_kfree_skb(skb);
1290 nfc_err(dev->dev, "RF setting error %d\n", rc);
1294 static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1295 struct sk_buff *resp)
1297 struct pn533_cmd_jump_dep_response *rsp;
1298 struct nfc_target nfc_target;
1299 u8 target_gt_len;
1300 int rc;
1302 if (IS_ERR(resp))
1303 return PTR_ERR(resp);
1305 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1307 rc = rsp->status & PN533_CMD_RET_MASK;
1308 if (rc != PN533_CMD_RET_SUCCESS) {
1309 /* Not target found, turn radio off */
1310 queue_work(dev->wq, &dev->rf_work);
1312 dev_kfree_skb(resp);
1313 return 0;
1316 dev_dbg(dev->dev, "Creating new target");
1318 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1319 nfc_target.nfcid1_len = 10;
1320 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1321 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1322 if (rc)
1323 goto error;
1325 dev->tgt_available_prots = 0;
1326 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1328 /* ATR_RES general bytes are located at offset 17 */
1329 target_gt_len = resp->len - 17;
1330 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1331 rsp->gt, target_gt_len);
1332 if (!rc) {
1333 rc = nfc_dep_link_is_up(dev->nfc_dev,
1334 dev->nfc_dev->targets[0].idx,
1335 0, NFC_RF_INITIATOR);
1337 if (!rc)
1338 pn533_poll_reset_mod_list(dev);
1340 error:
1341 dev_kfree_skb(resp);
1342 return rc;
1345 #define PASSIVE_DATA_LEN 5
1346 static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1348 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1349 struct sk_buff *skb;
1350 int rc, skb_len;
1351 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1352 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1354 dev_dbg(dev->dev, "%s", __func__);
1356 if (!dev->gb) {
1357 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1359 if (!dev->gb || !dev->gb_len) {
1360 dev->poll_dep = 0;
1361 queue_work(dev->wq, &dev->rf_work);
1365 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1366 skb_len += PASSIVE_DATA_LEN;
1368 /* NFCID3 */
1369 skb_len += NFC_NFCID3_MAXSIZE;
1370 nfcid3[0] = 0x1;
1371 nfcid3[1] = 0xfe;
1372 get_random_bytes(nfcid3 + 2, 6);
1374 skb = pn533_alloc_skb(dev, skb_len);
1375 if (!skb)
1376 return -ENOMEM;
1378 skb_put_u8(skb, 0x01); /* Active */
1379 skb_put_u8(skb, 0x02); /* 424 kbps */
1381 next = skb_put(skb, 1); /* Next */
1382 *next = 0;
1384 /* Copy passive data */
1385 skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1386 *next |= 1;
1388 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1389 skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1390 *next |= 2;
1392 skb_put_data(skb, dev->gb, dev->gb_len);
1393 *next |= 4; /* We have some Gi */
1395 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1396 pn533_poll_dep_complete, NULL);
1398 if (rc < 0)
1399 dev_kfree_skb(skb);
1401 return rc;
1404 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1405 struct sk_buff *resp)
1407 struct pn533_poll_modulations *cur_mod;
1408 int rc;
1410 dev_dbg(dev->dev, "%s\n", __func__);
1412 if (IS_ERR(resp)) {
1413 rc = PTR_ERR(resp);
1415 nfc_err(dev->dev, "%s Poll complete error %d\n",
1416 __func__, rc);
1418 if (rc == -ENOENT) {
1419 if (dev->poll_mod_count != 0)
1420 return rc;
1421 goto stop_poll;
1422 } else if (rc < 0) {
1423 nfc_err(dev->dev,
1424 "Error %d when running poll\n", rc);
1425 goto stop_poll;
1429 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1431 if (cur_mod->len == 0) { /* Target mode */
1432 del_timer(&dev->listen_timer);
1433 rc = pn533_init_target_complete(dev, resp);
1434 goto done;
1437 /* Initiator mode */
1438 rc = pn533_start_poll_complete(dev, resp);
1439 if (!rc)
1440 goto done;
1442 if (!dev->poll_mod_count) {
1443 dev_dbg(dev->dev, "Polling has been stopped\n");
1444 goto done;
1447 pn533_poll_next_mod(dev);
1448 /* Not target found, turn radio off */
1449 queue_work(dev->wq, &dev->rf_work);
1451 done:
1452 dev_kfree_skb(resp);
1453 return rc;
1455 stop_poll:
1456 nfc_err(dev->dev, "Polling operation has been stopped\n");
1458 pn533_poll_reset_mod_list(dev);
1459 dev->poll_protocols = 0;
1460 return rc;
1463 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1464 struct pn533_poll_modulations *mod)
1466 struct sk_buff *skb;
1468 skb = pn533_alloc_skb(dev, mod->len);
1469 if (!skb)
1470 return NULL;
1472 skb_put_data(skb, &mod->data, mod->len);
1474 return skb;
1477 static int pn533_send_poll_frame(struct pn533 *dev)
1479 struct pn533_poll_modulations *mod;
1480 struct sk_buff *skb;
1481 int rc;
1482 u8 cmd_code;
1484 mod = dev->poll_mod_active[dev->poll_mod_curr];
1486 dev_dbg(dev->dev, "%s mod len %d\n",
1487 __func__, mod->len);
1489 if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) {
1490 dev->poll_dep = 0;
1491 return pn533_poll_dep(dev->nfc_dev);
1494 if (mod->len == 0) { /* Listen mode */
1495 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1496 skb = pn533_alloc_poll_tg_frame(dev);
1497 } else { /* Polling mode */
1498 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1499 skb = pn533_alloc_poll_in_frame(dev, mod);
1502 if (!skb) {
1503 nfc_err(dev->dev, "Failed to allocate skb\n");
1504 return -ENOMEM;
1507 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1508 NULL);
1509 if (rc < 0) {
1510 dev_kfree_skb(skb);
1511 nfc_err(dev->dev, "Polling loop error %d\n", rc);
1514 return rc;
1517 static void pn533_wq_poll(struct work_struct *work)
1519 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
1520 struct pn533_poll_modulations *cur_mod;
1521 int rc;
1523 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1525 dev_dbg(dev->dev,
1526 "%s cancel_listen %d modulation len %d\n",
1527 __func__, dev->cancel_listen, cur_mod->len);
1529 if (dev->cancel_listen == 1) {
1530 dev->cancel_listen = 0;
1531 dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1534 rc = pn533_send_poll_frame(dev);
1535 if (rc)
1536 return;
1538 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1539 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1542 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1543 u32 im_protocols, u32 tm_protocols)
1545 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1546 struct pn533_poll_modulations *cur_mod;
1547 u8 rand_mod;
1548 int rc;
1550 dev_dbg(dev->dev,
1551 "%s: im protocols 0x%x tm protocols 0x%x\n",
1552 __func__, im_protocols, tm_protocols);
1554 if (dev->tgt_active_prot) {
1555 nfc_err(dev->dev,
1556 "Cannot poll with a target already activated\n");
1557 return -EBUSY;
1560 if (dev->tgt_mode) {
1561 nfc_err(dev->dev,
1562 "Cannot poll while already being activated\n");
1563 return -EBUSY;
1566 if (tm_protocols) {
1567 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1568 if (dev->gb == NULL)
1569 tm_protocols = 0;
1572 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1573 dev->poll_protocols = im_protocols;
1574 dev->listen_protocols = tm_protocols;
1576 /* Do not always start polling from the same modulation */
1577 get_random_bytes(&rand_mod, sizeof(rand_mod));
1578 rand_mod %= dev->poll_mod_count;
1579 dev->poll_mod_curr = rand_mod;
1581 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1583 rc = pn533_send_poll_frame(dev);
1585 /* Start listen timer */
1586 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
1587 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1589 return rc;
1592 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1594 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1596 del_timer(&dev->listen_timer);
1598 if (!dev->poll_mod_count) {
1599 dev_dbg(dev->dev,
1600 "Polling operation was not running\n");
1601 return;
1604 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1605 flush_delayed_work(&dev->poll_work);
1606 pn533_poll_reset_mod_list(dev);
1609 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1611 struct pn533_cmd_activate_response *rsp;
1612 u16 gt_len;
1613 int rc;
1614 struct sk_buff *skb;
1615 struct sk_buff *resp;
1617 dev_dbg(dev->dev, "%s\n", __func__);
1619 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1620 if (!skb)
1621 return -ENOMEM;
1623 skb_put_u8(skb, 1); /* TG */
1624 skb_put_u8(skb, 0); /* Next */
1626 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1627 if (IS_ERR(resp))
1628 return PTR_ERR(resp);
1630 rsp = (struct pn533_cmd_activate_response *)resp->data;
1631 rc = rsp->status & PN533_CMD_RET_MASK;
1632 if (rc != PN533_CMD_RET_SUCCESS) {
1633 nfc_err(dev->dev,
1634 "Target activation failed (error 0x%x)\n", rc);
1635 dev_kfree_skb(resp);
1636 return -EIO;
1639 /* ATR_RES general bytes are located at offset 16 */
1640 gt_len = resp->len - 16;
1641 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1643 dev_kfree_skb(resp);
1644 return rc;
1647 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1648 struct nfc_target *target, u32 protocol)
1650 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1651 int rc;
1653 dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1655 if (dev->poll_mod_count) {
1656 nfc_err(dev->dev,
1657 "Cannot activate while polling\n");
1658 return -EBUSY;
1661 if (dev->tgt_active_prot) {
1662 nfc_err(dev->dev,
1663 "There is already an active target\n");
1664 return -EBUSY;
1667 if (!dev->tgt_available_prots) {
1668 nfc_err(dev->dev,
1669 "There is no available target to activate\n");
1670 return -EINVAL;
1673 if (!(dev->tgt_available_prots & (1 << protocol))) {
1674 nfc_err(dev->dev,
1675 "Target doesn't support requested proto %u\n",
1676 protocol);
1677 return -EINVAL;
1680 if (protocol == NFC_PROTO_NFC_DEP) {
1681 rc = pn533_activate_target_nfcdep(dev);
1682 if (rc) {
1683 nfc_err(dev->dev,
1684 "Activating target with DEP failed %d\n", rc);
1685 return rc;
1689 dev->tgt_active_prot = protocol;
1690 dev->tgt_available_prots = 0;
1692 return 0;
1695 static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1696 struct sk_buff *resp)
1698 int rc = 0;
1700 dev_dbg(dev->dev, "%s\n", __func__);
1702 if (IS_ERR(resp)) {
1703 rc = PTR_ERR(resp);
1705 nfc_err(dev->dev, "Target release error %d\n", rc);
1707 return rc;
1710 rc = resp->data[0] & PN533_CMD_RET_MASK;
1711 if (rc != PN533_CMD_RET_SUCCESS)
1712 nfc_err(dev->dev,
1713 "Error 0x%x when releasing the target\n", rc);
1715 dev_kfree_skb(resp);
1716 return rc;
1719 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1720 struct nfc_target *target, u8 mode)
1722 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1723 struct sk_buff *skb;
1724 int rc;
1726 dev_dbg(dev->dev, "%s\n", __func__);
1728 if (!dev->tgt_active_prot) {
1729 nfc_err(dev->dev, "There is no active target\n");
1730 return;
1733 dev->tgt_active_prot = 0;
1734 skb_queue_purge(&dev->resp_q);
1736 skb = pn533_alloc_skb(dev, sizeof(u8));
1737 if (!skb)
1738 return;
1740 skb_put_u8(skb, 1); /* TG*/
1742 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
1743 pn533_deactivate_target_complete, NULL);
1744 if (rc < 0) {
1745 dev_kfree_skb(skb);
1746 nfc_err(dev->dev, "Target release error %d\n", rc);
1751 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1752 struct sk_buff *resp)
1754 struct pn533_cmd_jump_dep_response *rsp;
1755 u8 target_gt_len;
1756 int rc;
1757 u8 active = *(u8 *)arg;
1759 kfree(arg);
1761 if (IS_ERR(resp))
1762 return PTR_ERR(resp);
1764 if (dev->tgt_available_prots &&
1765 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1766 nfc_err(dev->dev,
1767 "The target does not support DEP\n");
1768 rc = -EINVAL;
1769 goto error;
1772 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1774 rc = rsp->status & PN533_CMD_RET_MASK;
1775 if (rc != PN533_CMD_RET_SUCCESS) {
1776 nfc_err(dev->dev,
1777 "Bringing DEP link up failed (error 0x%x)\n", rc);
1778 goto error;
1781 if (!dev->tgt_available_prots) {
1782 struct nfc_target nfc_target;
1784 dev_dbg(dev->dev, "Creating new target\n");
1786 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1787 nfc_target.nfcid1_len = 10;
1788 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1789 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1790 if (rc)
1791 goto error;
1793 dev->tgt_available_prots = 0;
1796 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1798 /* ATR_RES general bytes are located at offset 17 */
1799 target_gt_len = resp->len - 17;
1800 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1801 rsp->gt, target_gt_len);
1802 if (rc == 0)
1803 rc = nfc_dep_link_is_up(dev->nfc_dev,
1804 dev->nfc_dev->targets[0].idx,
1805 !active, NFC_RF_INITIATOR);
1807 error:
1808 dev_kfree_skb(resp);
1809 return rc;
1812 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1813 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1814 u8 comm_mode, u8 *gb, size_t gb_len)
1816 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1817 struct sk_buff *skb;
1818 int rc, skb_len;
1819 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1820 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1822 dev_dbg(dev->dev, "%s\n", __func__);
1824 if (dev->poll_mod_count) {
1825 nfc_err(dev->dev,
1826 "Cannot bring the DEP link up while polling\n");
1827 return -EBUSY;
1830 if (dev->tgt_active_prot) {
1831 nfc_err(dev->dev,
1832 "There is already an active target\n");
1833 return -EBUSY;
1836 skb_len = 3 + gb_len; /* ActPass + BR + Next */
1837 skb_len += PASSIVE_DATA_LEN;
1839 /* NFCID3 */
1840 skb_len += NFC_NFCID3_MAXSIZE;
1841 if (target && !target->nfcid2_len) {
1842 nfcid3[0] = 0x1;
1843 nfcid3[1] = 0xfe;
1844 get_random_bytes(nfcid3 + 2, 6);
1847 skb = pn533_alloc_skb(dev, skb_len);
1848 if (!skb)
1849 return -ENOMEM;
1851 skb_put_u8(skb, !comm_mode); /* ActPass */
1852 skb_put_u8(skb, 0x02); /* 424 kbps */
1854 next = skb_put(skb, 1); /* Next */
1855 *next = 0;
1857 /* Copy passive data */
1858 skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1859 *next |= 1;
1861 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1862 if (target && target->nfcid2_len)
1863 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
1864 target->nfcid2_len);
1865 else
1866 skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1867 *next |= 2;
1869 if (gb != NULL && gb_len > 0) {
1870 skb_put_data(skb, gb, gb_len);
1871 *next |= 4; /* We have some Gi */
1872 } else {
1873 *next = 0;
1876 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1877 if (!arg) {
1878 dev_kfree_skb(skb);
1879 return -ENOMEM;
1882 *arg = !comm_mode;
1884 pn533_rf_field(dev->nfc_dev, 0);
1886 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1887 pn533_in_dep_link_up_complete, arg);
1889 if (rc < 0) {
1890 dev_kfree_skb(skb);
1891 kfree(arg);
1894 return rc;
1897 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1899 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1901 dev_dbg(dev->dev, "%s\n", __func__);
1903 pn533_poll_reset_mod_list(dev);
1905 if (dev->tgt_mode || dev->tgt_active_prot)
1906 dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1908 dev->tgt_active_prot = 0;
1909 dev->tgt_mode = 0;
1911 skb_queue_purge(&dev->resp_q);
1913 return 0;
1916 struct pn533_data_exchange_arg {
1917 data_exchange_cb_t cb;
1918 void *cb_context;
1921 static struct sk_buff *pn533_build_response(struct pn533 *dev)
1923 struct sk_buff *skb, *tmp, *t;
1924 unsigned int skb_len = 0, tmp_len = 0;
1926 dev_dbg(dev->dev, "%s\n", __func__);
1928 if (skb_queue_empty(&dev->resp_q))
1929 return NULL;
1931 if (skb_queue_len(&dev->resp_q) == 1) {
1932 skb = skb_dequeue(&dev->resp_q);
1933 goto out;
1936 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1937 skb_len += tmp->len;
1939 dev_dbg(dev->dev, "%s total length %d\n",
1940 __func__, skb_len);
1942 skb = alloc_skb(skb_len, GFP_KERNEL);
1943 if (skb == NULL)
1944 goto out;
1946 skb_put(skb, skb_len);
1948 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1949 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1950 tmp_len += tmp->len;
1953 out:
1954 skb_queue_purge(&dev->resp_q);
1956 return skb;
1959 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1960 struct sk_buff *resp)
1962 struct pn533_data_exchange_arg *arg = _arg;
1963 struct sk_buff *skb;
1964 int rc = 0;
1965 u8 status, ret, mi;
1967 dev_dbg(dev->dev, "%s\n", __func__);
1969 if (IS_ERR(resp)) {
1970 rc = PTR_ERR(resp);
1971 goto _error;
1974 status = resp->data[0];
1975 ret = status & PN533_CMD_RET_MASK;
1976 mi = status & PN533_CMD_MI_MASK;
1978 skb_pull(resp, sizeof(status));
1980 if (ret != PN533_CMD_RET_SUCCESS) {
1981 nfc_err(dev->dev,
1982 "Exchanging data failed (error 0x%x)\n", ret);
1983 rc = -EIO;
1984 goto error;
1987 skb_queue_tail(&dev->resp_q, resp);
1989 if (mi) {
1990 dev->cmd_complete_mi_arg = arg;
1991 queue_work(dev->wq, &dev->mi_rx_work);
1992 return -EINPROGRESS;
1995 /* Prepare for the next round */
1996 if (skb_queue_len(&dev->fragment_skb) > 0) {
1997 dev->cmd_complete_dep_arg = arg;
1998 queue_work(dev->wq, &dev->mi_tx_work);
2000 return -EINPROGRESS;
2003 skb = pn533_build_response(dev);
2004 if (!skb) {
2005 rc = -ENOMEM;
2006 goto error;
2009 arg->cb(arg->cb_context, skb, 0);
2010 kfree(arg);
2011 return 0;
2013 error:
2014 dev_kfree_skb(resp);
2015 _error:
2016 skb_queue_purge(&dev->resp_q);
2017 arg->cb(arg->cb_context, NULL, rc);
2018 kfree(arg);
2019 return rc;
2023 * Receive an incoming pn533 frame. skb contains only header and payload.
2024 * If skb == NULL, it is a notification that the link below is dead.
2026 void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
2028 if (!dev->cmd)
2029 goto sched_wq;
2031 dev->cmd->status = status;
2033 if (status != 0) {
2034 dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
2035 goto sched_wq;
2038 if (skb == NULL) {
2039 pr_err("NULL Frame -> link is dead\n");
2040 goto sched_wq;
2043 if (pn533_rx_frame_is_ack(skb->data)) {
2044 dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2045 dev_kfree_skb(skb);
2046 return;
2049 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2050 dev->ops->rx_frame_size(skb->data), false);
2052 if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2053 nfc_err(dev->dev, "Received an invalid frame\n");
2054 dev->cmd->status = -EIO;
2055 } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2056 nfc_err(dev->dev, "It it not the response to the last command\n");
2057 dev->cmd->status = -EIO;
2060 dev->cmd->resp = skb;
2062 sched_wq:
2063 queue_work(dev->wq, &dev->cmd_complete_work);
2065 EXPORT_SYMBOL(pn533_recv_frame);
2067 /* Split the Tx skb into small chunks */
2068 static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2070 struct sk_buff *frag;
2071 int frag_size;
2073 do {
2074 /* Remaining size */
2075 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2076 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2077 else
2078 frag_size = skb->len;
2080 /* Allocate and reserve */
2081 frag = pn533_alloc_skb(dev, frag_size);
2082 if (!frag) {
2083 skb_queue_purge(&dev->fragment_skb);
2084 break;
2087 if (!dev->tgt_mode) {
2088 /* Reserve the TG/MI byte */
2089 skb_reserve(frag, 1);
2091 /* MI + TG */
2092 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2093 *(u8 *)skb_push(frag, sizeof(u8)) =
2094 (PN533_CMD_MI_MASK | 1);
2095 else
2096 *(u8 *)skb_push(frag, sizeof(u8)) = 1; /* TG */
2099 skb_put_data(frag, skb->data, frag_size);
2101 /* Reduce the size of incoming buffer */
2102 skb_pull(skb, frag_size);
2104 /* Add this to skb_queue */
2105 skb_queue_tail(&dev->fragment_skb, frag);
2107 } while (skb->len > 0);
2109 dev_kfree_skb(skb);
2111 return skb_queue_len(&dev->fragment_skb);
2114 static int pn533_transceive(struct nfc_dev *nfc_dev,
2115 struct nfc_target *target, struct sk_buff *skb,
2116 data_exchange_cb_t cb, void *cb_context)
2118 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2119 struct pn533_data_exchange_arg *arg = NULL;
2120 int rc;
2122 dev_dbg(dev->dev, "%s\n", __func__);
2124 if (!dev->tgt_active_prot) {
2125 nfc_err(dev->dev,
2126 "Can't exchange data if there is no active target\n");
2127 rc = -EINVAL;
2128 goto error;
2131 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2132 if (!arg) {
2133 rc = -ENOMEM;
2134 goto error;
2137 arg->cb = cb;
2138 arg->cb_context = cb_context;
2140 switch (dev->device_type) {
2141 case PN533_DEVICE_PASORI:
2142 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2143 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2144 skb,
2145 pn533_data_exchange_complete,
2146 arg);
2148 break;
2150 default:
2151 /* jumbo frame ? */
2152 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2153 rc = pn533_fill_fragment_skbs(dev, skb);
2154 if (rc <= 0)
2155 goto error;
2157 skb = skb_dequeue(&dev->fragment_skb);
2158 if (!skb) {
2159 rc = -EIO;
2160 goto error;
2162 } else {
2163 *(u8 *)skb_push(skb, sizeof(u8)) = 1; /* TG */
2166 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2167 skb, pn533_data_exchange_complete,
2168 arg);
2170 break;
2173 if (rc < 0) /* rc from send_async */
2174 goto error;
2176 return 0;
2178 error:
2179 kfree(arg);
2180 dev_kfree_skb(skb);
2181 return rc;
2184 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2185 struct sk_buff *resp)
2187 u8 status;
2189 dev_dbg(dev->dev, "%s\n", __func__);
2191 if (IS_ERR(resp))
2192 return PTR_ERR(resp);
2194 status = resp->data[0];
2196 /* Prepare for the next round */
2197 if (skb_queue_len(&dev->fragment_skb) > 0) {
2198 queue_work(dev->wq, &dev->mi_tm_tx_work);
2199 return -EINPROGRESS;
2201 dev_kfree_skb(resp);
2203 if (status != 0) {
2204 nfc_tm_deactivated(dev->nfc_dev);
2206 dev->tgt_mode = 0;
2208 return 0;
2211 queue_work(dev->wq, &dev->tg_work);
2213 return 0;
2216 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2218 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2219 int rc;
2221 dev_dbg(dev->dev, "%s\n", __func__);
2223 /* let's split in multiple chunks if size's too big */
2224 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2225 rc = pn533_fill_fragment_skbs(dev, skb);
2226 if (rc <= 0)
2227 goto error;
2229 /* get the first skb */
2230 skb = skb_dequeue(&dev->fragment_skb);
2231 if (!skb) {
2232 rc = -EIO;
2233 goto error;
2236 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2237 pn533_tm_send_complete, NULL);
2238 } else {
2239 /* Send th skb */
2240 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2241 pn533_tm_send_complete, NULL);
2244 error:
2245 if (rc < 0) {
2246 dev_kfree_skb(skb);
2247 skb_queue_purge(&dev->fragment_skb);
2250 return rc;
2253 static void pn533_wq_mi_recv(struct work_struct *work)
2255 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2256 struct sk_buff *skb;
2257 int rc;
2259 dev_dbg(dev->dev, "%s\n", __func__);
2261 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2262 if (!skb)
2263 goto error;
2265 switch (dev->device_type) {
2266 case PN533_DEVICE_PASORI:
2267 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2268 rc = pn533_send_cmd_direct_async(dev,
2269 PN533_CMD_IN_COMM_THRU,
2270 skb,
2271 pn533_data_exchange_complete,
2272 dev->cmd_complete_mi_arg);
2274 break;
2276 default:
2277 skb_put_u8(skb, 1); /*TG*/
2279 rc = pn533_send_cmd_direct_async(dev,
2280 PN533_CMD_IN_DATA_EXCHANGE,
2281 skb,
2282 pn533_data_exchange_complete,
2283 dev->cmd_complete_mi_arg);
2285 break;
2288 if (rc == 0) /* success */
2289 return;
2291 nfc_err(dev->dev,
2292 "Error %d when trying to perform data_exchange\n", rc);
2294 dev_kfree_skb(skb);
2295 kfree(dev->cmd_complete_mi_arg);
2297 error:
2298 dev->phy_ops->send_ack(dev, GFP_KERNEL);
2299 queue_work(dev->wq, &dev->cmd_work);
2302 static void pn533_wq_mi_send(struct work_struct *work)
2304 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2305 struct sk_buff *skb;
2306 int rc;
2308 dev_dbg(dev->dev, "%s\n", __func__);
2310 /* Grab the first skb in the queue */
2311 skb = skb_dequeue(&dev->fragment_skb);
2313 if (skb == NULL) { /* No more data */
2314 /* Reset the queue for future use */
2315 skb_queue_head_init(&dev->fragment_skb);
2316 goto error;
2319 switch (dev->device_type) {
2320 case PN533_DEVICE_PASORI:
2321 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2322 rc = -EIO;
2323 break;
2326 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2327 skb,
2328 pn533_data_exchange_complete,
2329 dev->cmd_complete_dep_arg);
2331 break;
2333 default:
2334 /* Still some fragments? */
2335 rc = pn533_send_cmd_direct_async(dev,
2336 PN533_CMD_IN_DATA_EXCHANGE,
2337 skb,
2338 pn533_data_exchange_complete,
2339 dev->cmd_complete_dep_arg);
2341 break;
2344 if (rc == 0) /* success */
2345 return;
2347 nfc_err(dev->dev,
2348 "Error %d when trying to perform data_exchange\n", rc);
2350 dev_kfree_skb(skb);
2351 kfree(dev->cmd_complete_dep_arg);
2353 error:
2354 dev->phy_ops->send_ack(dev, GFP_KERNEL);
2355 queue_work(dev->wq, &dev->cmd_work);
2358 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2359 u8 cfgdata_len)
2361 struct sk_buff *skb;
2362 struct sk_buff *resp;
2363 int skb_len;
2365 dev_dbg(dev->dev, "%s\n", __func__);
2367 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2369 skb = pn533_alloc_skb(dev, skb_len);
2370 if (!skb)
2371 return -ENOMEM;
2373 skb_put_u8(skb, cfgitem);
2374 skb_put_data(skb, cfgdata, cfgdata_len);
2376 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2377 if (IS_ERR(resp))
2378 return PTR_ERR(resp);
2380 dev_kfree_skb(resp);
2381 return 0;
2384 static int pn533_get_firmware_version(struct pn533 *dev,
2385 struct pn533_fw_version *fv)
2387 struct sk_buff *skb;
2388 struct sk_buff *resp;
2390 skb = pn533_alloc_skb(dev, 0);
2391 if (!skb)
2392 return -ENOMEM;
2394 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2395 if (IS_ERR(resp))
2396 return PTR_ERR(resp);
2398 fv->ic = resp->data[0];
2399 fv->ver = resp->data[1];
2400 fv->rev = resp->data[2];
2401 fv->support = resp->data[3];
2403 dev_kfree_skb(resp);
2404 return 0;
2407 static int pn533_pasori_fw_reset(struct pn533 *dev)
2409 struct sk_buff *skb;
2410 struct sk_buff *resp;
2412 dev_dbg(dev->dev, "%s\n", __func__);
2414 skb = pn533_alloc_skb(dev, sizeof(u8));
2415 if (!skb)
2416 return -ENOMEM;
2418 skb_put_u8(skb, 0x1);
2420 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2421 if (IS_ERR(resp))
2422 return PTR_ERR(resp);
2424 dev_kfree_skb(resp);
2426 return 0;
2429 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2431 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2432 u8 rf_field = !!rf;
2433 int rc;
2435 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2437 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2438 (u8 *)&rf_field, 1);
2439 if (rc) {
2440 nfc_err(dev->dev, "Error on setting RF field\n");
2441 return rc;
2444 return rc;
2447 static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2449 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2450 struct sk_buff *skb;
2451 struct sk_buff *resp;
2453 skb = pn533_alloc_skb(dev, 1);
2454 if (!skb)
2455 return -ENOMEM;
2457 skb_put_u8(skb, 0x01);
2459 resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2460 if (IS_ERR(resp))
2461 return PTR_ERR(resp);
2463 dev_kfree_skb(resp);
2464 return 0;
2467 static int pn533_dev_up(struct nfc_dev *nfc_dev)
2469 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2471 if (dev->device_type == PN533_DEVICE_PN532) {
2472 int rc = pn532_sam_configuration(nfc_dev);
2474 if (rc)
2475 return rc;
2478 return pn533_rf_field(nfc_dev, 1);
2481 static int pn533_dev_down(struct nfc_dev *nfc_dev)
2483 return pn533_rf_field(nfc_dev, 0);
2486 static struct nfc_ops pn533_nfc_ops = {
2487 .dev_up = pn533_dev_up,
2488 .dev_down = pn533_dev_down,
2489 .dep_link_up = pn533_dep_link_up,
2490 .dep_link_down = pn533_dep_link_down,
2491 .start_poll = pn533_start_poll,
2492 .stop_poll = pn533_stop_poll,
2493 .activate_target = pn533_activate_target,
2494 .deactivate_target = pn533_deactivate_target,
2495 .im_transceive = pn533_transceive,
2496 .tm_send = pn533_tm_send,
2499 static int pn533_setup(struct pn533 *dev)
2501 struct pn533_config_max_retries max_retries;
2502 struct pn533_config_timing timing;
2503 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2504 int rc;
2506 switch (dev->device_type) {
2507 case PN533_DEVICE_STD:
2508 case PN533_DEVICE_PASORI:
2509 case PN533_DEVICE_ACR122U:
2510 case PN533_DEVICE_PN532:
2511 max_retries.mx_rty_atr = 0x2;
2512 max_retries.mx_rty_psl = 0x1;
2513 max_retries.mx_rty_passive_act =
2514 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2516 timing.rfu = PN533_CONFIG_TIMING_102;
2517 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2518 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2520 break;
2522 default:
2523 nfc_err(dev->dev, "Unknown device type %d\n",
2524 dev->device_type);
2525 return -EINVAL;
2528 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2529 (u8 *)&max_retries, sizeof(max_retries));
2530 if (rc) {
2531 nfc_err(dev->dev,
2532 "Error on setting MAX_RETRIES config\n");
2533 return rc;
2537 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2538 (u8 *)&timing, sizeof(timing));
2539 if (rc) {
2540 nfc_err(dev->dev, "Error on setting RF timings\n");
2541 return rc;
2544 switch (dev->device_type) {
2545 case PN533_DEVICE_STD:
2546 case PN533_DEVICE_PN532:
2547 break;
2549 case PN533_DEVICE_PASORI:
2550 pn533_pasori_fw_reset(dev);
2552 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2553 pasori_cfg, 3);
2554 if (rc) {
2555 nfc_err(dev->dev,
2556 "Error while settings PASORI config\n");
2557 return rc;
2560 pn533_pasori_fw_reset(dev);
2562 break;
2565 return 0;
2568 int pn533_finalize_setup(struct pn533 *dev)
2571 struct pn533_fw_version fw_ver;
2572 int rc;
2574 memset(&fw_ver, 0, sizeof(fw_ver));
2576 rc = pn533_get_firmware_version(dev, &fw_ver);
2577 if (rc) {
2578 nfc_err(dev->dev, "Unable to get FW version\n");
2579 return rc;
2582 nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
2583 fw_ver.ic, fw_ver.ver, fw_ver.rev);
2585 rc = pn533_setup(dev);
2586 if (rc)
2587 return rc;
2589 return 0;
2591 EXPORT_SYMBOL_GPL(pn533_finalize_setup);
2593 struct pn533 *pn533_register_device(u32 device_type,
2594 u32 protocols,
2595 enum pn533_protocol_type protocol_type,
2596 void *phy,
2597 struct pn533_phy_ops *phy_ops,
2598 struct pn533_frame_ops *fops,
2599 struct device *dev,
2600 struct device *parent)
2602 struct pn533 *priv;
2603 int rc = -ENOMEM;
2605 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2606 if (!priv)
2607 return ERR_PTR(-ENOMEM);
2609 priv->phy = phy;
2610 priv->phy_ops = phy_ops;
2611 priv->dev = dev;
2612 if (fops != NULL)
2613 priv->ops = fops;
2614 else
2615 priv->ops = &pn533_std_frame_ops;
2617 priv->protocol_type = protocol_type;
2618 priv->device_type = device_type;
2620 mutex_init(&priv->cmd_lock);
2622 INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2623 INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2624 INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2625 INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2626 INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2627 INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2628 INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2629 INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2630 INIT_WORK(&priv->rf_work, pn533_wq_rf);
2631 priv->wq = alloc_ordered_workqueue("pn533", 0);
2632 if (priv->wq == NULL)
2633 goto error;
2635 timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0);
2637 skb_queue_head_init(&priv->resp_q);
2638 skb_queue_head_init(&priv->fragment_skb);
2640 INIT_LIST_HEAD(&priv->cmd_queue);
2642 priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2643 priv->ops->tx_header_len +
2644 PN533_CMD_DATAEXCH_HEAD_LEN,
2645 priv->ops->tx_tail_len);
2646 if (!priv->nfc_dev) {
2647 rc = -ENOMEM;
2648 goto destroy_wq;
2651 nfc_set_parent_dev(priv->nfc_dev, parent);
2652 nfc_set_drvdata(priv->nfc_dev, priv);
2654 rc = nfc_register_device(priv->nfc_dev);
2655 if (rc)
2656 goto free_nfc_dev;
2658 return priv;
2660 free_nfc_dev:
2661 nfc_free_device(priv->nfc_dev);
2663 destroy_wq:
2664 destroy_workqueue(priv->wq);
2665 error:
2666 kfree(priv);
2667 return ERR_PTR(rc);
2669 EXPORT_SYMBOL_GPL(pn533_register_device);
2671 void pn533_unregister_device(struct pn533 *priv)
2673 struct pn533_cmd *cmd, *n;
2675 nfc_unregister_device(priv->nfc_dev);
2676 nfc_free_device(priv->nfc_dev);
2678 flush_delayed_work(&priv->poll_work);
2679 destroy_workqueue(priv->wq);
2681 skb_queue_purge(&priv->resp_q);
2683 del_timer(&priv->listen_timer);
2685 list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
2686 list_del(&cmd->queue);
2687 kfree(cmd);
2690 kfree(priv);
2692 EXPORT_SYMBOL_GPL(pn533_unregister_device);
2695 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2696 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2697 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2698 MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2699 MODULE_VERSION(VERSION);
2700 MODULE_LICENSE("GPL");