2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/device.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/nfc.h>
30 #include <linux/netdevice.h>
31 #include <net/nfc/nfc.h>
35 #define PN533_VENDOR_ID 0x4CC
36 #define PN533_PRODUCT_ID 0x2533
38 #define SCM_VENDOR_ID 0x4E6
39 #define SCL3711_PRODUCT_ID 0x5591
41 static const struct usb_device_id pn533_table[] = {
42 { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID) },
43 { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID) },
46 MODULE_DEVICE_TABLE(usb, pn533_table);
48 /* frame definitions */
49 #define PN533_FRAME_TAIL_SIZE 2
50 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
51 PN533_FRAME_TAIL_SIZE)
52 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
53 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
54 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
57 #define PN533_SOF 0x00FF
59 /* frame identifier: in/out/error */
60 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
61 #define PN533_DIR_OUT 0xD4
62 #define PN533_DIR_IN 0xD5
65 #define PN533_FRAME_CMD(f) (f->data[1])
66 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
67 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
69 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
70 #define PN533_CMD_RF_CONFIGURATION 0x32
71 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
72 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
73 #define PN533_CMD_IN_ATR 0x50
74 #define PN533_CMD_IN_RELEASE 0x52
76 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
78 /* PN533 Return codes */
79 #define PN533_CMD_RET_MASK 0x3F
80 #define PN533_CMD_MI_MASK 0x40
81 #define PN533_CMD_RET_SUCCESS 0x00
85 typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
86 u8 *params, int params_len);
88 /* structs for pn533 commands */
90 /* PN533_CMD_GET_FIRMWARE_VERSION */
91 struct pn533_fw_version {
98 /* PN533_CMD_RF_CONFIGURATION */
99 #define PN533_CFGITEM_MAX_RETRIES 0x05
101 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
102 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
104 struct pn533_config_max_retries {
107 u8 mx_rty_passive_act;
110 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
112 /* felica commands opcode */
113 #define PN533_FELICA_OPC_SENSF_REQ 0
114 #define PN533_FELICA_OPC_SENSF_RES 1
115 /* felica SENSF_REQ parameters */
116 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
117 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
118 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
119 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
121 /* type B initiator_data values */
122 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
123 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
124 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
126 union pn533_cmd_poll_initdata {
139 /* Poll modulations */
141 PN533_POLL_MOD_106KBPS_A,
142 PN533_POLL_MOD_212KBPS_FELICA,
143 PN533_POLL_MOD_424KBPS_FELICA,
144 PN533_POLL_MOD_106KBPS_JEWEL,
145 PN533_POLL_MOD_847KBPS_B,
147 __PN533_POLL_MOD_AFTER_LAST,
149 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
151 struct pn533_poll_modulations {
155 union pn533_cmd_poll_initdata initiator_data;
160 const struct pn533_poll_modulations poll_mod[] = {
161 [PN533_POLL_MOD_106KBPS_A] = {
168 [PN533_POLL_MOD_212KBPS_FELICA] = {
172 .initiator_data.felica = {
173 .opcode = PN533_FELICA_OPC_SENSF_REQ,
174 .sc = PN533_FELICA_SENSF_SC_ALL,
175 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
181 [PN533_POLL_MOD_424KBPS_FELICA] = {
185 .initiator_data.felica = {
186 .opcode = PN533_FELICA_OPC_SENSF_REQ,
187 .sc = PN533_FELICA_SENSF_SC_ALL,
188 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
194 [PN533_POLL_MOD_106KBPS_JEWEL] = {
201 [PN533_POLL_MOD_847KBPS_B] = {
205 .initiator_data.type_b = {
206 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
208 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
215 /* PN533_CMD_IN_ATR */
217 struct pn533_cmd_activate_param {
222 struct pn533_cmd_activate_response {
236 struct usb_device *udev;
237 struct usb_interface *interface;
238 struct nfc_dev *nfc_dev;
242 struct pn533_frame *out_frame;
246 struct pn533_frame *in_frame;
248 struct tasklet_struct tasklet;
249 struct pn533_frame *tklt_in_frame;
252 pn533_cmd_complete_t cmd_complete;
253 void *cmd_complete_arg;
254 struct semaphore cmd_lock;
257 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
262 u8 tgt_available_prots;
274 /* The rule: value + checksum = 0 */
275 static inline u8 pn533_checksum(u8 value)
280 /* The rule: sum(data elements) + checksum = 0 */
281 static u8 pn533_data_checksum(u8 *data, int datalen)
286 for (i = 0; i < datalen; i++)
289 return pn533_checksum(sum);
293 * pn533_tx_frame_ack - create a ack frame
294 * @frame: The frame to be set as ack
296 * Ack is different type of standard frame. As a standard frame, it has
297 * preamble and start_frame. However the checksum of this frame must fail,
298 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
299 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
300 * After datalen_checksum field, the postamble is placed.
302 static void pn533_tx_frame_ack(struct pn533_frame *frame)
305 frame->start_frame = cpu_to_be16(PN533_SOF);
307 frame->datalen_checksum = 0xFF;
308 /* data[0] is used as postamble */
312 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
315 frame->start_frame = cpu_to_be16(PN533_SOF);
316 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
317 PN533_FRAME_CMD(frame) = cmd;
321 static void pn533_tx_frame_finish(struct pn533_frame *frame)
323 frame->datalen_checksum = pn533_checksum(frame->datalen);
325 PN533_FRAME_CHECKSUM(frame) =
326 pn533_data_checksum(frame->data, frame->datalen);
328 PN533_FRAME_POSTAMBLE(frame) = 0;
331 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
335 if (frame->start_frame != cpu_to_be16(PN533_SOF))
338 checksum = pn533_checksum(frame->datalen);
339 if (checksum != frame->datalen_checksum)
342 checksum = pn533_data_checksum(frame->data, frame->datalen);
343 if (checksum != PN533_FRAME_CHECKSUM(frame))
349 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
351 if (frame->start_frame != cpu_to_be16(PN533_SOF))
354 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
360 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
362 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
365 static void pn533_tasklet_cmd_complete(unsigned long arg)
367 struct pn533 *dev = (struct pn533 *) arg;
368 struct pn533_frame *in_frame = dev->tklt_in_frame;
371 if (dev->tklt_in_error)
372 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
375 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
376 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
377 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
379 if (rc != -EINPROGRESS)
383 static void pn533_recv_response(struct urb *urb)
385 struct pn533 *dev = urb->context;
386 struct pn533_frame *in_frame;
388 dev->tklt_in_frame = NULL;
390 switch (urb->status) {
397 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
398 " status: %d", urb->status);
399 dev->tklt_in_error = urb->status;
402 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
404 dev->tklt_in_error = urb->status;
408 in_frame = dev->in_urb->transfer_buffer;
410 if (!pn533_rx_frame_is_valid(in_frame)) {
411 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
412 dev->tklt_in_error = -EIO;
416 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
417 nfc_dev_err(&dev->interface->dev, "The received frame is not "
418 "response to the last command");
419 dev->tklt_in_error = -EIO;
423 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
424 dev->tklt_in_error = 0;
425 dev->tklt_in_frame = in_frame;
428 tasklet_schedule(&dev->tasklet);
431 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
433 dev->in_urb->complete = pn533_recv_response;
435 return usb_submit_urb(dev->in_urb, flags);
438 static void pn533_recv_ack(struct urb *urb)
440 struct pn533 *dev = urb->context;
441 struct pn533_frame *in_frame;
444 switch (urb->status) {
451 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
452 " status: %d", urb->status);
453 dev->tklt_in_error = urb->status;
456 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
458 dev->tklt_in_error = urb->status;
462 in_frame = dev->in_urb->transfer_buffer;
464 if (!pn533_rx_frame_is_ack(in_frame)) {
465 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
466 dev->tklt_in_error = -EIO;
470 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
472 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
474 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
476 dev->tklt_in_error = rc;
483 dev->tklt_in_frame = NULL;
484 tasklet_schedule(&dev->tasklet);
487 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
489 dev->in_urb->complete = pn533_recv_ack;
491 return usb_submit_urb(dev->in_urb, flags);
494 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
498 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
500 pn533_tx_frame_ack(dev->out_frame);
502 dev->out_urb->transfer_buffer = dev->out_frame;
503 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
504 rc = usb_submit_urb(dev->out_urb, flags);
509 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
510 struct pn533_frame *out_frame,
511 struct pn533_frame *in_frame,
513 pn533_cmd_complete_t cmd_complete,
514 void *arg, gfp_t flags)
518 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
519 PN533_FRAME_CMD(out_frame));
521 dev->cmd = PN533_FRAME_CMD(out_frame);
522 dev->cmd_complete = cmd_complete;
523 dev->cmd_complete_arg = arg;
525 dev->out_urb->transfer_buffer = out_frame;
526 dev->out_urb->transfer_buffer_length =
527 PN533_FRAME_SIZE(out_frame);
529 dev->in_urb->transfer_buffer = in_frame;
530 dev->in_urb->transfer_buffer_length = in_frame_len;
532 rc = usb_submit_urb(dev->out_urb, flags);
536 rc = pn533_submit_urb_for_ack(dev, flags);
543 usb_unlink_urb(dev->out_urb);
547 static int pn533_send_cmd_frame_async(struct pn533 *dev,
548 struct pn533_frame *out_frame,
549 struct pn533_frame *in_frame,
551 pn533_cmd_complete_t cmd_complete,
552 void *arg, gfp_t flags)
556 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
558 if (down_trylock(&dev->cmd_lock))
561 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
562 in_frame_len, cmd_complete, arg, flags);
572 struct pn533_sync_cmd_response {
574 struct completion done;
577 static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
578 u8 *params, int params_len)
580 struct pn533_sync_cmd_response *arg = _arg;
582 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
586 if (params_len < 0) /* error */
587 arg->rc = params_len;
589 complete(&arg->done);
594 static int pn533_send_cmd_frame_sync(struct pn533 *dev,
595 struct pn533_frame *out_frame,
596 struct pn533_frame *in_frame,
600 struct pn533_sync_cmd_response arg;
602 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
604 init_completion(&arg.done);
606 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
607 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
611 wait_for_completion(&arg.done);
616 static void pn533_send_complete(struct urb *urb)
618 struct pn533 *dev = urb->context;
620 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
622 switch (urb->status) {
629 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
630 " status: %d", urb->status);
633 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
638 struct pn533_target_type_a {
646 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
647 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
648 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
650 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
651 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
653 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
654 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
656 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
657 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
658 #define PN533_TYPE_A_SEL_PROT_DEP 2
659 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
661 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
667 if (target_data_len < sizeof(struct pn533_target_type_a))
670 /* The lenght check of nfcid[] and ats[] are not being performed because
671 the values are not being used */
673 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
674 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
675 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
677 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
678 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
679 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
680 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
683 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
684 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
690 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
693 struct pn533_target_type_a *tgt_type_a;
695 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
697 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
700 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
701 case PN533_TYPE_A_SEL_PROT_MIFARE:
702 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
704 case PN533_TYPE_A_SEL_PROT_ISO14443:
705 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
707 case PN533_TYPE_A_SEL_PROT_DEP:
708 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
710 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
711 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
712 NFC_PROTO_NFC_DEP_MASK;
716 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
717 nfc_tgt->sel_res = tgt_type_a->sel_res;
722 struct pn533_target_felica {
731 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
732 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
734 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
737 if (target_data_len < sizeof(struct pn533_target_felica))
740 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
746 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
749 struct pn533_target_felica *tgt_felica;
751 tgt_felica = (struct pn533_target_felica *) tgt_data;
753 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
756 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
757 tgt_felica->nfcid2[1] ==
758 PN533_FELICA_SENSF_NFCID2_DEP_B2)
759 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
761 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
766 struct pn533_target_jewel {
771 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
777 if (target_data_len < sizeof(struct pn533_target_jewel))
780 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
781 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
782 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
784 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
785 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
786 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
787 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
793 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
796 struct pn533_target_jewel *tgt_jewel;
798 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
800 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
803 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
804 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
809 struct pn533_type_b_prot_info {
815 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
816 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
817 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
819 struct pn533_type_b_sens_res {
823 struct pn533_type_b_prot_info prot_info;
826 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
828 struct pn533_target_type_b {
829 struct pn533_type_b_sens_res sensb_res;
834 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
837 if (target_data_len < sizeof(struct pn533_target_type_b))
840 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
843 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
844 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
850 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
853 struct pn533_target_type_b *tgt_type_b;
855 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
857 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
860 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
865 struct pn533_poll_response {
871 static int pn533_target_found(struct pn533 *dev,
872 struct pn533_poll_response *resp, int resp_len)
875 struct nfc_target nfc_tgt;
878 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
884 target_data_len = resp_len - sizeof(struct pn533_poll_response);
886 switch (dev->poll_mod_curr) {
887 case PN533_POLL_MOD_106KBPS_A:
888 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
891 case PN533_POLL_MOD_212KBPS_FELICA:
892 case PN533_POLL_MOD_424KBPS_FELICA:
893 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
896 case PN533_POLL_MOD_106KBPS_JEWEL:
897 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
900 case PN533_POLL_MOD_847KBPS_B:
901 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
905 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
913 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
914 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
915 " have the desired protocol");
919 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
920 "0x%x", nfc_tgt.supported_protocols);
922 dev->tgt_available_prots = nfc_tgt.supported_protocols;
924 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
929 static void pn533_poll_reset_mod_list(struct pn533 *dev)
931 dev->poll_mod_count = 0;
934 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
936 dev->poll_mod_active[dev->poll_mod_count] =
937 (struct pn533_poll_modulations *) &poll_mod[mod_index];
938 dev->poll_mod_count++;
941 static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
943 pn533_poll_reset_mod_list(dev);
945 if (protocols & NFC_PROTO_MIFARE_MASK
946 || protocols & NFC_PROTO_ISO14443_MASK
947 || protocols & NFC_PROTO_NFC_DEP_MASK)
948 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
950 if (protocols & NFC_PROTO_FELICA_MASK
951 || protocols & NFC_PROTO_NFC_DEP_MASK) {
952 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
953 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
956 if (protocols & NFC_PROTO_JEWEL_MASK)
957 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
959 if (protocols & NFC_PROTO_ISO14443_MASK)
960 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
963 static void pn533_start_poll_frame(struct pn533_frame *frame,
964 struct pn533_poll_modulations *mod)
967 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
969 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
970 frame->datalen += mod->len;
972 pn533_tx_frame_finish(frame);
975 static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
976 u8 *params, int params_len)
978 struct pn533_poll_response *resp;
979 struct pn533_poll_modulations *next_mod;
982 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
984 if (params_len == -ENOENT) {
985 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
990 if (params_len < 0) {
991 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
996 resp = (struct pn533_poll_response *) params;
998 rc = pn533_target_found(dev, resp, params_len);
1000 /* We must stop the poll after a valid target found */
1005 nfc_dev_err(&dev->interface->dev, "The target found is"
1006 " not valid - continuing to poll");
1009 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1011 next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1013 nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1014 dev->poll_mod_curr);
1016 pn533_start_poll_frame(dev->out_frame, next_mod);
1018 /* Don't need to down the semaphore again */
1019 rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1020 dev->in_maxlen, pn533_start_poll_complete,
1024 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1025 " because poll has been stopped");
1030 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1031 " next modulation", rc);
1035 /* Inform caller function to do not up the semaphore */
1036 return -EINPROGRESS;
1039 pn533_poll_reset_mod_list(dev);
1040 dev->poll_protocols = 0;
1044 static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
1046 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1047 struct pn533_poll_modulations *start_mod;
1050 nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
1053 if (dev->poll_mod_count) {
1054 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1059 if (dev->tgt_active_prot) {
1060 nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
1061 " already activated");
1065 pn533_poll_create_mod_list(dev, protocols);
1067 if (!dev->poll_mod_count) {
1068 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1074 nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1075 dev->poll_mod_count);
1077 dev->poll_mod_curr = 0;
1078 start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1080 pn533_start_poll_frame(dev->out_frame, start_mod);
1082 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1083 dev->in_maxlen, pn533_start_poll_complete,
1087 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1092 dev->poll_protocols = protocols;
1097 pn533_poll_reset_mod_list(dev);
1101 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1103 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1105 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1107 if (!dev->poll_mod_count) {
1108 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1113 /* An ack will cancel the last issued command (poll) */
1114 pn533_send_ack(dev, GFP_KERNEL);
1116 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1117 usb_kill_urb(dev->in_urb);
1120 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1122 struct pn533_cmd_activate_param param;
1123 struct pn533_cmd_activate_response *resp;
1126 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1128 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1132 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), ¶m,
1133 sizeof(struct pn533_cmd_activate_param));
1134 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1136 pn533_tx_frame_finish(dev->out_frame);
1138 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1143 resp = (struct pn533_cmd_activate_response *)
1144 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1145 rc = resp->status & PN533_CMD_RET_MASK;
1146 if (rc != PN533_CMD_RET_SUCCESS)
1152 static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
1155 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1158 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1161 if (dev->poll_mod_count) {
1162 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1167 if (dev->tgt_active_prot) {
1168 nfc_dev_err(&dev->interface->dev, "There is already an active"
1173 if (!dev->tgt_available_prots) {
1174 nfc_dev_err(&dev->interface->dev, "There is no available target"
1179 if (!(dev->tgt_available_prots & (1 << protocol))) {
1180 nfc_dev_err(&dev->interface->dev, "The target does not support"
1181 " the requested protocol %u", protocol);
1185 if (protocol == NFC_PROTO_NFC_DEP) {
1186 rc = pn533_activate_target_nfcdep(dev);
1188 nfc_dev_err(&dev->interface->dev, "Error %d when"
1189 " activating target with"
1190 " NFC_DEP protocol", rc);
1195 dev->tgt_active_prot = protocol;
1196 dev->tgt_available_prots = 0;
1201 static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
1203 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1208 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1210 if (!dev->tgt_active_prot) {
1211 nfc_dev_err(&dev->interface->dev, "There is no active target");
1215 dev->tgt_active_prot = 0;
1217 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1220 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1221 dev->out_frame->datalen += sizeof(u8);
1223 pn533_tx_frame_finish(dev->out_frame);
1225 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1228 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1229 " command to the controller");
1233 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1234 rc = status & PN533_CMD_RET_MASK;
1235 if (rc != PN533_CMD_RET_SUCCESS)
1236 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1242 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1243 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1245 static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
1247 int payload_len = skb->len;
1248 struct pn533_frame *out_frame;
1251 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1254 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1255 /* TODO: Implement support to multi-part data exchange */
1256 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1258 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1262 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1263 out_frame = (struct pn533_frame *) skb->data;
1265 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1268 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1269 out_frame->datalen += sizeof(u8);
1271 /* The data is already in the out_frame, just update the datalen */
1272 out_frame->datalen += payload_len;
1274 pn533_tx_frame_finish(out_frame);
1275 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1280 struct pn533_data_exchange_arg {
1281 struct sk_buff *skb_resp;
1282 struct sk_buff *skb_out;
1283 data_exchange_cb_t cb;
1287 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1288 u8 *params, int params_len)
1290 struct pn533_data_exchange_arg *arg = _arg;
1291 struct sk_buff *skb_resp = arg->skb_resp;
1292 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1297 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1299 dev_kfree_skb_irq(arg->skb_out);
1301 if (params_len < 0) { /* error */
1306 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1310 cmd_ret = status & PN533_CMD_RET_MASK;
1311 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1312 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1313 " exchanging data", cmd_ret);
1318 if (status & PN533_CMD_MI_MASK) {
1319 /* TODO: Implement support to multi-part data exchange */
1320 nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
1322 /* Prevent the other messages from controller */
1323 pn533_send_ack(dev, GFP_ATOMIC);
1328 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1329 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1331 arg->cb(arg->cb_context, skb_resp, 0);
1336 dev_kfree_skb_irq(skb_resp);
1337 arg->cb(arg->cb_context, NULL, err);
1342 int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
1343 struct sk_buff *skb,
1344 data_exchange_cb_t cb,
1347 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1348 struct pn533_frame *out_frame, *in_frame;
1349 struct pn533_data_exchange_arg *arg;
1350 struct sk_buff *skb_resp;
1354 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1356 if (!dev->tgt_active_prot) {
1357 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1358 " there is no active target");
1363 rc = pn533_data_exchange_tx_frame(dev, skb);
1367 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1368 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1369 PN533_FRAME_TAIL_SIZE;
1371 skb_resp = nfc_alloc_skb(skb_resp_len, GFP_KERNEL);
1377 in_frame = (struct pn533_frame *) skb_resp->data;
1378 out_frame = (struct pn533_frame *) skb->data;
1380 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1386 arg->skb_resp = skb_resp;
1389 arg->cb_context = cb_context;
1391 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1392 pn533_data_exchange_complete, arg,
1395 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1396 " perform data_exchange", rc);
1405 kfree_skb(skb_resp);
1411 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1417 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1419 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1421 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1422 params[0] = cfgitem;
1423 memcpy(¶ms[1], cfgdata, cfgdata_len);
1424 dev->out_frame->datalen += (1 + cfgdata_len);
1426 pn533_tx_frame_finish(dev->out_frame);
1428 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1434 struct nfc_ops pn533_nfc_ops = {
1437 .start_poll = pn533_start_poll,
1438 .stop_poll = pn533_stop_poll,
1439 .activate_target = pn533_activate_target,
1440 .deactivate_target = pn533_deactivate_target,
1441 .data_exchange = pn533_data_exchange,
1444 static int pn533_probe(struct usb_interface *interface,
1445 const struct usb_device_id *id)
1447 struct pn533_fw_version *fw_ver;
1449 struct usb_host_interface *iface_desc;
1450 struct usb_endpoint_descriptor *endpoint;
1451 struct pn533_config_max_retries max_retries;
1452 int in_endpoint = 0;
1453 int out_endpoint = 0;
1458 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1462 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1463 dev->interface = interface;
1464 sema_init(&dev->cmd_lock, 1);
1466 iface_desc = interface->cur_altsetting;
1467 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1468 endpoint = &iface_desc->endpoint[i].desc;
1470 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1471 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
1472 in_endpoint = endpoint->bEndpointAddress;
1475 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
1477 le16_to_cpu(endpoint->wMaxPacketSize);
1478 out_endpoint = endpoint->bEndpointAddress;
1482 if (!in_endpoint || !out_endpoint) {
1483 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
1484 " bulk-out endpoint");
1489 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
1490 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1491 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
1492 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1494 if (!dev->in_frame || !dev->out_frame ||
1495 !dev->in_urb || !dev->out_urb)
1498 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1499 usb_rcvbulkpipe(dev->udev, in_endpoint),
1500 NULL, 0, NULL, dev);
1501 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1502 usb_sndbulkpipe(dev->udev, out_endpoint),
1504 pn533_send_complete, dev);
1506 tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
1508 usb_set_intfdata(interface, dev);
1510 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
1511 pn533_tx_frame_finish(dev->out_frame);
1513 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1518 fw_ver = (struct pn533_fw_version *)
1519 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1520 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
1521 " attached", fw_ver->ver, fw_ver->rev);
1523 protocols = NFC_PROTO_JEWEL_MASK
1524 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
1525 | NFC_PROTO_ISO14443_MASK
1526 | NFC_PROTO_NFC_DEP_MASK;
1528 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
1529 PN533_CMD_DATAEXCH_HEAD_LEN,
1530 PN533_FRAME_TAIL_SIZE);
1534 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
1535 nfc_set_drvdata(dev->nfc_dev, dev);
1537 rc = nfc_register_device(dev->nfc_dev);
1541 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
1542 max_retries.mx_rty_psl = 2;
1543 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
1545 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
1546 (u8 *) &max_retries, sizeof(max_retries));
1549 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
1557 nfc_free_device(dev->nfc_dev);
1559 tasklet_kill(&dev->tasklet);
1561 kfree(dev->in_frame);
1562 usb_free_urb(dev->in_urb);
1563 kfree(dev->out_frame);
1564 usb_free_urb(dev->out_urb);
1569 static void pn533_disconnect(struct usb_interface *interface)
1573 dev = usb_get_intfdata(interface);
1574 usb_set_intfdata(interface, NULL);
1576 nfc_unregister_device(dev->nfc_dev);
1577 nfc_free_device(dev->nfc_dev);
1579 usb_kill_urb(dev->in_urb);
1580 usb_kill_urb(dev->out_urb);
1582 tasklet_kill(&dev->tasklet);
1584 kfree(dev->in_frame);
1585 usb_free_urb(dev->in_urb);
1586 kfree(dev->out_frame);
1587 usb_free_urb(dev->out_urb);
1590 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
1593 static struct usb_driver pn533_driver = {
1595 .probe = pn533_probe,
1596 .disconnect = pn533_disconnect,
1597 .id_table = pn533_table,
1600 static int __init pn533_init(void)
1604 rc = usb_register(&pn533_driver);
1606 err("usb_register failed. Error number %d", rc);
1611 static void __exit pn533_exit(void)
1613 usb_deregister(&pn533_driver);
1616 module_init(pn533_init);
1617 module_exit(pn533_exit);
1619 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1620 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1621 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
1622 MODULE_VERSION(VERSION);
1623 MODULE_LICENSE("GPL");