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 #define SONY_VENDOR_ID 0x054c
42 #define PASORI_PRODUCT_ID 0x02e1
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
47 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
48 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
49 NFC_PROTO_NFC_DEP_MASK |\
50 NFC_PROTO_ISO14443_B_MASK)
52 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
53 NFC_PROTO_MIFARE_MASK | \
54 NFC_PROTO_FELICA_MASK | \
55 NFC_PROTO_ISO14443_MASK | \
56 NFC_PROTO_NFC_DEP_MASK)
58 static const struct usb_device_id pn533_table[] = {
59 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = PN533_VENDOR_ID,
61 .idProduct = PN533_PRODUCT_ID,
62 .driver_info = PN533_DEVICE_STD,
64 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = SCM_VENDOR_ID,
66 .idProduct = SCL3711_PRODUCT_ID,
67 .driver_info = PN533_DEVICE_STD,
69 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = SONY_VENDOR_ID,
71 .idProduct = PASORI_PRODUCT_ID,
72 .driver_info = PN533_DEVICE_PASORI,
76 MODULE_DEVICE_TABLE(usb, pn533_table);
78 /* How much time we spend listening for initiators */
79 #define PN533_LISTEN_TIME 2
81 /* frame definitions */
82 #define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI)
85 #define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
86 + 2) /* data[0] TFI, data[1] CC */
87 #define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
90 * Max extended frame payload len, excluding TFI and CC
91 * which are already in PN533_FRAME_HEADER_LEN.
93 #define PN533_FRAME_MAX_PAYLOAD_LEN 263
95 #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
97 #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
98 #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
99 #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
102 #define PN533_SOF 0x00FF
104 /* frame identifier: in/out/error */
105 #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
106 #define PN533_DIR_OUT 0xD4
107 #define PN533_DIR_IN 0xD5
110 #define PN533_FRAME_CMD(f) (f->data[1])
111 #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
112 #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
114 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
115 #define PN533_CMD_RF_CONFIGURATION 0x32
116 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
117 #define PN533_CMD_IN_COMM_THRU 0x42
118 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
119 #define PN533_CMD_IN_ATR 0x50
120 #define PN533_CMD_IN_RELEASE 0x52
121 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
123 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
124 #define PN533_CMD_TG_GET_DATA 0x86
125 #define PN533_CMD_TG_SET_DATA 0x8e
126 #define PN533_CMD_UNDEF 0xff
128 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
130 /* PN533 Return codes */
131 #define PN533_CMD_RET_MASK 0x3F
132 #define PN533_CMD_MI_MASK 0x40
133 #define PN533_CMD_RET_SUCCESS 0x00
137 typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
138 u8 *params, int params_len);
140 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
141 struct sk_buff *resp);
143 /* structs for pn533 commands */
145 /* PN533_CMD_GET_FIRMWARE_VERSION */
146 struct pn533_fw_version {
153 /* PN533_CMD_RF_CONFIGURATION */
154 #define PN533_CFGITEM_TIMING 0x02
155 #define PN533_CFGITEM_MAX_RETRIES 0x05
156 #define PN533_CFGITEM_PASORI 0x82
158 #define PN533_CONFIG_TIMING_102 0xb
159 #define PN533_CONFIG_TIMING_204 0xc
160 #define PN533_CONFIG_TIMING_409 0xd
161 #define PN533_CONFIG_TIMING_819 0xe
163 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
164 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
166 struct pn533_config_max_retries {
169 u8 mx_rty_passive_act;
172 struct pn533_config_timing {
178 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
180 /* felica commands opcode */
181 #define PN533_FELICA_OPC_SENSF_REQ 0
182 #define PN533_FELICA_OPC_SENSF_RES 1
183 /* felica SENSF_REQ parameters */
184 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
185 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
186 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
187 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
189 /* type B initiator_data values */
190 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
191 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
192 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
194 union pn533_cmd_poll_initdata {
207 /* Poll modulations */
209 PN533_POLL_MOD_106KBPS_A,
210 PN533_POLL_MOD_212KBPS_FELICA,
211 PN533_POLL_MOD_424KBPS_FELICA,
212 PN533_POLL_MOD_106KBPS_JEWEL,
213 PN533_POLL_MOD_847KBPS_B,
216 __PN533_POLL_MOD_AFTER_LAST,
218 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
220 struct pn533_poll_modulations {
224 union pn533_cmd_poll_initdata initiator_data;
229 const struct pn533_poll_modulations poll_mod[] = {
230 [PN533_POLL_MOD_106KBPS_A] = {
237 [PN533_POLL_MOD_212KBPS_FELICA] = {
241 .initiator_data.felica = {
242 .opcode = PN533_FELICA_OPC_SENSF_REQ,
243 .sc = PN533_FELICA_SENSF_SC_ALL,
244 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
250 [PN533_POLL_MOD_424KBPS_FELICA] = {
254 .initiator_data.felica = {
255 .opcode = PN533_FELICA_OPC_SENSF_REQ,
256 .sc = PN533_FELICA_SENSF_SC_ALL,
257 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
263 [PN533_POLL_MOD_106KBPS_JEWEL] = {
270 [PN533_POLL_MOD_847KBPS_B] = {
274 .initiator_data.type_b = {
275 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
277 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
282 [PN533_LISTEN_MOD] = {
287 /* PN533_CMD_IN_ATR */
289 struct pn533_cmd_activate_param {
294 struct pn533_cmd_activate_response {
306 /* PN533_CMD_IN_JUMP_FOR_DEP */
307 struct pn533_cmd_jump_dep {
314 struct pn533_cmd_jump_dep_response {
328 /* PN533_TG_INIT_AS_TARGET */
329 #define PN533_INIT_TARGET_PASSIVE 0x1
330 #define PN533_INIT_TARGET_DEP 0x2
332 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
333 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
334 #define PN533_INIT_TARGET_RESP_DEP 0x4
336 struct pn533_cmd_init_target {
345 struct pn533_cmd_init_target_response {
351 struct usb_device *udev;
352 struct usb_interface *interface;
353 struct nfc_dev *nfc_dev;
356 struct pn533_frame *out_frame;
359 struct pn533_frame *in_frame;
361 struct sk_buff_head resp_q;
363 struct workqueue_struct *wq;
364 struct work_struct cmd_work;
365 struct work_struct cmd_complete_work;
366 struct work_struct poll_work;
367 struct work_struct mi_work;
368 struct work_struct tg_work;
369 struct timer_list listen_timer;
370 struct pn533_frame *wq_in_frame;
374 pn533_cmd_complete_t cmd_complete;
375 void *cmd_complete_arg;
376 struct mutex cmd_lock;
379 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
383 u32 listen_protocols;
388 u8 tgt_available_prots;
394 struct list_head cmd_queue;
399 struct list_head queue;
400 struct pn533_frame *out_frame;
401 struct pn533_frame *in_frame;
405 struct sk_buff *resp;
406 pn533_cmd_complete_t cmd_complete;
418 /* The rule: value + checksum = 0 */
419 static inline u8 pn533_checksum(u8 value)
424 /* The rule: sum(data elements) + checksum = 0 */
425 static u8 pn533_data_checksum(u8 *data, int datalen)
430 for (i = 0; i < datalen; i++)
433 return pn533_checksum(sum);
437 * pn533_tx_frame_ack - create a ack frame
438 * @frame: The frame to be set as ack
440 * Ack is different type of standard frame. As a standard frame, it has
441 * preamble and start_frame. However the checksum of this frame must fail,
442 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
443 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
444 * After datalen_checksum field, the postamble is placed.
446 static void pn533_tx_frame_ack(struct pn533_frame *frame)
449 frame->start_frame = cpu_to_be16(PN533_SOF);
451 frame->datalen_checksum = 0xFF;
452 /* data[0] is used as postamble */
456 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
459 frame->start_frame = cpu_to_be16(PN533_SOF);
460 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
461 PN533_FRAME_CMD(frame) = cmd;
465 static void pn533_tx_frame_finish(struct pn533_frame *frame)
467 frame->datalen_checksum = pn533_checksum(frame->datalen);
469 PN533_FRAME_CHECKSUM(frame) =
470 pn533_data_checksum(frame->data, frame->datalen);
472 PN533_FRAME_POSTAMBLE(frame) = 0;
475 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
479 if (frame->start_frame != cpu_to_be16(PN533_SOF))
482 checksum = pn533_checksum(frame->datalen);
483 if (checksum != frame->datalen_checksum)
486 checksum = pn533_data_checksum(frame->data, frame->datalen);
487 if (checksum != PN533_FRAME_CHECKSUM(frame))
493 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
495 if (frame->start_frame != cpu_to_be16(PN533_SOF))
498 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
504 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
506 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
510 static void pn533_wq_cmd_complete(struct work_struct *work)
512 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
513 struct pn533_frame *in_frame;
516 in_frame = dev->wq_in_frame;
518 if (dev->wq_in_error)
519 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
522 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
523 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
524 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
526 if (rc != -EINPROGRESS)
527 queue_work(dev->wq, &dev->cmd_work);
530 static void pn533_recv_response(struct urb *urb)
532 struct pn533 *dev = urb->context;
533 struct pn533_frame *in_frame;
535 dev->wq_in_frame = NULL;
537 switch (urb->status) {
544 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
545 " status: %d", urb->status);
546 dev->wq_in_error = urb->status;
549 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
551 dev->wq_in_error = urb->status;
555 in_frame = dev->in_urb->transfer_buffer;
557 if (!pn533_rx_frame_is_valid(in_frame)) {
558 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
559 dev->wq_in_error = -EIO;
563 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
564 nfc_dev_err(&dev->interface->dev, "The received frame is not "
565 "response to the last command");
566 dev->wq_in_error = -EIO;
570 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
571 dev->wq_in_error = 0;
572 dev->wq_in_frame = in_frame;
575 queue_work(dev->wq, &dev->cmd_complete_work);
578 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
580 dev->in_urb->complete = pn533_recv_response;
582 return usb_submit_urb(dev->in_urb, flags);
585 static void pn533_recv_ack(struct urb *urb)
587 struct pn533 *dev = urb->context;
588 struct pn533_frame *in_frame;
591 switch (urb->status) {
598 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
599 " status: %d", urb->status);
600 dev->wq_in_error = urb->status;
603 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
605 dev->wq_in_error = urb->status;
609 in_frame = dev->in_urb->transfer_buffer;
611 if (!pn533_rx_frame_is_ack(in_frame)) {
612 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
613 dev->wq_in_error = -EIO;
617 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
619 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
621 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
623 dev->wq_in_error = rc;
630 dev->wq_in_frame = NULL;
631 queue_work(dev->wq, &dev->cmd_complete_work);
634 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
636 dev->in_urb->complete = pn533_recv_ack;
638 return usb_submit_urb(dev->in_urb, flags);
641 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
645 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
647 pn533_tx_frame_ack(dev->out_frame);
649 dev->out_urb->transfer_buffer = dev->out_frame;
650 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
651 rc = usb_submit_urb(dev->out_urb, flags);
656 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
657 struct pn533_frame *out_frame,
658 struct pn533_frame *in_frame,
660 pn533_cmd_complete_t cmd_complete,
665 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
666 PN533_FRAME_CMD(out_frame));
668 dev->cmd = PN533_FRAME_CMD(out_frame);
669 dev->cmd_complete = cmd_complete;
670 dev->cmd_complete_arg = arg;
672 dev->out_urb->transfer_buffer = out_frame;
673 dev->out_urb->transfer_buffer_length =
674 PN533_FRAME_SIZE(out_frame);
676 dev->in_urb->transfer_buffer = in_frame;
677 dev->in_urb->transfer_buffer_length = in_frame_len;
679 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
683 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
690 usb_unlink_urb(dev->out_urb);
694 static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
696 struct pn533_frame *frame;
697 /* payload is already there, just update datalen */
698 int payload_len = skb->len;
700 skb_push(skb, PN533_FRAME_HEADER_LEN);
701 skb_put(skb, PN533_FRAME_TAIL_LEN);
703 frame = (struct pn533_frame *)skb->data;
705 pn533_tx_frame_init(frame, cmd_code);
706 frame->datalen += payload_len;
707 pn533_tx_frame_finish(frame);
710 struct pn533_send_async_complete_arg {
711 pn533_send_async_complete_t complete_cb;
712 void *complete_cb_context;
713 struct sk_buff *resp;
717 static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
720 struct pn533_send_async_complete_arg *arg = _arg;
722 struct sk_buff *req = arg->req;
723 struct sk_buff *resp = arg->resp;
725 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
730 if (params_len < 0) {
731 nfc_dev_err(&dev->interface->dev,
732 "Error %d when starting as a target",
735 arg->complete_cb(dev, arg->complete_cb_context,
736 ERR_PTR(params_len));
742 skb_put(resp, PN533_FRAME_SIZE(frame));
743 skb_pull(resp, PN533_FRAME_HEADER_LEN);
744 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
746 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
753 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
754 struct sk_buff *req, struct sk_buff *resp,
756 pn533_send_async_complete_t complete_cb,
757 void *complete_cb_context)
759 struct pn533_cmd *cmd;
760 struct pn533_send_async_complete_arg *arg;
763 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
765 arg = kzalloc(sizeof(arg), GFP_KERNEL);
769 arg->complete_cb = complete_cb;
770 arg->complete_cb_context = complete_cb_context;
774 pn533_build_cmd_frame(cmd_code, req);
776 mutex_lock(&dev->cmd_lock);
778 if (!dev->cmd_pending) {
779 rc = __pn533_send_cmd_frame_async(dev,
780 (struct pn533_frame *)req->data,
781 (struct pn533_frame *)resp->data,
782 resp_len, pn533_send_async_complete,
787 dev->cmd_pending = 1;
791 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
793 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
799 INIT_LIST_HEAD(&cmd->queue);
800 cmd->cmd_code = cmd_code;
805 list_add_tail(&cmd->queue, &dev->cmd_queue);
812 mutex_unlock(&dev->cmd_lock);
816 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
818 pn533_send_async_complete_t complete_cb,
819 void *complete_cb_context)
821 struct sk_buff *resp;
823 int resp_len = PN533_FRAME_HEADER_LEN +
824 PN533_FRAME_MAX_PAYLOAD_LEN +
825 PN533_FRAME_TAIL_LEN;
827 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
829 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
833 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
834 complete_cb_context);
841 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
843 pn533_send_async_complete_t complete_cb,
844 void *complete_cb_context)
846 struct sk_buff *resp;
849 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
851 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
855 rc = __pn533_send_async(dev, cmd_code, req, resp,
856 PN533_NORMAL_FRAME_MAX_LEN,
857 complete_cb, complete_cb_context);
864 static void pn533_wq_cmd(struct work_struct *work)
866 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
867 struct pn533_cmd *cmd;
869 mutex_lock(&dev->cmd_lock);
871 if (list_empty(&dev->cmd_queue)) {
872 dev->cmd_pending = 0;
873 mutex_unlock(&dev->cmd_lock);
877 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
879 list_del(&cmd->queue);
881 mutex_unlock(&dev->cmd_lock);
883 if (cmd->cmd_code != PN533_CMD_UNDEF)
884 __pn533_send_cmd_frame_async(dev,
885 (struct pn533_frame *)cmd->req->data,
886 (struct pn533_frame *)cmd->resp->data,
887 PN533_NORMAL_FRAME_MAX_LEN,
888 pn533_send_async_complete,
891 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame,
893 cmd->cmd_complete, cmd->arg);
898 static int pn533_send_cmd_frame_async(struct pn533 *dev,
899 struct pn533_frame *out_frame,
900 struct pn533_frame *in_frame,
902 pn533_cmd_complete_t cmd_complete,
905 struct pn533_cmd *cmd;
908 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
910 mutex_lock(&dev->cmd_lock);
912 if (!dev->cmd_pending) {
913 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
914 in_frame_len, cmd_complete,
917 dev->cmd_pending = 1;
922 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__);
924 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
930 INIT_LIST_HEAD(&cmd->queue);
931 cmd->out_frame = out_frame;
932 cmd->in_frame = in_frame;
933 cmd->in_frame_len = in_frame_len;
934 cmd->cmd_code = PN533_CMD_UNDEF;
935 cmd->cmd_complete = cmd_complete;
938 list_add_tail(&cmd->queue, &dev->cmd_queue);
941 mutex_unlock(&dev->cmd_lock);
946 struct pn533_sync_cmd_response {
948 struct sk_buff *resp;
949 struct completion done;
952 static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
953 u8 *params, int params_len)
955 struct pn533_sync_cmd_response *arg = _arg;
957 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
961 if (params_len < 0) /* error */
962 arg->rc = params_len;
964 complete(&arg->done);
969 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
970 struct sk_buff *resp)
972 struct pn533_sync_cmd_response *arg = _arg;
974 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
977 complete(&arg->done);
982 /* pn533_send_cmd_sync
984 * Please note the req parameter is freed inside the function to
985 * limit a number of return value interpretations by the caller.
987 * 1. negative in case of error during TX path -> req should be freed
989 * 2. negative in case of error during RX path -> req should not be freed
990 * as it's been already freed at the begining of RX path by
993 * 3. valid pointer in case of succesfult RX path
995 * A caller has to check a return value with IS_ERR macro. If the test pass,
996 * the returned pointer is valid.
999 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1000 struct sk_buff *req)
1003 struct pn533_sync_cmd_response arg;
1005 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1007 init_completion(&arg.done);
1009 rc = pn533_send_cmd_async(dev, cmd_code, req,
1010 pn533_send_sync_complete, &arg);
1016 wait_for_completion(&arg.done);
1021 static int pn533_send_cmd_frame_sync(struct pn533 *dev,
1022 struct pn533_frame *out_frame,
1023 struct pn533_frame *in_frame,
1027 struct pn533_sync_cmd_response arg;
1029 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1031 init_completion(&arg.done);
1033 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
1034 pn533_sync_cmd_complete, &arg);
1038 wait_for_completion(&arg.done);
1043 static void pn533_send_complete(struct urb *urb)
1045 struct pn533 *dev = urb->context;
1047 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1049 switch (urb->status) {
1056 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
1057 " status: %d", urb->status);
1060 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
1061 " %d", urb->status);
1065 struct pn533_target_type_a {
1073 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1074 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1075 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1077 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1078 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1080 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1081 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1083 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1084 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1085 #define PN533_TYPE_A_SEL_PROT_DEP 2
1086 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1088 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1089 int target_data_len)
1094 if (target_data_len < sizeof(struct pn533_target_type_a))
1097 /* The lenght check of nfcid[] and ats[] are not being performed because
1098 the values are not being used */
1100 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1101 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1102 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1104 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1105 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1106 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1107 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1110 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1111 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1117 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1120 struct pn533_target_type_a *tgt_type_a;
1122 tgt_type_a = (struct pn533_target_type_a *) tgt_data;
1124 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1127 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1128 case PN533_TYPE_A_SEL_PROT_MIFARE:
1129 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1131 case PN533_TYPE_A_SEL_PROT_ISO14443:
1132 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1134 case PN533_TYPE_A_SEL_PROT_DEP:
1135 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1137 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1138 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1139 NFC_PROTO_NFC_DEP_MASK;
1143 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1144 nfc_tgt->sel_res = tgt_type_a->sel_res;
1145 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1146 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1151 struct pn533_target_felica {
1160 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1161 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1163 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1164 int target_data_len)
1166 if (target_data_len < sizeof(struct pn533_target_felica))
1169 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1175 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1178 struct pn533_target_felica *tgt_felica;
1180 tgt_felica = (struct pn533_target_felica *) tgt_data;
1182 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1185 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
1186 tgt_felica->nfcid2[1] ==
1187 PN533_FELICA_SENSF_NFCID2_DEP_B2)
1188 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1190 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1192 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1193 nfc_tgt->sensf_res_len = 9;
1198 struct pn533_target_jewel {
1203 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1204 int target_data_len)
1209 if (target_data_len < sizeof(struct pn533_target_jewel))
1212 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1213 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1214 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1216 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1217 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1218 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1219 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1225 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1228 struct pn533_target_jewel *tgt_jewel;
1230 tgt_jewel = (struct pn533_target_jewel *) tgt_data;
1232 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1235 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1236 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1237 nfc_tgt->nfcid1_len = 4;
1238 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1243 struct pn533_type_b_prot_info {
1249 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1250 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1251 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1253 struct pn533_type_b_sens_res {
1257 struct pn533_type_b_prot_info prot_info;
1260 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1262 struct pn533_target_type_b {
1263 struct pn533_type_b_sens_res sensb_res;
1268 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1269 int target_data_len)
1271 if (target_data_len < sizeof(struct pn533_target_type_b))
1274 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1277 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1278 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1284 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1287 struct pn533_target_type_b *tgt_type_b;
1289 tgt_type_b = (struct pn533_target_type_b *) tgt_data;
1291 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1294 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1299 struct pn533_poll_response {
1305 static int pn533_target_found(struct pn533 *dev,
1306 struct pn533_poll_response *resp, int resp_len)
1308 int target_data_len;
1309 struct nfc_target nfc_tgt;
1312 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1313 dev->poll_mod_curr);
1318 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1320 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1322 switch (dev->poll_mod_curr) {
1323 case PN533_POLL_MOD_106KBPS_A:
1324 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
1327 case PN533_POLL_MOD_212KBPS_FELICA:
1328 case PN533_POLL_MOD_424KBPS_FELICA:
1329 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
1332 case PN533_POLL_MOD_106KBPS_JEWEL:
1333 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
1336 case PN533_POLL_MOD_847KBPS_B:
1337 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
1341 nfc_dev_err(&dev->interface->dev, "Unknown current poll"
1349 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1350 nfc_dev_dbg(&dev->interface->dev, "The target found does not"
1351 " have the desired protocol");
1355 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
1356 "0x%x", nfc_tgt.supported_protocols);
1358 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1360 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1365 static inline void pn533_poll_next_mod(struct pn533 *dev)
1367 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1370 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1372 dev->poll_mod_count = 0;
1375 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1377 dev->poll_mod_active[dev->poll_mod_count] =
1378 (struct pn533_poll_modulations *) &poll_mod[mod_index];
1379 dev->poll_mod_count++;
1382 static void pn533_poll_create_mod_list(struct pn533 *dev,
1383 u32 im_protocols, u32 tm_protocols)
1385 pn533_poll_reset_mod_list(dev);
1387 if (im_protocols & NFC_PROTO_MIFARE_MASK
1388 || im_protocols & NFC_PROTO_ISO14443_MASK
1389 || im_protocols & NFC_PROTO_NFC_DEP_MASK)
1390 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1392 if (im_protocols & NFC_PROTO_FELICA_MASK
1393 || im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1394 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1395 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1398 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1399 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1401 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1402 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1405 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1408 static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len)
1410 struct pn533_poll_response *resp;
1413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1415 resp = (struct pn533_poll_response *) params;
1417 rc = pn533_target_found(dev, resp, params_len);
1419 /* We must stop the poll after a valid target found */
1421 pn533_poll_reset_mod_list(dev);
1429 static int pn533_init_target_frame(struct pn533_frame *frame,
1430 u8 *gb, size_t gb_len)
1432 struct pn533_cmd_init_target *cmd;
1434 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1435 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1436 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1437 0xff, 0xff}; /* System code */
1438 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1440 0x40}; /* SEL_RES for DEP */
1442 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
1443 cmd = kzalloc(cmd_len, GFP_KERNEL);
1447 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
1449 /* DEP support only */
1450 cmd->mode |= PN533_INIT_TARGET_DEP;
1453 memcpy(cmd->felica, felica_params, 18);
1454 get_random_bytes(cmd->felica + 2, 6);
1457 memset(cmd->nfcid3, 0, 10);
1458 memcpy(cmd->nfcid3, cmd->felica, 8);
1461 memcpy(cmd->mifare, mifare_params, 6);
1464 cmd->gb_len = gb_len;
1465 memcpy(cmd->gb, gb, gb_len);
1468 cmd->gb[gb_len] = 0;
1470 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1472 frame->datalen += cmd_len;
1474 pn533_tx_frame_finish(frame);
1481 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1482 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1483 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1484 u8 *params, int params_len)
1486 struct sk_buff *skb_resp = arg;
1487 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1489 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1491 if (params_len < 0) {
1492 nfc_dev_err(&dev->interface->dev,
1493 "Error %d when starting as a target",
1499 if (params_len > 0 && params[0] != 0) {
1500 nfc_tm_deactivated(dev->nfc_dev);
1504 kfree_skb(skb_resp);
1508 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
1510 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
1511 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
1513 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1516 static void pn533_wq_tg_get_data(struct work_struct *work)
1518 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1519 struct pn533_frame *in_frame;
1520 struct sk_buff *skb_resp;
1521 size_t skb_resp_len;
1523 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1525 skb_resp_len = PN533_FRAME_HEADER_LEN +
1526 PN533_CMD_DATAEXCH_HEAD_LEN +
1527 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1528 PN533_FRAME_TAIL_LEN;
1530 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
1534 in_frame = (struct pn533_frame *)skb_resp->data;
1536 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
1537 pn533_tx_frame_finish(dev->out_frame);
1539 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1541 pn533_tm_get_data_complete,
1547 #define ATR_REQ_GB_OFFSET 17
1548 static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len)
1550 struct pn533_cmd_init_target_response *resp;
1551 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1555 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1557 if (params_len < 0) {
1558 nfc_dev_err(&dev->interface->dev,
1559 "Error %d when starting as a target",
1565 if (params_len < ATR_REQ_GB_OFFSET + 1)
1568 resp = (struct pn533_cmd_init_target_response *) params;
1570 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
1571 resp->mode, params_len);
1573 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
1574 if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
1575 comm_mode = NFC_COMM_ACTIVE;
1577 /* Again, only DEP */
1578 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1581 gb = resp->cmd + ATR_REQ_GB_OFFSET;
1582 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
1584 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1585 comm_mode, gb, gb_len);
1587 nfc_dev_err(&dev->interface->dev,
1588 "Error when signaling target activation");
1594 queue_work(dev->wq, &dev->tg_work);
1599 static void pn533_listen_mode_timer(unsigned long data)
1601 struct pn533 *dev = (struct pn533 *) data;
1603 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1605 /* An ack will cancel the last issued command (poll) */
1606 pn533_send_ack(dev, GFP_ATOMIC);
1608 dev->cancel_listen = 1;
1610 pn533_poll_next_mod(dev);
1612 queue_work(dev->wq, &dev->poll_work);
1615 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1616 u8 *params, int params_len)
1618 struct pn533_poll_modulations *cur_mod;
1621 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1623 if (params_len == -ENOENT) {
1624 if (dev->poll_mod_count != 0)
1627 nfc_dev_err(&dev->interface->dev,
1628 "Polling operation has been stopped");
1633 if (params_len < 0) {
1634 nfc_dev_err(&dev->interface->dev,
1635 "Error %d when running poll", params_len);
1640 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1642 if (cur_mod->len == 0) {
1643 del_timer(&dev->listen_timer);
1645 return pn533_init_target_complete(dev, params, params_len);
1647 rc = pn533_start_poll_complete(dev, params, params_len);
1652 pn533_poll_next_mod(dev);
1654 queue_work(dev->wq, &dev->poll_work);
1659 pn533_poll_reset_mod_list(dev);
1660 dev->poll_protocols = 0;
1664 static void pn533_build_poll_frame(struct pn533 *dev,
1665 struct pn533_frame *frame,
1666 struct pn533_poll_modulations *mod)
1668 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len);
1670 if (mod->len == 0) {
1672 pn533_init_target_frame(frame, dev->gb, dev->gb_len);
1675 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1677 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
1678 frame->datalen += mod->len;
1680 pn533_tx_frame_finish(frame);
1684 static int pn533_send_poll_frame(struct pn533 *dev)
1686 struct pn533_poll_modulations *cur_mod;
1689 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1691 pn533_build_poll_frame(dev, dev->out_frame, cur_mod);
1693 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
1694 PN533_NORMAL_FRAME_MAX_LEN,
1695 pn533_poll_complete,
1698 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1703 static void pn533_wq_poll(struct work_struct *work)
1705 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1706 struct pn533_poll_modulations *cur_mod;
1709 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1711 nfc_dev_dbg(&dev->interface->dev,
1712 "%s cancel_listen %d modulation len %d",
1713 __func__, dev->cancel_listen, cur_mod->len);
1715 if (dev->cancel_listen == 1) {
1716 dev->cancel_listen = 0;
1717 usb_kill_urb(dev->in_urb);
1720 rc = pn533_send_poll_frame(dev);
1724 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1725 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1730 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1731 u32 im_protocols, u32 tm_protocols)
1733 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1735 nfc_dev_dbg(&dev->interface->dev,
1736 "%s: im protocols 0x%x tm protocols 0x%x",
1737 __func__, im_protocols, tm_protocols);
1739 if (dev->tgt_active_prot) {
1740 nfc_dev_err(&dev->interface->dev,
1741 "Cannot poll with a target already activated");
1745 if (dev->tgt_mode) {
1746 nfc_dev_err(&dev->interface->dev,
1747 "Cannot poll while already being activated");
1752 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1753 if (dev->gb == NULL)
1757 dev->poll_mod_curr = 0;
1758 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1759 dev->poll_protocols = im_protocols;
1760 dev->listen_protocols = tm_protocols;
1762 return pn533_send_poll_frame(dev);
1765 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1767 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1769 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1771 del_timer(&dev->listen_timer);
1773 if (!dev->poll_mod_count) {
1774 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
1779 /* An ack will cancel the last issued command (poll) */
1780 pn533_send_ack(dev, GFP_KERNEL);
1782 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1783 usb_kill_urb(dev->in_urb);
1785 pn533_poll_reset_mod_list(dev);
1788 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1790 struct pn533_cmd_activate_param param;
1791 struct pn533_cmd_activate_response *resp;
1795 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1797 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
1801 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), ¶m,
1802 sizeof(struct pn533_cmd_activate_param));
1803 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1805 pn533_tx_frame_finish(dev->out_frame);
1807 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1808 PN533_NORMAL_FRAME_MAX_LEN);
1812 resp = (struct pn533_cmd_activate_response *)
1813 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
1814 rc = resp->status & PN533_CMD_RET_MASK;
1815 if (rc != PN533_CMD_RET_SUCCESS)
1818 /* ATR_RES general bytes are located at offset 16 */
1819 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
1820 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
1825 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1826 struct nfc_target *target, u32 protocol)
1828 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1831 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1834 if (dev->poll_mod_count) {
1835 nfc_dev_err(&dev->interface->dev, "Cannot activate while"
1840 if (dev->tgt_active_prot) {
1841 nfc_dev_err(&dev->interface->dev, "There is already an active"
1846 if (!dev->tgt_available_prots) {
1847 nfc_dev_err(&dev->interface->dev, "There is no available target"
1852 if (!(dev->tgt_available_prots & (1 << protocol))) {
1853 nfc_dev_err(&dev->interface->dev, "The target does not support"
1854 " the requested protocol %u", protocol);
1858 if (protocol == NFC_PROTO_NFC_DEP) {
1859 rc = pn533_activate_target_nfcdep(dev);
1861 nfc_dev_err(&dev->interface->dev, "Error %d when"
1862 " activating target with"
1863 " NFC_DEP protocol", rc);
1868 dev->tgt_active_prot = protocol;
1869 dev->tgt_available_prots = 0;
1874 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1875 struct nfc_target *target)
1877 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1882 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1884 if (!dev->tgt_active_prot) {
1885 nfc_dev_err(&dev->interface->dev, "There is no active target");
1889 dev->tgt_active_prot = 0;
1891 skb_queue_purge(&dev->resp_q);
1893 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
1896 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1897 dev->out_frame->datalen += sizeof(u8);
1899 pn533_tx_frame_finish(dev->out_frame);
1901 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
1902 PN533_NORMAL_FRAME_MAX_LEN);
1904 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1905 " command to the controller");
1909 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
1910 rc = status & PN533_CMD_RET_MASK;
1911 if (rc != PN533_CMD_RET_SUCCESS)
1912 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
1919 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1920 u8 *params, int params_len)
1922 struct pn533_cmd_jump_dep_response *resp;
1923 struct nfc_target nfc_target;
1926 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg;
1927 u8 active = cmd->active;
1931 if (params_len == -ENOENT) {
1932 nfc_dev_dbg(&dev->interface->dev, "");
1936 if (params_len < 0) {
1937 nfc_dev_err(&dev->interface->dev,
1938 "Error %d when bringing DEP link up",
1943 if (dev->tgt_available_prots &&
1944 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1945 nfc_dev_err(&dev->interface->dev,
1946 "The target does not support DEP");
1950 resp = (struct pn533_cmd_jump_dep_response *) params;
1951 rc = resp->status & PN533_CMD_RET_MASK;
1952 if (rc != PN533_CMD_RET_SUCCESS) {
1953 nfc_dev_err(&dev->interface->dev,
1954 "Bringing DEP link up failed %d", rc);
1958 if (!dev->tgt_available_prots) {
1959 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1961 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1962 nfc_target.nfcid1_len = 10;
1963 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
1964 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1968 dev->tgt_available_prots = 0;
1971 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1973 /* ATR_RES general bytes are located at offset 17 */
1974 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
1975 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1976 resp->gt, target_gt_len);
1978 rc = nfc_dep_link_is_up(dev->nfc_dev,
1979 dev->nfc_dev->targets[0].idx,
1980 !active, NFC_RF_INITIATOR);
1985 static int pn533_mod_to_baud(struct pn533 *dev)
1987 switch (dev->poll_mod_curr) {
1988 case PN533_POLL_MOD_106KBPS_A:
1990 case PN533_POLL_MOD_212KBPS_FELICA:
1992 case PN533_POLL_MOD_424KBPS_FELICA:
1999 #define PASSIVE_DATA_LEN 5
2000 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2001 u8 comm_mode, u8* gb, size_t gb_len)
2003 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2004 struct pn533_cmd_jump_dep *cmd;
2005 u8 cmd_len, *data_ptr;
2006 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2009 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2011 if (dev->poll_mod_count) {
2012 nfc_dev_err(&dev->interface->dev,
2013 "Cannot bring the DEP link up while polling");
2017 if (dev->tgt_active_prot) {
2018 nfc_dev_err(&dev->interface->dev,
2019 "There is already an active target");
2023 baud = pn533_mod_to_baud(dev);
2025 nfc_dev_err(&dev->interface->dev,
2026 "Invalid curr modulation %d", dev->poll_mod_curr);
2030 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
2031 if (comm_mode == NFC_COMM_PASSIVE)
2032 cmd_len += PASSIVE_DATA_LEN;
2034 cmd = kzalloc(cmd_len, GFP_KERNEL);
2038 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
2040 cmd->active = !comm_mode;
2043 data_ptr = cmd->data;
2044 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) {
2045 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN);
2047 data_ptr += PASSIVE_DATA_LEN;
2050 if (gb != NULL && gb_len > 0) {
2051 cmd->next |= 4; /* We have some Gi */
2052 memcpy(data_ptr, gb, gb_len);
2057 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
2058 dev->out_frame->datalen += cmd_len;
2060 pn533_tx_frame_finish(dev->out_frame);
2062 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
2063 PN533_NORMAL_FRAME_MAX_LEN,
2064 pn533_in_dep_link_up_complete, cmd);
2071 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2073 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2075 pn533_poll_reset_mod_list(dev);
2077 if (dev->tgt_mode || dev->tgt_active_prot) {
2078 pn533_send_ack(dev, GFP_KERNEL);
2079 usb_kill_urb(dev->in_urb);
2082 dev->tgt_active_prot = 0;
2085 skb_queue_purge(&dev->resp_q);
2090 static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
2093 int payload_len = skb->len;
2094 struct pn533_frame *out_frame;
2097 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
2100 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2101 /* TODO: Implement support to multi-part data exchange */
2102 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
2104 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2108 skb_push(skb, PN533_FRAME_HEADER_LEN);
2110 if (target == true) {
2111 switch (dev->device_type) {
2112 case PN533_DEVICE_PASORI:
2113 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2114 out_frame = (struct pn533_frame *) skb->data;
2115 pn533_tx_frame_init(out_frame,
2116 PN533_CMD_IN_COMM_THRU);
2122 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
2123 out_frame = (struct pn533_frame *) skb->data;
2124 pn533_tx_frame_init(out_frame,
2125 PN533_CMD_IN_DATA_EXCHANGE);
2127 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
2129 out_frame->datalen += sizeof(u8);
2135 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
2136 out_frame = (struct pn533_frame *) skb->data;
2137 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
2141 /* The data is already in the out_frame, just update the datalen */
2142 out_frame->datalen += payload_len;
2144 pn533_tx_frame_finish(out_frame);
2145 skb_put(skb, PN533_FRAME_TAIL_LEN);
2150 struct pn533_data_exchange_arg {
2151 struct sk_buff *skb_resp;
2152 struct sk_buff *skb_out;
2153 data_exchange_cb_t cb;
2157 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2159 struct sk_buff *skb, *tmp, *t;
2160 unsigned int skb_len = 0, tmp_len = 0;
2162 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
2164 if (skb_queue_empty(&dev->resp_q))
2167 if (skb_queue_len(&dev->resp_q) == 1) {
2168 skb = skb_dequeue(&dev->resp_q);
2172 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2173 skb_len += tmp->len;
2175 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2178 skb = alloc_skb(skb_len, GFP_KERNEL);
2182 skb_put(skb, skb_len);
2184 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2185 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2186 tmp_len += tmp->len;
2190 skb_queue_purge(&dev->resp_q);
2195 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2196 u8 *params, int params_len)
2198 struct pn533_data_exchange_arg *arg = _arg;
2199 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
2200 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
2205 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2207 dev_kfree_skb(arg->skb_out);
2209 if (params_len < 0) { /* error */
2216 cmd_ret = status & PN533_CMD_RET_MASK;
2217 if (cmd_ret != PN533_CMD_RET_SUCCESS) {
2218 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
2219 " exchanging data", cmd_ret);
2224 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
2225 skb_pull(skb_resp, PN533_FRAME_HEADER_LEN);
2226 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
2227 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_LEN);
2228 skb_queue_tail(&dev->resp_q, skb_resp);
2230 if (status & PN533_CMD_MI_MASK) {
2231 queue_work(dev->wq, &dev->mi_work);
2232 return -EINPROGRESS;
2235 skb = pn533_build_response(dev);
2239 arg->cb(arg->cb_context, skb, 0);
2244 skb_queue_purge(&dev->resp_q);
2245 dev_kfree_skb(skb_resp);
2246 arg->cb(arg->cb_context, NULL, err);
2251 static int pn533_transceive(struct nfc_dev *nfc_dev,
2252 struct nfc_target *target, struct sk_buff *skb,
2253 data_exchange_cb_t cb, void *cb_context)
2255 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2256 struct pn533_frame *out_frame, *in_frame;
2257 struct pn533_data_exchange_arg *arg;
2258 struct sk_buff *skb_resp;
2262 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2264 if (!dev->tgt_active_prot) {
2265 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
2266 " there is no active target");
2271 rc = pn533_build_tx_frame(dev, skb, true);
2275 skb_resp_len = PN533_FRAME_HEADER_LEN +
2276 PN533_CMD_DATAEXCH_HEAD_LEN +
2277 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2278 PN533_FRAME_TAIL_LEN;
2280 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
2286 in_frame = (struct pn533_frame *) skb_resp->data;
2287 out_frame = (struct pn533_frame *) skb->data;
2289 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2295 arg->skb_resp = skb_resp;
2298 arg->cb_context = cb_context;
2300 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
2301 pn533_data_exchange_complete, arg);
2303 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2304 " perform data_exchange", rc);
2313 kfree_skb(skb_resp);
2319 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2320 u8 *params, int params_len)
2322 struct sk_buff *skb_out = arg;
2324 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2326 dev_kfree_skb(skb_out);
2328 if (params_len < 0) {
2329 nfc_dev_err(&dev->interface->dev,
2330 "Error %d when sending data",
2336 if (params_len > 0 && params[0] != 0) {
2337 nfc_tm_deactivated(dev->nfc_dev);
2344 queue_work(dev->wq, &dev->tg_work);
2349 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2351 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2352 struct pn533_frame *out_frame;
2355 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2357 rc = pn533_build_tx_frame(dev, skb, false);
2361 out_frame = (struct pn533_frame *) skb->data;
2363 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
2364 PN533_NORMAL_FRAME_MAX_LEN,
2365 pn533_tm_send_complete, skb);
2367 nfc_dev_err(&dev->interface->dev,
2368 "Error %d when trying to send data", rc);
2380 static void pn533_wq_mi_recv(struct work_struct *work)
2382 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2383 struct sk_buff *skb_cmd;
2384 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
2385 struct pn533_frame *out_frame, *in_frame;
2386 struct sk_buff *skb_resp;
2390 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2392 /* This is a zero payload size skb */
2393 skb_cmd = alloc_skb(PN533_FRAME_HEADER_LEN +
2394 PN533_CMD_DATAEXCH_HEAD_LEN +
2395 PN533_FRAME_TAIL_LEN,
2397 if (skb_cmd == NULL)
2400 skb_reserve(skb_cmd,
2401 PN533_FRAME_HEADER_LEN + PN533_CMD_DATAEXCH_HEAD_LEN);
2403 rc = pn533_build_tx_frame(dev, skb_cmd, true);
2407 skb_resp_len = PN533_FRAME_HEADER_LEN +
2408 PN533_CMD_DATAEXCH_HEAD_LEN +
2409 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2410 PN533_FRAME_TAIL_LEN;
2412 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
2418 in_frame = (struct pn533_frame *) skb_resp->data;
2419 out_frame = (struct pn533_frame *) skb_cmd->data;
2421 arg->skb_resp = skb_resp;
2422 arg->skb_out = skb_cmd;
2424 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
2426 pn533_data_exchange_complete,
2427 dev->cmd_complete_arg);
2431 nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
2432 " perform data_exchange", rc);
2434 kfree_skb(skb_resp);
2440 pn533_send_ack(dev, GFP_KERNEL);
2444 queue_work(dev->wq, &dev->cmd_work);
2447 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2453 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2455 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
2457 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2458 params[0] = cfgitem;
2459 memcpy(¶ms[1], cfgdata, cfgdata_len);
2460 dev->out_frame->datalen += (1 + cfgdata_len);
2462 pn533_tx_frame_finish(dev->out_frame);
2464 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2465 PN533_NORMAL_FRAME_MAX_LEN);
2470 static int pn533_fw_reset(struct pn533 *dev)
2475 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2477 pn533_tx_frame_init(dev->out_frame, 0x18);
2479 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
2481 dev->out_frame->datalen += 1;
2483 pn533_tx_frame_finish(dev->out_frame);
2485 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2486 PN533_NORMAL_FRAME_MAX_LEN);
2491 static struct nfc_ops pn533_nfc_ops = {
2494 .dep_link_up = pn533_dep_link_up,
2495 .dep_link_down = pn533_dep_link_down,
2496 .start_poll = pn533_start_poll,
2497 .stop_poll = pn533_stop_poll,
2498 .activate_target = pn533_activate_target,
2499 .deactivate_target = pn533_deactivate_target,
2500 .im_transceive = pn533_transceive,
2501 .tm_send = pn533_tm_send,
2504 static int pn533_setup(struct pn533 *dev)
2506 struct pn533_config_max_retries max_retries;
2507 struct pn533_config_timing timing;
2508 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2511 switch (dev->device_type) {
2512 case PN533_DEVICE_STD:
2513 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2514 max_retries.mx_rty_psl = 2;
2515 max_retries.mx_rty_passive_act =
2516 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2518 timing.rfu = PN533_CONFIG_TIMING_102;
2519 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2520 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2524 case PN533_DEVICE_PASORI:
2525 max_retries.mx_rty_atr = 0x2;
2526 max_retries.mx_rty_psl = 0x1;
2527 max_retries.mx_rty_passive_act =
2528 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2530 timing.rfu = PN533_CONFIG_TIMING_102;
2531 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2532 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2537 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2542 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2543 (u8 *)&max_retries, sizeof(max_retries));
2545 nfc_dev_err(&dev->interface->dev,
2546 "Error on setting MAX_RETRIES config");
2551 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2552 (u8 *)&timing, sizeof(timing));
2554 nfc_dev_err(&dev->interface->dev,
2555 "Error on setting RF timings");
2559 switch (dev->device_type) {
2560 case PN533_DEVICE_STD:
2563 case PN533_DEVICE_PASORI:
2564 pn533_fw_reset(dev);
2566 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2569 nfc_dev_err(&dev->interface->dev,
2570 "Error while settings PASORI config");
2574 pn533_fw_reset(dev);
2582 static int pn533_probe(struct usb_interface *interface,
2583 const struct usb_device_id *id)
2585 struct pn533_fw_version *fw_ver;
2587 struct usb_host_interface *iface_desc;
2588 struct usb_endpoint_descriptor *endpoint;
2589 int in_endpoint = 0;
2590 int out_endpoint = 0;
2595 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2599 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2600 dev->interface = interface;
2601 mutex_init(&dev->cmd_lock);
2603 iface_desc = interface->cur_altsetting;
2604 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2605 endpoint = &iface_desc->endpoint[i].desc;
2607 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2608 in_endpoint = endpoint->bEndpointAddress;
2610 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2611 out_endpoint = endpoint->bEndpointAddress;
2614 if (!in_endpoint || !out_endpoint) {
2615 nfc_dev_err(&interface->dev, "Could not find bulk-in or"
2616 " bulk-out endpoint");
2621 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2622 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2623 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2624 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2626 if (!dev->in_frame || !dev->out_frame ||
2627 !dev->in_urb || !dev->out_urb)
2630 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2631 usb_rcvbulkpipe(dev->udev, in_endpoint),
2632 NULL, 0, NULL, dev);
2633 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2634 usb_sndbulkpipe(dev->udev, out_endpoint),
2636 pn533_send_complete, dev);
2638 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2639 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2640 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2641 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2642 INIT_WORK(&dev->poll_work, pn533_wq_poll);
2643 dev->wq = alloc_ordered_workqueue("pn533", 0);
2644 if (dev->wq == NULL)
2647 init_timer(&dev->listen_timer);
2648 dev->listen_timer.data = (unsigned long) dev;
2649 dev->listen_timer.function = pn533_listen_mode_timer;
2651 skb_queue_head_init(&dev->resp_q);
2653 INIT_LIST_HEAD(&dev->cmd_queue);
2655 usb_set_intfdata(interface, dev);
2657 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
2658 pn533_tx_frame_finish(dev->out_frame);
2660 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2661 PN533_NORMAL_FRAME_MAX_LEN);
2665 fw_ver = (struct pn533_fw_version *)
2666 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2667 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2668 " attached", fw_ver->ver, fw_ver->rev);
2670 dev->device_type = id->driver_info;
2671 switch (dev->device_type) {
2672 case PN533_DEVICE_STD:
2673 protocols = PN533_ALL_PROTOCOLS;
2676 case PN533_DEVICE_PASORI:
2677 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2681 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2687 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2688 PN533_FRAME_HEADER_LEN +
2689 PN533_CMD_DATAEXCH_HEAD_LEN,
2690 PN533_FRAME_TAIL_LEN);
2694 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2695 nfc_set_drvdata(dev->nfc_dev, dev);
2697 rc = nfc_register_device(dev->nfc_dev);
2701 rc = pn533_setup(dev);
2703 goto unregister_nfc_dev;
2708 nfc_unregister_device(dev->nfc_dev);
2711 nfc_free_device(dev->nfc_dev);
2714 destroy_workqueue(dev->wq);
2716 kfree(dev->in_frame);
2717 usb_free_urb(dev->in_urb);
2718 kfree(dev->out_frame);
2719 usb_free_urb(dev->out_urb);
2724 static void pn533_disconnect(struct usb_interface *interface)
2727 struct pn533_cmd *cmd, *n;
2729 dev = usb_get_intfdata(interface);
2730 usb_set_intfdata(interface, NULL);
2732 nfc_unregister_device(dev->nfc_dev);
2733 nfc_free_device(dev->nfc_dev);
2735 usb_kill_urb(dev->in_urb);
2736 usb_kill_urb(dev->out_urb);
2738 destroy_workqueue(dev->wq);
2740 skb_queue_purge(&dev->resp_q);
2742 del_timer(&dev->listen_timer);
2744 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2745 list_del(&cmd->queue);
2749 kfree(dev->in_frame);
2750 usb_free_urb(dev->in_urb);
2751 kfree(dev->out_frame);
2752 usb_free_urb(dev->out_urb);
2755 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2758 static struct usb_driver pn533_driver = {
2760 .probe = pn533_probe,
2761 .disconnect = pn533_disconnect,
2762 .id_table = pn533_table,
2765 module_usb_driver(pn533_driver);
2767 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2768 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2769 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2770 MODULE_VERSION(VERSION);
2771 MODULE_LICENSE("GPL");