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
75 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
77 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
79 /* PN533 Return codes */
80 #define PN533_CMD_RET_MASK 0x3F
81 #define PN533_CMD_MI_MASK 0x40
82 #define PN533_CMD_RET_SUCCESS 0x00
86 typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
87 u8 *params, int params_len);
89 /* structs for pn533 commands */
91 /* PN533_CMD_GET_FIRMWARE_VERSION */
92 struct pn533_fw_version {
99 /* PN533_CMD_RF_CONFIGURATION */
100 #define PN533_CFGITEM_MAX_RETRIES 0x05
102 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
103 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
105 struct pn533_config_max_retries {
108 u8 mx_rty_passive_act;
111 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
113 /* felica commands opcode */
114 #define PN533_FELICA_OPC_SENSF_REQ 0
115 #define PN533_FELICA_OPC_SENSF_RES 1
116 /* felica SENSF_REQ parameters */
117 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
118 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
119 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
120 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
122 /* type B initiator_data values */
123 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
124 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
125 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
127 union pn533_cmd_poll_initdata {
140 /* Poll modulations */
142 PN533_POLL_MOD_106KBPS_A,
143 PN533_POLL_MOD_212KBPS_FELICA,
144 PN533_POLL_MOD_424KBPS_FELICA,
145 PN533_POLL_MOD_106KBPS_JEWEL,
146 PN533_POLL_MOD_847KBPS_B,
148 __PN533_POLL_MOD_AFTER_LAST,
150 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
152 struct pn533_poll_modulations {
156 union pn533_cmd_poll_initdata initiator_data;
161 const struct pn533_poll_modulations poll_mod[] = {
162 [PN533_POLL_MOD_106KBPS_A] = {
169 [PN533_POLL_MOD_212KBPS_FELICA] = {
173 .initiator_data.felica = {
174 .opcode = PN533_FELICA_OPC_SENSF_REQ,
175 .sc = PN533_FELICA_SENSF_SC_ALL,
176 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
182 [PN533_POLL_MOD_424KBPS_FELICA] = {
186 .initiator_data.felica = {
187 .opcode = PN533_FELICA_OPC_SENSF_REQ,
188 .sc = PN533_FELICA_SENSF_SC_ALL,
189 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
195 [PN533_POLL_MOD_106KBPS_JEWEL] = {
202 [PN533_POLL_MOD_847KBPS_B] = {
206 .initiator_data.type_b = {
207 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
209 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
216 /* PN533_CMD_IN_ATR */
218 struct pn533_cmd_activate_param {
223 struct pn533_cmd_activate_response {
235 /* PN533_CMD_IN_JUMP_FOR_DEP */
236 struct pn533_cmd_jump_dep {
243 struct pn533_cmd_jump_dep_response {
257 struct usb_device *udev;
258 struct usb_interface *interface;
259 struct nfc_dev *nfc_dev;
263 struct pn533_frame *out_frame;
267 struct pn533_frame *in_frame;
269 struct sk_buff_head resp_q;
271 struct workqueue_struct *wq;
272 struct work_struct cmd_work;
273 struct work_struct mi_work;
274 struct pn533_frame *wq_in_frame;
277 pn533_cmd_complete_t cmd_complete;
278 void *cmd_complete_arg;
279 struct semaphore cmd_lock;
282 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
287 u8 tgt_available_prots;
299 /* The rule: value + checksum = 0 */
300 static inline u8 pn533_checksum(u8 value)
305 /* The rule: sum(data elements) + checksum = 0 */
306 static u8 pn533_data_checksum(u8 *data, int datalen)
311 for (i = 0; i < datalen; i++)
314 return pn533_checksum(sum);
318 * pn533_tx_frame_ack - create a ack frame
319 * @frame: The frame to be set as ack
321 * Ack is different type of standard frame. As a standard frame, it has
322 * preamble and start_frame. However the checksum of this frame must fail,
323 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
324 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
325 * After datalen_checksum field, the postamble is placed.
327 static void pn533_tx_frame_ack(struct pn533_frame *frame)
330 frame->start_frame = cpu_to_be16(PN533_SOF);
332 frame->datalen_checksum = 0xFF;
333 /* data[0] is used as postamble */
337 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
340 frame->start_frame = cpu_to_be16(PN533_SOF);
341 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
342 PN533_FRAME_CMD(frame) = cmd;
346 static void pn533_tx_frame_finish(struct pn533_frame *frame)
348 frame->datalen_checksum = pn533_checksum(frame->datalen);
350 PN533_FRAME_CHECKSUM(frame) =
351 pn533_data_checksum(frame->data, frame->datalen);
353 PN533_FRAME_POSTAMBLE(frame) = 0;
356 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
360 if (frame->start_frame != cpu_to_be16(PN533_SOF))
363 checksum = pn533_checksum(frame->datalen);
364 if (checksum != frame->datalen_checksum)
367 checksum = pn533_data_checksum(frame->data, frame->datalen);
368 if (checksum != PN533_FRAME_CHECKSUM(frame))
374 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
376 if (frame->start_frame != cpu_to_be16(PN533_SOF))
379 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
385 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
387 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
391 static void pn533_wq_cmd_complete(struct work_struct *work)
393 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
394 struct pn533_frame *in_frame;
397 in_frame = dev->wq_in_frame;
399 if (dev->wq_in_error)
400 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
403 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
404 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
405 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
407 if (rc != -EINPROGRESS)
411 static void pn533_recv_response(struct urb *urb)
413 struct pn533 *dev = urb->context;
414 struct pn533_frame *in_frame;
416 dev->wq_in_frame = NULL;
418 switch (urb->status) {
425 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
426 " status: %d", urb->status);
427 dev->wq_in_error = urb->status;
430 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
432 dev->wq_in_error = urb->status;
436 in_frame = dev->in_urb->transfer_buffer;
438 if (!pn533_rx_frame_is_valid(in_frame)) {
439 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
440 dev->wq_in_error = -EIO;
444 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
445 nfc_dev_err(&dev->interface->dev, "The received frame is not "
446 "response to the last command");
447 dev->wq_in_error = -EIO;
451 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
452 dev->wq_in_error = 0;
453 dev->wq_in_frame = in_frame;
456 queue_work(dev->wq, &dev->cmd_work);
459 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
461 dev->in_urb->complete = pn533_recv_response;
463 return usb_submit_urb(dev->in_urb, flags);
466 static void pn533_recv_ack(struct urb *urb)
468 struct pn533 *dev = urb->context;
469 struct pn533_frame *in_frame;
472 switch (urb->status) {
479 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
480 " status: %d", urb->status);
481 dev->wq_in_error = urb->status;
484 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
486 dev->wq_in_error = urb->status;
490 in_frame = dev->in_urb->transfer_buffer;
492 if (!pn533_rx_frame_is_ack(in_frame)) {
493 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
494 dev->wq_in_error = -EIO;
498 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
500 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
502 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
504 dev->wq_in_error = rc;
511 dev->wq_in_frame = NULL;
512 queue_work(dev->wq, &dev->cmd_work);
515 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
517 dev->in_urb->complete = pn533_recv_ack;
519 return usb_submit_urb(dev->in_urb, flags);
522 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
526 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
528 pn533_tx_frame_ack(dev->out_frame);
530 dev->out_urb->transfer_buffer = dev->out_frame;
531 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
532 rc = usb_submit_urb(dev->out_urb, flags);
537 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
538 struct pn533_frame *out_frame,
539 struct pn533_frame *in_frame,
541 pn533_cmd_complete_t cmd_complete,
542 void *arg, gfp_t flags)
546 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
547 PN533_FRAME_CMD(out_frame));
549 dev->cmd = PN533_FRAME_CMD(out_frame);
550 dev->cmd_complete = cmd_complete;
551 dev->cmd_complete_arg = arg;
553 dev->out_urb->transfer_buffer = out_frame;
554 dev->out_urb->transfer_buffer_length =
555 PN533_FRAME_SIZE(out_frame);
557 dev->in_urb->transfer_buffer = in_frame;
558 dev->in_urb->transfer_buffer_length = in_frame_len;
560 rc = usb_submit_urb(dev->out_urb, flags);
564 rc = pn533_submit_urb_for_ack(dev, flags);
571 usb_unlink_urb(dev->out_urb);
575 static int pn533_send_cmd_frame_async(struct pn533 *dev,
576 struct pn533_frame *out_frame,
577 struct pn533_frame *in_frame,
579 pn533_cmd_complete_t cmd_complete,
580 void *arg, gfp_t flags)
584 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
586 if (down_trylock(&dev->cmd_lock))
589 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
590 in_frame_len, cmd_complete, arg, flags);
600 struct pn533_sync_cmd_response {
602 struct completion done;
605 static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
606 u8 *params, int params_len)
608 struct pn533_sync_cmd_response *arg = _arg;
610 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
614 if (params_len < 0) /* error */
615 arg->rc = params_len;
617 complete(&arg->done);
622 static int pn533_send_cmd_frame_sync(struct pn533 *dev,
623 struct pn533_frame *out_frame,
624 struct pn533_frame *in_frame,
628 struct pn533_sync_cmd_response arg;
630 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
632 init_completion(&arg.done);
634 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
635 pn533_sync_cmd_complete, &arg, GFP_KERNEL);
639 wait_for_completion(&arg.done);
644 static void pn533_send_complete(struct urb *urb)
646 struct pn533 *dev = urb->context;
648 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
650 switch (urb->status) {
657 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
658 " status: %d", urb->status);
661 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
666 struct pn533_target_type_a {
674 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
675 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
676 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
678 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
679 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
681 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
682 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
684 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
685 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
686 #define PN533_TYPE_A_SEL_PROT_DEP 2
687 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
689 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
695 if (target_data_len < sizeof(struct pn533_target_type_a))
698 /* The lenght check of nfcid[] and ats[] are not being performed because
699 the values are not being used */
701 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
702 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
703 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
705 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
706 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
707 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
708 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
711 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
712 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
718 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
721 struct pn533_target_type_a *tgt_type_a;
723 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
725 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
728 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
729 case PN533_TYPE_A_SEL_PROT_MIFARE:
730 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
732 case PN533_TYPE_A_SEL_PROT_ISO14443:
733 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
735 case PN533_TYPE_A_SEL_PROT_DEP:
736 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
738 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
739 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
740 NFC_PROTO_NFC_DEP_MASK;
744 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
745 nfc_tgt->sel_res = tgt_type_a->sel_res;
746 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
747 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
752 struct pn533_target_felica {
761 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
762 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
764 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
767 if (target_data_len < sizeof(struct pn533_target_felica))
770 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
776 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
779 struct pn533_target_felica *tgt_felica;
781 tgt_felica = (struct pn533_target_felica *) tgt_data;
783 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
786 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
787 tgt_felica->nfcid2[1] ==
788 PN533_FELICA_SENSF_NFCID2_DEP_B2)
789 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
791 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
793 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
794 nfc_tgt->sensf_res_len = 9;
799 struct pn533_target_jewel {
804 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
810 if (target_data_len < sizeof(struct pn533_target_jewel))
813 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
814 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
815 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
817 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
818 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
819 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
820 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
826 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
829 struct pn533_target_jewel *tgt_jewel;
831 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
833 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
836 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
837 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
838 nfc_tgt->nfcid1_len = 4;
839 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
844 struct pn533_type_b_prot_info {
850 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
851 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
852 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
854 struct pn533_type_b_sens_res {
858 struct pn533_type_b_prot_info prot_info;
861 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
863 struct pn533_target_type_b {
864 struct pn533_type_b_sens_res sensb_res;
869 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
872 if (target_data_len < sizeof(struct pn533_target_type_b))
875 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
878 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
879 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
885 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
888 struct pn533_target_type_b *tgt_type_b;
890 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
892 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
895 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
900 struct pn533_poll_response {
906 static int pn533_target_found(struct pn533 *dev,
907 struct pn533_poll_response *resp, int resp_len)
910 struct nfc_target nfc_tgt;
913 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
919 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
921 target_data_len = resp_len - sizeof(struct pn533_poll_response);
923 switch (dev->poll_mod_curr) {
924 case PN533_POLL_MOD_106KBPS_A:
925 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
928 case PN533_POLL_MOD_212KBPS_FELICA:
929 case PN533_POLL_MOD_424KBPS_FELICA:
930 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
933 case PN533_POLL_MOD_106KBPS_JEWEL:
934 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
937 case PN533_POLL_MOD_847KBPS_B:
938 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
942 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
950 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
951 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
952 " have the desired protocol");
956 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
957 "0x%x", nfc_tgt.supported_protocols);
959 dev->tgt_available_prots = nfc_tgt.supported_protocols;
961 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
966 static void pn533_poll_reset_mod_list(struct pn533 *dev)
968 dev->poll_mod_count = 0;
971 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
973 dev->poll_mod_active[dev->poll_mod_count] =
974 (struct pn533_poll_modulations *) &poll_mod[mod_index];
975 dev->poll_mod_count++;
978 static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
980 pn533_poll_reset_mod_list(dev);
982 if (protocols & NFC_PROTO_MIFARE_MASK
983 || protocols & NFC_PROTO_ISO14443_MASK
984 || protocols & NFC_PROTO_NFC_DEP_MASK)
985 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
987 if (protocols & NFC_PROTO_FELICA_MASK
988 || protocols & NFC_PROTO_NFC_DEP_MASK) {
989 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
990 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
993 if (protocols & NFC_PROTO_JEWEL_MASK)
994 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
996 if (protocols & NFC_PROTO_ISO14443_MASK)
997 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1000 static void pn533_start_poll_frame(struct pn533_frame *frame,
1001 struct pn533_poll_modulations *mod)
1004 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1006 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1007 frame->datalen += mod->len;
1009 pn533_tx_frame_finish(frame);
1012 static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
1013 u8 *params, int params_len)
1015 struct pn533_poll_response *resp;
1016 struct pn533_poll_modulations *next_mod;
1019 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1021 if (params_len == -ENOENT) {
1022 nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
1027 if (params_len < 0) {
1028 nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
1033 resp = (struct pn533_poll_response *) params;
1035 rc = pn533_target_found(dev, resp, params_len);
1037 /* We must stop the poll after a valid target found */
1042 nfc_dev_err(&dev->interface->dev, "The target found is"
1043 " not valid - continuing to poll");
1046 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1048 next_mod = dev->poll_mod_active[dev->poll_mod_curr];
1050 nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
1051 dev->poll_mod_curr);
1053 pn533_start_poll_frame(dev->out_frame, next_mod);
1055 /* Don't need to down the semaphore again */
1056 rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1057 dev->in_maxlen, pn533_start_poll_complete,
1061 nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
1062 " because poll has been stopped");
1067 nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
1068 " next modulation", rc);
1072 /* Inform caller function to do not up the semaphore */
1073 return -EINPROGRESS;
1076 pn533_poll_reset_mod_list(dev);
1077 dev->poll_protocols = 0;
1081 static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
1083 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1084 struct pn533_poll_modulations *start_mod;
1087 nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
1090 if (dev->poll_mod_count) {
1091 nfc_dev_err(&dev->interface->dev, "Polling operation already"
1096 if (dev->tgt_active_prot) {
1097 nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
1098 " already activated");
1102 pn533_poll_create_mod_list(dev, protocols);
1104 if (!dev->poll_mod_count) {
1105 nfc_dev_err(&dev->interface->dev, "No valid protocols"
1111 nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
1112 dev->poll_mod_count);
1114 dev->poll_mod_curr = 0;
1115 start_mod = dev->poll_mod_active[dev->poll_mod_curr];
1117 pn533_start_poll_frame(dev->out_frame, start_mod);
1119 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1120 dev->in_maxlen, pn533_start_poll_complete,
1124 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1129 dev->poll_protocols = protocols;
1134 pn533_poll_reset_mod_list(dev);
1138 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1140 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1142 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1144 if (!dev->poll_mod_count) {
1145 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1150 /* An ack will cancel the last issued command (poll) */
1151 pn533_send_ack(dev, GFP_KERNEL);
1153 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1154 usb_kill_urb(dev->in_urb);
1157 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1159 struct pn533_cmd_activate_param param;
1160 struct pn533_cmd_activate_response *resp;
1164 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1166 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1170 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), ¶m,
1171 sizeof(struct pn533_cmd_activate_param));
1172 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1174 pn533_tx_frame_finish(dev->out_frame);
1176 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1181 resp = (struct pn533_cmd_activate_response *)
1182 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1183 rc = resp->status & PN533_CMD_RET_MASK;
1184 if (rc != PN533_CMD_RET_SUCCESS)
1187 /* ATR_RES general bytes are located at offset 16 */
1188 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1189 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1194 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1195 struct nfc_target *target, u32 protocol)
1197 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1200 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1203 if (dev->poll_mod_count) {
1204 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1209 if (dev->tgt_active_prot) {
1210 nfc_dev_err(&dev->interface->dev, "There is already an active"
1215 if (!dev->tgt_available_prots) {
1216 nfc_dev_err(&dev->interface->dev, "There is no available target"
1221 if (!(dev->tgt_available_prots & (1 << protocol))) {
1222 nfc_dev_err(&dev->interface->dev, "The target does not support"
1223 " the requested protocol %u", protocol);
1227 if (protocol == NFC_PROTO_NFC_DEP) {
1228 rc = pn533_activate_target_nfcdep(dev);
1230 nfc_dev_err(&dev->interface->dev, "Error %d when"
1231 " activating target with"
1232 " NFC_DEP protocol", rc);
1237 dev->tgt_active_prot = protocol;
1238 dev->tgt_available_prots = 0;
1243 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1244 struct nfc_target *target)
1246 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1251 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1253 if (!dev->tgt_active_prot) {
1254 nfc_dev_err(&dev->interface->dev, "There is no active target");
1258 dev->tgt_active_prot = 0;
1260 skb_queue_purge(&dev->resp_q);
1262 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1265 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1266 dev->out_frame->datalen += sizeof(u8);
1268 pn533_tx_frame_finish(dev->out_frame);
1270 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1273 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1274 " command to the controller");
1278 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1279 rc = status & PN533_CMD_RET_MASK;
1280 if (rc != PN533_CMD_RET_SUCCESS)
1281 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1288 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1289 u8 *params, int params_len)
1291 struct pn533_cmd_jump_dep *cmd;
1292 struct pn533_cmd_jump_dep_response *resp;
1293 struct nfc_target nfc_target;
1297 if (params_len == -ENOENT) {
1298 nfc_dev_dbg(&dev->interface->dev, "");
1302 if (params_len < 0) {
1303 nfc_dev_err(&dev->interface->dev,
1304 "Error %d when bringing DEP link up",
1309 if (dev->tgt_available_prots &&
1310 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1311 nfc_dev_err(&dev->interface->dev,
1312 "The target does not support DEP");
1316 resp = (struct pn533_cmd_jump_dep_response *) params;
1317 cmd = (struct pn533_cmd_jump_dep *) arg;
1318 rc = resp->status & PN533_CMD_RET_MASK;
1319 if (rc != PN533_CMD_RET_SUCCESS) {
1320 nfc_dev_err(&dev->interface->dev,
1321 "Bringing DEP link up failed %d", rc);
1325 if (!dev->tgt_available_prots) {
1326 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1328 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1329 nfc_target.nfcid1_len = 10;
1330 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
1331 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1335 dev->tgt_available_prots = 0;
1338 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1340 /* ATR_RES general bytes are located at offset 17 */
1341 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1342 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1343 resp->gt, target_gt_len);
1345 rc = nfc_dep_link_is_up(dev->nfc_dev,
1346 dev->nfc_dev->targets[0].idx,
1347 !cmd->active, NFC_RF_INITIATOR);
1352 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1353 u8 comm_mode, u8* gb, size_t gb_len)
1355 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1356 struct pn533_cmd_jump_dep *cmd;
1360 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1362 if (dev->poll_mod_count) {
1363 nfc_dev_err(&dev->interface->dev,
1364 "Cannot bring the DEP link up while polling");
1368 if (dev->tgt_active_prot) {
1369 nfc_dev_err(&dev->interface->dev,
1370 "There is already an active target");
1374 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
1375 cmd = kzalloc(cmd_len, GFP_KERNEL);
1379 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
1381 cmd->active = !comm_mode;
1383 if (gb != NULL && gb_len > 0) {
1384 cmd->next = 4; /* We have some Gi */
1385 memcpy(cmd->gt, gb, gb_len);
1390 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
1391 dev->out_frame->datalen += cmd_len;
1393 pn533_tx_frame_finish(dev->out_frame);
1395 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1396 dev->in_maxlen, pn533_in_dep_link_up_complete,
1408 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1410 pn533_deactivate_target(nfc_dev, 0);
1415 #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
1416 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1418 static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
1420 int payload_len = skb->len;
1421 struct pn533_frame *out_frame;
1424 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1427 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1428 /* TODO: Implement support to multi-part data exchange */
1429 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1431 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1435 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1436 out_frame = (struct pn533_frame *) skb->data;
1438 pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
1441 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
1442 out_frame->datalen += sizeof(u8);
1444 /* The data is already in the out_frame, just update the datalen */
1445 out_frame->datalen += payload_len;
1447 pn533_tx_frame_finish(out_frame);
1448 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1453 struct pn533_data_exchange_arg {
1454 struct sk_buff *skb_resp;
1455 struct sk_buff *skb_out;
1456 data_exchange_cb_t cb;
1460 static struct sk_buff *pn533_build_response(struct pn533 *dev)
1462 struct sk_buff *skb, *tmp, *t;
1463 unsigned int skb_len = 0, tmp_len = 0;
1465 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
1467 if (skb_queue_empty(&dev->resp_q))
1470 if (skb_queue_len(&dev->resp_q) == 1) {
1471 skb = skb_dequeue(&dev->resp_q);
1475 skb_queue_walk_safe(&dev->resp_q, tmp, t)
1476 skb_len += tmp->len;
1478 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
1481 skb = alloc_skb(skb_len, GFP_KERNEL);
1485 skb_put(skb, skb_len);
1487 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
1488 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
1489 tmp_len += tmp->len;
1493 skb_queue_purge(&dev->resp_q);
1498 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1499 u8 *params, int params_len)
1501 struct pn533_data_exchange_arg *arg = _arg;
1502 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
1503 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1508 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1510 dev_kfree_skb(arg->skb_out);
1512 if (params_len < 0) { /* error */
1519 cmd_ret = status & PN533_CMD_RET_MASK;
1520 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
1521 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
1522 " exchanging data", cmd_ret);
1527 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1528 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1529 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1530 skb_queue_tail(&dev->resp_q, skb_resp);
1532 if (status & PN533_CMD_MI_MASK) {
1533 queue_work(dev->wq, &dev->mi_work);
1534 return -EINPROGRESS;
1537 skb = pn533_build_response(dev);
1541 arg->cb(arg->cb_context, skb, 0);
1546 skb_queue_purge(&dev->resp_q);
1547 dev_kfree_skb(skb_resp);
1548 arg->cb(arg->cb_context, NULL, err);
1553 static int pn533_data_exchange(struct nfc_dev *nfc_dev,
1554 struct nfc_target *target, struct sk_buff *skb,
1555 data_exchange_cb_t cb, void *cb_context)
1557 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1558 struct pn533_frame *out_frame, *in_frame;
1559 struct pn533_data_exchange_arg *arg;
1560 struct sk_buff *skb_resp;
1564 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1566 if (!dev->tgt_active_prot) {
1567 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
1568 " there is no active target");
1573 rc = pn533_data_exchange_tx_frame(dev, skb);
1577 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1578 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1579 PN533_FRAME_TAIL_SIZE;
1581 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1587 in_frame = (struct pn533_frame *) skb_resp->data;
1588 out_frame = (struct pn533_frame *) skb->data;
1590 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
1596 arg->skb_resp = skb_resp;
1599 arg->cb_context = cb_context;
1601 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
1602 pn533_data_exchange_complete, arg,
1605 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1606 " perform data_exchange", rc);
1615 kfree_skb(skb_resp);
1621 static void pn533_wq_mi_recv(struct work_struct *work)
1623 struct pn533 *dev = container_of(work, struct pn533, mi_work);
1624 struct sk_buff *skb_cmd;
1625 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
1626 struct pn533_frame *out_frame, *in_frame;
1627 struct sk_buff *skb_resp;
1631 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1633 /* This is a zero payload size skb */
1634 skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
1636 if (skb_cmd == NULL)
1639 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
1641 rc = pn533_data_exchange_tx_frame(dev, skb_cmd);
1645 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
1646 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1647 PN533_FRAME_TAIL_SIZE;
1648 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
1654 in_frame = (struct pn533_frame *) skb_resp->data;
1655 out_frame = (struct pn533_frame *) skb_cmd->data;
1657 arg->skb_resp = skb_resp;
1658 arg->skb_out = skb_cmd;
1660 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
1662 pn533_data_exchange_complete,
1663 dev->cmd_complete_arg, GFP_KERNEL);
1667 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
1668 " perform data_exchange", rc);
1670 kfree_skb(skb_resp);
1676 pn533_send_ack(dev, GFP_KERNEL);
1683 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
1689 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1691 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
1693 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
1694 params[0] = cfgitem;
1695 memcpy(¶ms[1], cfgdata, cfgdata_len);
1696 dev->out_frame->datalen += (1 + cfgdata_len);
1698 pn533_tx_frame_finish(dev->out_frame);
1700 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1706 struct nfc_ops pn533_nfc_ops = {
1709 .dep_link_up = pn533_dep_link_up,
1710 .dep_link_down = pn533_dep_link_down,
1711 .start_poll = pn533_start_poll,
1712 .stop_poll = pn533_stop_poll,
1713 .activate_target = pn533_activate_target,
1714 .deactivate_target = pn533_deactivate_target,
1715 .data_exchange = pn533_data_exchange,
1718 static int pn533_probe(struct usb_interface *interface,
1719 const struct usb_device_id *id)
1721 struct pn533_fw_version *fw_ver;
1723 struct usb_host_interface *iface_desc;
1724 struct usb_endpoint_descriptor *endpoint;
1725 struct pn533_config_max_retries max_retries;
1726 int in_endpoint = 0;
1727 int out_endpoint = 0;
1732 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1736 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1737 dev->interface = interface;
1738 sema_init(&dev->cmd_lock, 1);
1740 iface_desc = interface->cur_altsetting;
1741 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1742 endpoint = &iface_desc->endpoint[i].desc;
1744 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
1745 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
1746 in_endpoint = endpoint->bEndpointAddress;
1749 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
1751 le16_to_cpu(endpoint->wMaxPacketSize);
1752 out_endpoint = endpoint->bEndpointAddress;
1756 if (!in_endpoint || !out_endpoint) {
1757 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
1758 " bulk-out endpoint");
1763 dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
1764 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1765 dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
1766 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1768 if (!dev->in_frame || !dev->out_frame ||
1769 !dev->in_urb || !dev->out_urb)
1772 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1773 usb_rcvbulkpipe(dev->udev, in_endpoint),
1774 NULL, 0, NULL, dev);
1775 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1776 usb_sndbulkpipe(dev->udev, out_endpoint),
1778 pn533_send_complete, dev);
1780 INIT_WORK(&dev->cmd_work, pn533_wq_cmd_complete);
1781 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
1782 dev->wq = alloc_workqueue("pn533",
1783 WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
1785 if (dev->wq == NULL)
1788 skb_queue_head_init(&dev->resp_q);
1790 usb_set_intfdata(interface, dev);
1792 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
1793 pn533_tx_frame_finish(dev->out_frame);
1795 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1800 fw_ver = (struct pn533_fw_version *)
1801 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1802 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
1803 " attached", fw_ver->ver, fw_ver->rev);
1805 protocols = NFC_PROTO_JEWEL_MASK
1806 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
1807 | NFC_PROTO_ISO14443_MASK
1808 | NFC_PROTO_NFC_DEP_MASK;
1810 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
1811 PN533_CMD_DATAEXCH_HEAD_LEN,
1812 PN533_FRAME_TAIL_SIZE);
1816 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
1817 nfc_set_drvdata(dev->nfc_dev, dev);
1819 rc = nfc_register_device(dev->nfc_dev);
1823 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
1824 max_retries.mx_rty_psl = 2;
1825 max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
1827 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
1828 (u8 *) &max_retries, sizeof(max_retries));
1831 nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
1839 nfc_free_device(dev->nfc_dev);
1841 destroy_workqueue(dev->wq);
1843 kfree(dev->in_frame);
1844 usb_free_urb(dev->in_urb);
1845 kfree(dev->out_frame);
1846 usb_free_urb(dev->out_urb);
1851 static void pn533_disconnect(struct usb_interface *interface)
1855 dev = usb_get_intfdata(interface);
1856 usb_set_intfdata(interface, NULL);
1858 nfc_unregister_device(dev->nfc_dev);
1859 nfc_free_device(dev->nfc_dev);
1861 usb_kill_urb(dev->in_urb);
1862 usb_kill_urb(dev->out_urb);
1864 destroy_workqueue(dev->wq);
1866 skb_queue_purge(&dev->resp_q);
1868 kfree(dev->in_frame);
1869 usb_free_urb(dev->in_urb);
1870 kfree(dev->out_frame);
1871 usb_free_urb(dev->out_urb);
1874 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
1877 static struct usb_driver pn533_driver = {
1879 .probe = pn533_probe,
1880 .disconnect = pn533_disconnect,
1881 .id_table = pn533_table,
1884 module_usb_driver(pn533_driver);
1886 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
1887 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
1888 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
1889 MODULE_VERSION(VERSION);
1890 MODULE_LICENSE("GPL");