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_response {
301 struct pn533_cmd_jump_dep_response {
315 /* PN533_TG_INIT_AS_TARGET */
316 #define PN533_INIT_TARGET_PASSIVE 0x1
317 #define PN533_INIT_TARGET_DEP 0x2
319 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
320 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
321 #define PN533_INIT_TARGET_RESP_DEP 0x4
324 struct usb_device *udev;
325 struct usb_interface *interface;
326 struct nfc_dev *nfc_dev;
329 struct pn533_frame *out_frame;
333 struct sk_buff_head resp_q;
335 struct workqueue_struct *wq;
336 struct work_struct cmd_work;
337 struct work_struct cmd_complete_work;
338 struct work_struct poll_work;
339 struct work_struct mi_work;
340 struct work_struct tg_work;
341 struct timer_list listen_timer;
342 struct pn533_frame *wq_in_frame;
346 pn533_cmd_complete_t cmd_complete;
347 void *cmd_complete_arg;
348 void *cmd_complete_mi_arg;
349 struct mutex cmd_lock;
352 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
356 u32 listen_protocols;
361 u8 tgt_available_prots;
367 struct list_head cmd_queue;
372 struct list_head queue;
375 struct sk_buff *resp;
387 /* The rule: value + checksum = 0 */
388 static inline u8 pn533_checksum(u8 value)
393 /* The rule: sum(data elements) + checksum = 0 */
394 static u8 pn533_data_checksum(u8 *data, int datalen)
399 for (i = 0; i < datalen; i++)
402 return pn533_checksum(sum);
406 * pn533_tx_frame_ack - create a ack frame
407 * @frame: The frame to be set as ack
409 * Ack is different type of standard frame. As a standard frame, it has
410 * preamble and start_frame. However the checksum of this frame must fail,
411 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
412 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
413 * After datalen_checksum field, the postamble is placed.
415 static void pn533_tx_frame_ack(struct pn533_frame *frame)
418 frame->start_frame = cpu_to_be16(PN533_SOF);
420 frame->datalen_checksum = 0xFF;
421 /* data[0] is used as postamble */
425 static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
428 frame->start_frame = cpu_to_be16(PN533_SOF);
429 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
430 PN533_FRAME_CMD(frame) = cmd;
434 static void pn533_tx_frame_finish(struct pn533_frame *frame)
436 frame->datalen_checksum = pn533_checksum(frame->datalen);
438 PN533_FRAME_CHECKSUM(frame) =
439 pn533_data_checksum(frame->data, frame->datalen);
441 PN533_FRAME_POSTAMBLE(frame) = 0;
444 static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
448 if (frame->start_frame != cpu_to_be16(PN533_SOF))
451 checksum = pn533_checksum(frame->datalen);
452 if (checksum != frame->datalen_checksum)
455 checksum = pn533_data_checksum(frame->data, frame->datalen);
456 if (checksum != PN533_FRAME_CHECKSUM(frame))
462 static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
464 if (frame->start_frame != cpu_to_be16(PN533_SOF))
467 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
473 static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
475 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
479 static void pn533_wq_cmd_complete(struct work_struct *work)
481 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
482 struct pn533_frame *in_frame;
485 in_frame = dev->wq_in_frame;
487 if (dev->wq_in_error)
488 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
491 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
492 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
493 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
495 if (rc != -EINPROGRESS)
496 queue_work(dev->wq, &dev->cmd_work);
499 static void pn533_recv_response(struct urb *urb)
501 struct pn533 *dev = urb->context;
502 struct pn533_frame *in_frame;
504 dev->wq_in_frame = NULL;
506 switch (urb->status) {
511 nfc_dev_dbg(&dev->interface->dev,
512 "The urb has been canceled (status %d)",
514 dev->wq_in_error = urb->status;
519 nfc_dev_err(&dev->interface->dev,
520 "Urb failure (status %d)", urb->status);
521 dev->wq_in_error = urb->status;
525 in_frame = dev->in_urb->transfer_buffer;
527 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
528 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
529 in_frame, PN533_FRAME_SIZE(in_frame), false);
531 if (!pn533_rx_frame_is_valid(in_frame)) {
532 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
533 dev->wq_in_error = -EIO;
537 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
538 nfc_dev_err(&dev->interface->dev,
539 "It it not the response to the last command");
540 dev->wq_in_error = -EIO;
544 dev->wq_in_error = 0;
545 dev->wq_in_frame = in_frame;
548 queue_work(dev->wq, &dev->cmd_complete_work);
551 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
553 dev->in_urb->complete = pn533_recv_response;
555 return usb_submit_urb(dev->in_urb, flags);
558 static void pn533_recv_ack(struct urb *urb)
560 struct pn533 *dev = urb->context;
561 struct pn533_frame *in_frame;
564 switch (urb->status) {
569 nfc_dev_dbg(&dev->interface->dev,
570 "The urb has been stopped (status %d)",
572 dev->wq_in_error = urb->status;
577 nfc_dev_err(&dev->interface->dev,
578 "Urb failure (status %d)", urb->status);
579 dev->wq_in_error = urb->status;
583 in_frame = dev->in_urb->transfer_buffer;
585 if (!pn533_rx_frame_is_ack(in_frame)) {
586 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
587 dev->wq_in_error = -EIO;
591 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
593 nfc_dev_err(&dev->interface->dev,
594 "usb_submit_urb failed with result %d", rc);
595 dev->wq_in_error = rc;
602 dev->wq_in_frame = NULL;
603 queue_work(dev->wq, &dev->cmd_complete_work);
606 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
608 dev->in_urb->complete = pn533_recv_ack;
610 return usb_submit_urb(dev->in_urb, flags);
613 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
617 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
619 pn533_tx_frame_ack(dev->out_frame);
621 dev->out_urb->transfer_buffer = dev->out_frame;
622 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
623 rc = usb_submit_urb(dev->out_urb, flags);
628 static int __pn533_send_cmd_frame_async(struct pn533 *dev,
629 struct pn533_frame *out_frame,
630 struct pn533_frame *in_frame,
632 pn533_cmd_complete_t cmd_complete,
637 dev->cmd = PN533_FRAME_CMD(out_frame);
638 dev->cmd_complete = cmd_complete;
639 dev->cmd_complete_arg = arg;
641 dev->out_urb->transfer_buffer = out_frame;
642 dev->out_urb->transfer_buffer_length =
643 PN533_FRAME_SIZE(out_frame);
645 dev->in_urb->transfer_buffer = in_frame;
646 dev->in_urb->transfer_buffer_length = in_frame_len;
648 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
649 out_frame, PN533_FRAME_SIZE(out_frame), false);
651 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
655 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
662 usb_unlink_urb(dev->out_urb);
666 static void pn533_build_cmd_frame(u8 cmd_code, struct sk_buff *skb)
668 struct pn533_frame *frame;
669 /* payload is already there, just update datalen */
670 int payload_len = skb->len;
672 skb_push(skb, PN533_FRAME_HEADER_LEN);
673 skb_put(skb, PN533_FRAME_TAIL_LEN);
675 frame = (struct pn533_frame *)skb->data;
677 pn533_tx_frame_init(frame, cmd_code);
678 frame->datalen += payload_len;
679 pn533_tx_frame_finish(frame);
682 struct pn533_send_async_complete_arg {
683 pn533_send_async_complete_t complete_cb;
684 void *complete_cb_context;
685 struct sk_buff *resp;
689 static int pn533_send_async_complete(struct pn533 *dev, void *_arg, u8 *params,
692 struct pn533_send_async_complete_arg *arg = _arg;
694 struct sk_buff *req = arg->req;
695 struct sk_buff *resp = arg->resp;
697 struct pn533_frame *frame = (struct pn533_frame *)resp->data;
702 if (params_len < 0) {
703 arg->complete_cb(dev, arg->complete_cb_context,
704 ERR_PTR(params_len));
710 skb_put(resp, PN533_FRAME_SIZE(frame));
711 skb_pull(resp, PN533_FRAME_HEADER_LEN);
712 skb_trim(resp, resp->len - PN533_FRAME_TAIL_LEN);
714 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
721 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
722 struct sk_buff *req, struct sk_buff *resp,
724 pn533_send_async_complete_t complete_cb,
725 void *complete_cb_context)
727 struct pn533_cmd *cmd;
728 struct pn533_send_async_complete_arg *arg;
731 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
733 arg = kzalloc(sizeof(arg), GFP_KERNEL);
737 arg->complete_cb = complete_cb;
738 arg->complete_cb_context = complete_cb_context;
742 pn533_build_cmd_frame(cmd_code, req);
744 mutex_lock(&dev->cmd_lock);
746 if (!dev->cmd_pending) {
747 rc = __pn533_send_cmd_frame_async(dev,
748 (struct pn533_frame *)req->data,
749 (struct pn533_frame *)resp->data,
750 resp_len, pn533_send_async_complete,
755 dev->cmd_pending = 1;
759 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
762 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
768 INIT_LIST_HEAD(&cmd->queue);
769 cmd->cmd_code = cmd_code;
774 list_add_tail(&cmd->queue, &dev->cmd_queue);
781 mutex_unlock(&dev->cmd_lock);
785 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
787 pn533_send_async_complete_t complete_cb,
788 void *complete_cb_context)
790 struct sk_buff *resp;
792 int resp_len = PN533_FRAME_HEADER_LEN +
793 PN533_FRAME_MAX_PAYLOAD_LEN +
794 PN533_FRAME_TAIL_LEN;
796 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
800 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
801 complete_cb_context);
808 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
810 pn533_send_async_complete_t complete_cb,
811 void *complete_cb_context)
813 struct sk_buff *resp;
816 resp = alloc_skb(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
820 rc = __pn533_send_async(dev, cmd_code, req, resp,
821 PN533_NORMAL_FRAME_MAX_LEN,
822 complete_cb, complete_cb_context);
830 * pn533_send_cmd_direct_async
832 * The function sends a piority cmd directly to the chip omiting the cmd
833 * queue. It's intended to be used by chaining mechanism of received responses
834 * where the host has to request every single chunk of data before scheduling
835 * next cmd from the queue.
837 static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
839 pn533_send_async_complete_t complete_cb,
840 void *complete_cb_context)
842 struct pn533_send_async_complete_arg *arg;
843 struct sk_buff *resp;
845 int resp_len = PN533_FRAME_HEADER_LEN +
846 PN533_FRAME_MAX_PAYLOAD_LEN +
847 PN533_FRAME_TAIL_LEN;
849 resp = alloc_skb(resp_len, GFP_KERNEL);
853 arg = kzalloc(sizeof(arg), GFP_KERNEL);
859 arg->complete_cb = complete_cb;
860 arg->complete_cb_context = complete_cb_context;
864 pn533_build_cmd_frame(cmd_code, req);
866 rc = __pn533_send_cmd_frame_async(dev, (struct pn533_frame *)req->data,
867 (struct pn533_frame *)resp->data,
868 resp_len, pn533_send_async_complete,
878 static void pn533_wq_cmd(struct work_struct *work)
880 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
881 struct pn533_cmd *cmd;
883 mutex_lock(&dev->cmd_lock);
885 if (list_empty(&dev->cmd_queue)) {
886 dev->cmd_pending = 0;
887 mutex_unlock(&dev->cmd_lock);
891 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
893 list_del(&cmd->queue);
895 mutex_unlock(&dev->cmd_lock);
897 __pn533_send_cmd_frame_async(dev,
898 (struct pn533_frame *)cmd->req->data,
899 (struct pn533_frame *)cmd->resp->data,
900 PN533_NORMAL_FRAME_MAX_LEN,
901 pn533_send_async_complete,
907 struct pn533_sync_cmd_response {
908 struct sk_buff *resp;
909 struct completion done;
912 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
913 struct sk_buff *resp)
915 struct pn533_sync_cmd_response *arg = _arg;
918 complete(&arg->done);
923 /* pn533_send_cmd_sync
925 * Please note the req parameter is freed inside the function to
926 * limit a number of return value interpretations by the caller.
928 * 1. negative in case of error during TX path -> req should be freed
930 * 2. negative in case of error during RX path -> req should not be freed
931 * as it's been already freed at the begining of RX path by
934 * 3. valid pointer in case of succesfult RX path
936 * A caller has to check a return value with IS_ERR macro. If the test pass,
937 * the returned pointer is valid.
940 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
944 struct pn533_sync_cmd_response arg;
946 init_completion(&arg.done);
948 rc = pn533_send_cmd_async(dev, cmd_code, req,
949 pn533_send_sync_complete, &arg);
955 wait_for_completion(&arg.done);
960 static void pn533_send_complete(struct urb *urb)
962 struct pn533 *dev = urb->context;
964 switch (urb->status) {
969 nfc_dev_dbg(&dev->interface->dev,
970 "The urb has been stopped (status %d)",
975 nfc_dev_err(&dev->interface->dev,
976 "Urb failure (status %d)", urb->status);
980 static struct sk_buff *pn533_alloc_skb(unsigned int size)
984 skb = alloc_skb(PN533_FRAME_HEADER_LEN +
986 PN533_FRAME_TAIL_LEN, GFP_KERNEL);
989 skb_reserve(skb, PN533_FRAME_HEADER_LEN);
994 struct pn533_target_type_a {
1002 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1003 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1004 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1006 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1007 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1009 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1010 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1012 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1013 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1014 #define PN533_TYPE_A_SEL_PROT_DEP 2
1015 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1017 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1018 int target_data_len)
1023 if (target_data_len < sizeof(struct pn533_target_type_a))
1026 /* The lenght check of nfcid[] and ats[] are not being performed because
1027 the values are not being used */
1029 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1030 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1031 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1033 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1034 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1035 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1036 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1039 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1040 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1046 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1049 struct pn533_target_type_a *tgt_type_a;
1051 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1053 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1056 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1057 case PN533_TYPE_A_SEL_PROT_MIFARE:
1058 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1060 case PN533_TYPE_A_SEL_PROT_ISO14443:
1061 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1063 case PN533_TYPE_A_SEL_PROT_DEP:
1064 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1066 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1067 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1068 NFC_PROTO_NFC_DEP_MASK;
1072 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1073 nfc_tgt->sel_res = tgt_type_a->sel_res;
1074 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1075 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1080 struct pn533_target_felica {
1089 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1090 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1092 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1093 int target_data_len)
1095 if (target_data_len < sizeof(struct pn533_target_felica))
1098 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1104 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1107 struct pn533_target_felica *tgt_felica;
1109 tgt_felica = (struct pn533_target_felica *)tgt_data;
1111 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1114 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1115 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1116 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1118 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1120 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1121 nfc_tgt->sensf_res_len = 9;
1126 struct pn533_target_jewel {
1131 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1132 int target_data_len)
1137 if (target_data_len < sizeof(struct pn533_target_jewel))
1140 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1141 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1142 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1144 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1145 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1146 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1147 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1153 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1156 struct pn533_target_jewel *tgt_jewel;
1158 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1160 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1163 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1164 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1165 nfc_tgt->nfcid1_len = 4;
1166 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1171 struct pn533_type_b_prot_info {
1177 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1178 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1179 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1181 struct pn533_type_b_sens_res {
1185 struct pn533_type_b_prot_info prot_info;
1188 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1190 struct pn533_target_type_b {
1191 struct pn533_type_b_sens_res sensb_res;
1196 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1197 int target_data_len)
1199 if (target_data_len < sizeof(struct pn533_target_type_b))
1202 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1205 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1206 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1212 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1215 struct pn533_target_type_b *tgt_type_b;
1217 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1219 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1222 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1227 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1230 struct nfc_target nfc_tgt;
1233 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1234 dev->poll_mod_curr);
1239 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1241 switch (dev->poll_mod_curr) {
1242 case PN533_POLL_MOD_106KBPS_A:
1243 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1245 case PN533_POLL_MOD_212KBPS_FELICA:
1246 case PN533_POLL_MOD_424KBPS_FELICA:
1247 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1249 case PN533_POLL_MOD_106KBPS_JEWEL:
1250 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1252 case PN533_POLL_MOD_847KBPS_B:
1253 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1256 nfc_dev_err(&dev->interface->dev,
1257 "Unknown current poll modulation");
1264 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1265 nfc_dev_dbg(&dev->interface->dev,
1266 "The Tg found doesn't have the desired protocol");
1270 nfc_dev_dbg(&dev->interface->dev,
1271 "Target found - supported protocols: 0x%x",
1272 nfc_tgt.supported_protocols);
1274 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1276 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1281 static inline void pn533_poll_next_mod(struct pn533 *dev)
1283 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1286 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1288 dev->poll_mod_count = 0;
1291 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1293 dev->poll_mod_active[dev->poll_mod_count] =
1294 (struct pn533_poll_modulations *)&poll_mod[mod_index];
1295 dev->poll_mod_count++;
1298 static void pn533_poll_create_mod_list(struct pn533 *dev,
1299 u32 im_protocols, u32 tm_protocols)
1301 pn533_poll_reset_mod_list(dev);
1303 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1304 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1305 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1306 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1308 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1309 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1310 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1311 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1314 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1315 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1317 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1318 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1321 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1324 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1326 u8 nbtg, tg, *tgdata;
1329 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1331 nbtg = resp->data[0];
1333 tgdata = &resp->data[2];
1334 tgdata_len = resp->len - 2; /* nbtg + tg */
1337 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1339 /* We must stop the poll after a valid target found */
1341 pn533_poll_reset_mod_list(dev);
1349 static struct sk_buff *pn533_alloc_poll_tg_frame(u8 *gbytes, size_t gbytes_len)
1351 struct sk_buff *skb;
1352 u8 *felica, *nfcid3, *gb;
1354 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1355 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1356 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1357 0xff, 0xff}; /* System code */
1359 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1361 0x40}; /* SEL_RES for DEP */
1363 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1364 felica (18), nfcid3 (10), gb_len (1) */
1368 skb = pn533_alloc_skb(skb_len);
1372 /* DEP support only */
1373 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
1376 memcpy(skb_put(skb, 6), mifare_params, 6);
1379 felica = skb_put(skb, 18);
1380 memcpy(felica, felica_params, 18);
1381 get_random_bytes(felica + 2, 6);
1384 nfcid3 = skb_put(skb, 10);
1385 memset(nfcid3, 0, 10);
1386 memcpy(nfcid3, felica, 8);
1389 *skb_put(skb, 1) = gbytes_len;
1391 gb = skb_put(skb, gbytes_len);
1392 memcpy(gb, gbytes, gbytes_len);
1395 *skb_put(skb, 1) = 0;
1400 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1401 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1402 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1403 struct sk_buff *resp)
1407 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1410 return PTR_ERR(resp);
1412 status = resp->data[0];
1413 skb_pull(resp, sizeof(status));
1416 nfc_tm_deactivated(dev->nfc_dev);
1418 dev_kfree_skb(resp);
1422 return nfc_tm_data_received(dev->nfc_dev, resp);
1425 static void pn533_wq_tg_get_data(struct work_struct *work)
1427 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1429 struct sk_buff *skb;
1432 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1434 skb = pn533_alloc_skb(0);
1438 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1439 pn533_tm_get_data_complete, NULL);
1447 #define ATR_REQ_GB_OFFSET 17
1448 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1450 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1454 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1456 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1459 mode = resp->data[0];
1460 cmd = &resp->data[1];
1462 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1465 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1466 PN533_INIT_TARGET_RESP_ACTIVE)
1467 comm_mode = NFC_COMM_ACTIVE;
1469 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1472 gb = cmd + ATR_REQ_GB_OFFSET;
1473 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1475 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1476 comm_mode, gb, gb_len);
1478 nfc_dev_err(&dev->interface->dev,
1479 "Error when signaling target activation");
1484 queue_work(dev->wq, &dev->tg_work);
1489 static void pn533_listen_mode_timer(unsigned long data)
1491 struct pn533 *dev = (struct pn533 *)data;
1493 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1495 /* An ack will cancel the last issued command (poll) */
1496 pn533_send_ack(dev, GFP_ATOMIC);
1498 dev->cancel_listen = 1;
1500 pn533_poll_next_mod(dev);
1502 queue_work(dev->wq, &dev->poll_work);
1505 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1506 struct sk_buff *resp)
1508 struct pn533_poll_modulations *cur_mod;
1511 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1516 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1519 if (rc == -ENOENT) {
1520 if (dev->poll_mod_count != 0)
1524 } else if (rc < 0) {
1525 nfc_dev_err(&dev->interface->dev,
1526 "Error %d when running poll", rc);
1531 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1533 if (cur_mod->len == 0) { /* Target mode */
1534 del_timer(&dev->listen_timer);
1535 rc = pn533_init_target_complete(dev, resp);
1539 /* Initiator mode */
1540 rc = pn533_start_poll_complete(dev, resp);
1544 pn533_poll_next_mod(dev);
1545 queue_work(dev->wq, &dev->poll_work);
1548 dev_kfree_skb(resp);
1552 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1554 pn533_poll_reset_mod_list(dev);
1555 dev->poll_protocols = 0;
1559 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533_poll_modulations
1562 struct sk_buff *skb;
1564 skb = pn533_alloc_skb(mod->len);
1568 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1573 static int pn533_send_poll_frame(struct pn533 *dev)
1575 struct pn533_poll_modulations *mod;
1576 struct sk_buff *skb;
1580 mod = dev->poll_mod_active[dev->poll_mod_curr];
1582 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1583 __func__, mod->len);
1585 if (mod->len == 0) { /* Listen mode */
1586 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1587 skb = pn533_alloc_poll_tg_frame(dev->gb, dev->gb_len);
1588 } else { /* Polling mode */
1589 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1590 skb = pn533_alloc_poll_in_frame(mod);
1594 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1598 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1602 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1608 static void pn533_wq_poll(struct work_struct *work)
1610 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1611 struct pn533_poll_modulations *cur_mod;
1614 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1616 nfc_dev_dbg(&dev->interface->dev,
1617 "%s cancel_listen %d modulation len %d",
1618 __func__, dev->cancel_listen, cur_mod->len);
1620 if (dev->cancel_listen == 1) {
1621 dev->cancel_listen = 0;
1622 usb_kill_urb(dev->in_urb);
1625 rc = pn533_send_poll_frame(dev);
1629 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1630 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1635 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1636 u32 im_protocols, u32 tm_protocols)
1638 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1640 nfc_dev_dbg(&dev->interface->dev,
1641 "%s: im protocols 0x%x tm protocols 0x%x",
1642 __func__, im_protocols, tm_protocols);
1644 if (dev->tgt_active_prot) {
1645 nfc_dev_err(&dev->interface->dev,
1646 "Cannot poll with a target already activated");
1650 if (dev->tgt_mode) {
1651 nfc_dev_err(&dev->interface->dev,
1652 "Cannot poll while already being activated");
1657 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1658 if (dev->gb == NULL)
1662 dev->poll_mod_curr = 0;
1663 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1664 dev->poll_protocols = im_protocols;
1665 dev->listen_protocols = tm_protocols;
1667 return pn533_send_poll_frame(dev);
1670 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1672 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1674 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1676 del_timer(&dev->listen_timer);
1678 if (!dev->poll_mod_count) {
1679 nfc_dev_dbg(&dev->interface->dev,
1680 "Polling operation was not running");
1684 /* An ack will cancel the last issued command (poll) */
1685 pn533_send_ack(dev, GFP_KERNEL);
1687 /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
1688 usb_kill_urb(dev->in_urb);
1690 pn533_poll_reset_mod_list(dev);
1693 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1695 struct pn533_cmd_activate_response *rsp;
1699 struct sk_buff *skb;
1700 struct sk_buff *resp;
1702 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1704 skb = pn533_alloc_skb(sizeof(u8) * 2); /*TG + Next*/
1708 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1709 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1711 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1713 return PTR_ERR(resp);
1715 rsp = (struct pn533_cmd_activate_response *)resp->data;
1716 rc = rsp->status & PN533_CMD_RET_MASK;
1717 if (rc != PN533_CMD_RET_SUCCESS)
1718 dev_kfree_skb(resp);
1721 /* ATR_RES general bytes are located at offset 16 */
1722 gt_len = resp->len - 16;
1723 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1725 dev_kfree_skb(resp);
1729 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1730 struct nfc_target *target, u32 protocol)
1732 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1735 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1738 if (dev->poll_mod_count) {
1739 nfc_dev_err(&dev->interface->dev,
1740 "Cannot activate while polling");
1744 if (dev->tgt_active_prot) {
1745 nfc_dev_err(&dev->interface->dev,
1746 "There is already an active target");
1750 if (!dev->tgt_available_prots) {
1751 nfc_dev_err(&dev->interface->dev,
1752 "There is no available target to activate");
1756 if (!(dev->tgt_available_prots & (1 << protocol))) {
1757 nfc_dev_err(&dev->interface->dev,
1758 "Target doesn't support requested proto %u",
1763 if (protocol == NFC_PROTO_NFC_DEP) {
1764 rc = pn533_activate_target_nfcdep(dev);
1766 nfc_dev_err(&dev->interface->dev,
1767 "Activating target with DEP failed %d", rc);
1772 dev->tgt_active_prot = protocol;
1773 dev->tgt_available_prots = 0;
1778 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1779 struct nfc_target *target)
1781 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1783 struct sk_buff *skb;
1784 struct sk_buff *resp;
1788 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1790 if (!dev->tgt_active_prot) {
1791 nfc_dev_err(&dev->interface->dev, "There is no active target");
1795 dev->tgt_active_prot = 0;
1796 skb_queue_purge(&dev->resp_q);
1798 skb = pn533_alloc_skb(sizeof(u8));
1802 *skb_put(skb, 1) = 1; /* TG*/
1804 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1808 rc = resp->data[0] & PN533_CMD_RET_MASK;
1809 if (rc != PN533_CMD_RET_SUCCESS)
1810 nfc_dev_err(&dev->interface->dev,
1811 "Error 0x%x when releasing the target", rc);
1813 dev_kfree_skb(resp);
1818 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1819 struct sk_buff *resp)
1821 struct pn533_cmd_jump_dep_response *rsp;
1824 u8 active = *(u8 *)arg;
1829 return PTR_ERR(resp);
1831 if (dev->tgt_available_prots &&
1832 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1833 nfc_dev_err(&dev->interface->dev,
1834 "The target does not support DEP");
1839 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1841 rc = rsp->status & PN533_CMD_RET_MASK;
1842 if (rc != PN533_CMD_RET_SUCCESS) {
1843 nfc_dev_err(&dev->interface->dev,
1844 "Bringing DEP link up failed %d", rc);
1848 if (!dev->tgt_available_prots) {
1849 struct nfc_target nfc_target;
1851 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1853 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1854 nfc_target.nfcid1_len = 10;
1855 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1856 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1860 dev->tgt_available_prots = 0;
1863 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1865 /* ATR_RES general bytes are located at offset 17 */
1866 target_gt_len = resp->len - 17;
1867 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1868 rsp->gt, target_gt_len);
1870 rc = nfc_dep_link_is_up(dev->nfc_dev,
1871 dev->nfc_dev->targets[0].idx,
1872 !active, NFC_RF_INITIATOR);
1875 dev_kfree_skb(resp);
1879 static int pn533_mod_to_baud(struct pn533 *dev)
1881 switch (dev->poll_mod_curr) {
1882 case PN533_POLL_MOD_106KBPS_A:
1884 case PN533_POLL_MOD_212KBPS_FELICA:
1886 case PN533_POLL_MOD_424KBPS_FELICA:
1893 #define PASSIVE_DATA_LEN 5
1894 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1895 u8 comm_mode, u8 *gb, size_t gb_len)
1897 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1898 struct sk_buff *skb;
1899 int rc, baud, skb_len;
1902 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1904 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1906 if (dev->poll_mod_count) {
1907 nfc_dev_err(&dev->interface->dev,
1908 "Cannot bring the DEP link up while polling");
1912 if (dev->tgt_active_prot) {
1913 nfc_dev_err(&dev->interface->dev,
1914 "There is already an active target");
1918 baud = pn533_mod_to_baud(dev);
1920 nfc_dev_err(&dev->interface->dev,
1921 "Invalid curr modulation %d", dev->poll_mod_curr);
1925 skb_len = 3 + gb_len; /* ActPass + BR + Next */
1926 if (comm_mode == NFC_COMM_PASSIVE)
1927 skb_len += PASSIVE_DATA_LEN;
1929 skb = pn533_alloc_skb(skb_len);
1933 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1934 *skb_put(skb, 1) = baud; /* Baud rate */
1936 next = skb_put(skb, 1); /* Next */
1939 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1940 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1945 if (gb != NULL && gb_len > 0) {
1946 memcpy(skb_put(skb, gb_len), gb, gb_len);
1947 *next |= 4; /* We have some Gi */
1952 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1960 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1961 pn533_in_dep_link_up_complete, arg);
1971 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1973 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1975 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1977 pn533_poll_reset_mod_list(dev);
1979 if (dev->tgt_mode || dev->tgt_active_prot) {
1980 pn533_send_ack(dev, GFP_KERNEL);
1981 usb_kill_urb(dev->in_urb);
1984 dev->tgt_active_prot = 0;
1987 skb_queue_purge(&dev->resp_q);
1992 struct pn533_data_exchange_arg {
1993 data_exchange_cb_t cb;
1997 static struct sk_buff *pn533_build_response(struct pn533 *dev)
1999 struct sk_buff *skb, *tmp, *t;
2000 unsigned int skb_len = 0, tmp_len = 0;
2002 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2004 if (skb_queue_empty(&dev->resp_q))
2007 if (skb_queue_len(&dev->resp_q) == 1) {
2008 skb = skb_dequeue(&dev->resp_q);
2012 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2013 skb_len += tmp->len;
2015 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2018 skb = alloc_skb(skb_len, GFP_KERNEL);
2022 skb_put(skb, skb_len);
2024 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2025 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2026 tmp_len += tmp->len;
2030 skb_queue_purge(&dev->resp_q);
2035 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2036 struct sk_buff *resp)
2038 struct pn533_data_exchange_arg *arg = _arg;
2039 struct sk_buff *skb;
2043 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2050 status = resp->data[0];
2051 ret = status & PN533_CMD_RET_MASK;
2052 mi = status & PN533_CMD_MI_MASK;
2054 skb_pull(resp, sizeof(status));
2056 if (ret != PN533_CMD_RET_SUCCESS) {
2057 nfc_dev_err(&dev->interface->dev,
2058 "PN533 reported error %d when exchanging data",
2064 skb_queue_tail(&dev->resp_q, resp);
2067 dev->cmd_complete_mi_arg = arg;
2068 queue_work(dev->wq, &dev->mi_work);
2069 return -EINPROGRESS;
2072 skb = pn533_build_response(dev);
2076 arg->cb(arg->cb_context, skb, 0);
2081 dev_kfree_skb(resp);
2083 skb_queue_purge(&dev->resp_q);
2084 arg->cb(arg->cb_context, NULL, rc);
2089 static int pn533_transceive(struct nfc_dev *nfc_dev,
2090 struct nfc_target *target, struct sk_buff *skb,
2091 data_exchange_cb_t cb, void *cb_context)
2093 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2094 struct pn533_data_exchange_arg *arg = NULL;
2097 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2099 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2100 /* TODO: Implement support to multi-part data exchange */
2101 nfc_dev_err(&dev->interface->dev,
2102 "Data length greater than the max allowed: %d",
2103 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2108 if (!dev->tgt_active_prot) {
2109 nfc_dev_err(&dev->interface->dev,
2110 "Can't exchange data if there is no active target");
2115 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2122 arg->cb_context = cb_context;
2124 switch (dev->device_type) {
2125 case PN533_DEVICE_PASORI:
2126 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2127 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2129 pn533_data_exchange_complete,
2135 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2137 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2138 skb, pn533_data_exchange_complete,
2144 if (rc < 0) /* rc from send_async */
2155 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2156 struct sk_buff *resp)
2160 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2163 return PTR_ERR(resp);
2165 status = resp->data[0];
2167 dev_kfree_skb(resp);
2170 nfc_tm_deactivated(dev->nfc_dev);
2177 queue_work(dev->wq, &dev->tg_work);
2182 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2184 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2187 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2189 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2190 nfc_dev_err(&dev->interface->dev,
2191 "Data length greater than the max allowed: %d",
2192 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2196 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2197 pn533_tm_send_complete, NULL);
2204 static void pn533_wq_mi_recv(struct work_struct *work)
2206 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2208 struct sk_buff *skb;
2211 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2213 skb = pn533_alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN);
2217 switch (dev->device_type) {
2218 case PN533_DEVICE_PASORI:
2219 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2220 rc = pn533_send_cmd_direct_async(dev,
2221 PN533_CMD_IN_COMM_THRU,
2223 pn533_data_exchange_complete,
2224 dev->cmd_complete_mi_arg);
2229 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
2231 rc = pn533_send_cmd_direct_async(dev,
2232 PN533_CMD_IN_DATA_EXCHANGE,
2234 pn533_data_exchange_complete,
2235 dev->cmd_complete_mi_arg);
2240 if (rc == 0) /* success */
2243 nfc_dev_err(&dev->interface->dev,
2244 "Error %d when trying to perform data_exchange", rc);
2247 kfree(dev->cmd_complete_arg);
2250 pn533_send_ack(dev, GFP_KERNEL);
2251 queue_work(dev->wq, &dev->cmd_work);
2254 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2257 struct sk_buff *skb;
2258 struct sk_buff *resp;
2262 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2264 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2266 skb = pn533_alloc_skb(skb_len);
2270 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2271 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2273 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2275 return PTR_ERR(resp);
2277 dev_kfree_skb(resp);
2281 static int pn533_get_firmware_version(struct pn533 *dev,
2282 struct pn533_fw_version *fv)
2284 struct sk_buff *skb;
2285 struct sk_buff *resp;
2287 skb = pn533_alloc_skb(0);
2291 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2293 return PTR_ERR(resp);
2295 fv->ic = resp->data[0];
2296 fv->ver = resp->data[1];
2297 fv->rev = resp->data[2];
2298 fv->support = resp->data[3];
2300 dev_kfree_skb(resp);
2304 static int pn533_fw_reset(struct pn533 *dev)
2306 struct sk_buff *skb;
2307 struct sk_buff *resp;
2309 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2311 skb = pn533_alloc_skb(sizeof(u8));
2315 *skb_put(skb, sizeof(u8)) = 0x1;
2317 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2319 return PTR_ERR(resp);
2321 dev_kfree_skb(resp);
2326 static struct nfc_ops pn533_nfc_ops = {
2329 .dep_link_up = pn533_dep_link_up,
2330 .dep_link_down = pn533_dep_link_down,
2331 .start_poll = pn533_start_poll,
2332 .stop_poll = pn533_stop_poll,
2333 .activate_target = pn533_activate_target,
2334 .deactivate_target = pn533_deactivate_target,
2335 .im_transceive = pn533_transceive,
2336 .tm_send = pn533_tm_send,
2339 static int pn533_setup(struct pn533 *dev)
2341 struct pn533_config_max_retries max_retries;
2342 struct pn533_config_timing timing;
2343 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2346 switch (dev->device_type) {
2347 case PN533_DEVICE_STD:
2348 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2349 max_retries.mx_rty_psl = 2;
2350 max_retries.mx_rty_passive_act =
2351 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2353 timing.rfu = PN533_CONFIG_TIMING_102;
2354 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2355 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2359 case PN533_DEVICE_PASORI:
2360 max_retries.mx_rty_atr = 0x2;
2361 max_retries.mx_rty_psl = 0x1;
2362 max_retries.mx_rty_passive_act =
2363 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2365 timing.rfu = PN533_CONFIG_TIMING_102;
2366 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2367 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2372 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2377 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2378 (u8 *)&max_retries, sizeof(max_retries));
2380 nfc_dev_err(&dev->interface->dev,
2381 "Error on setting MAX_RETRIES config");
2386 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2387 (u8 *)&timing, sizeof(timing));
2389 nfc_dev_err(&dev->interface->dev,
2390 "Error on setting RF timings");
2394 switch (dev->device_type) {
2395 case PN533_DEVICE_STD:
2398 case PN533_DEVICE_PASORI:
2399 pn533_fw_reset(dev);
2401 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2404 nfc_dev_err(&dev->interface->dev,
2405 "Error while settings PASORI config");
2409 pn533_fw_reset(dev);
2417 static int pn533_probe(struct usb_interface *interface,
2418 const struct usb_device_id *id)
2420 struct pn533_fw_version fw_ver;
2422 struct usb_host_interface *iface_desc;
2423 struct usb_endpoint_descriptor *endpoint;
2424 int in_endpoint = 0;
2425 int out_endpoint = 0;
2430 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2434 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2435 dev->interface = interface;
2436 mutex_init(&dev->cmd_lock);
2438 iface_desc = interface->cur_altsetting;
2439 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2440 endpoint = &iface_desc->endpoint[i].desc;
2442 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2443 in_endpoint = endpoint->bEndpointAddress;
2445 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2446 out_endpoint = endpoint->bEndpointAddress;
2449 if (!in_endpoint || !out_endpoint) {
2450 nfc_dev_err(&interface->dev,
2451 "Could not find bulk-in or bulk-out endpoint");
2456 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2457 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2458 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2460 if (!dev->out_frame || !dev->in_urb || !dev->out_urb)
2463 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2464 usb_rcvbulkpipe(dev->udev, in_endpoint),
2465 NULL, 0, NULL, dev);
2466 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2467 usb_sndbulkpipe(dev->udev, out_endpoint),
2468 NULL, 0, pn533_send_complete, dev);
2470 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2471 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2472 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2473 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2474 INIT_WORK(&dev->poll_work, pn533_wq_poll);
2475 dev->wq = alloc_ordered_workqueue("pn533", 0);
2476 if (dev->wq == NULL)
2479 init_timer(&dev->listen_timer);
2480 dev->listen_timer.data = (unsigned long) dev;
2481 dev->listen_timer.function = pn533_listen_mode_timer;
2483 skb_queue_head_init(&dev->resp_q);
2485 INIT_LIST_HEAD(&dev->cmd_queue);
2487 usb_set_intfdata(interface, dev);
2489 memset(&fw_ver, 0, sizeof(fw_ver));
2490 rc = pn533_get_firmware_version(dev, &fw_ver);
2494 nfc_dev_info(&dev->interface->dev,
2495 "NXP PN533 firmware ver %d.%d now attached",
2496 fw_ver.ver, fw_ver.rev);
2498 dev->device_type = id->driver_info;
2499 switch (dev->device_type) {
2500 case PN533_DEVICE_STD:
2501 protocols = PN533_ALL_PROTOCOLS;
2504 case PN533_DEVICE_PASORI:
2505 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2509 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2515 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2516 PN533_FRAME_HEADER_LEN +
2517 PN533_CMD_DATAEXCH_HEAD_LEN,
2518 PN533_FRAME_TAIL_LEN);
2522 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2523 nfc_set_drvdata(dev->nfc_dev, dev);
2525 rc = nfc_register_device(dev->nfc_dev);
2529 rc = pn533_setup(dev);
2531 goto unregister_nfc_dev;
2536 nfc_unregister_device(dev->nfc_dev);
2539 nfc_free_device(dev->nfc_dev);
2542 destroy_workqueue(dev->wq);
2544 usb_free_urb(dev->in_urb);
2545 kfree(dev->out_frame);
2546 usb_free_urb(dev->out_urb);
2551 static void pn533_disconnect(struct usb_interface *interface)
2554 struct pn533_cmd *cmd, *n;
2556 dev = usb_get_intfdata(interface);
2557 usb_set_intfdata(interface, NULL);
2559 nfc_unregister_device(dev->nfc_dev);
2560 nfc_free_device(dev->nfc_dev);
2562 usb_kill_urb(dev->in_urb);
2563 usb_kill_urb(dev->out_urb);
2565 destroy_workqueue(dev->wq);
2567 skb_queue_purge(&dev->resp_q);
2569 del_timer(&dev->listen_timer);
2571 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2572 list_del(&cmd->queue);
2576 usb_free_urb(dev->in_urb);
2577 kfree(dev->out_frame);
2578 usb_free_urb(dev->out_urb);
2581 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2584 static struct usb_driver pn533_driver = {
2586 .probe = pn533_probe,
2587 .disconnect = pn533_disconnect,
2588 .id_table = pn533_table,
2591 module_usb_driver(pn533_driver);
2593 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
2594 " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2595 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2596 MODULE_VERSION(VERSION);
2597 MODULE_LICENSE("GPL");