2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 * Copyright (C) 2012-2013 Tieto Poland
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/usb.h>
26 #include <linux/nfc.h>
27 #include <linux/netdevice.h>
28 #include <net/nfc/nfc.h>
32 #define PN533_VENDOR_ID 0x4CC
33 #define PN533_PRODUCT_ID 0x2533
35 #define SCM_VENDOR_ID 0x4E6
36 #define SCL3711_PRODUCT_ID 0x5591
38 #define SONY_VENDOR_ID 0x054c
39 #define PASORI_PRODUCT_ID 0x02e1
41 #define ACS_VENDOR_ID 0x072f
42 #define ACR122U_PRODUCT_ID 0x2200
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
46 #define PN533_DEVICE_ACR122U 0x3
48 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
49 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
50 NFC_PROTO_NFC_DEP_MASK |\
51 NFC_PROTO_ISO14443_B_MASK)
53 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
54 NFC_PROTO_MIFARE_MASK | \
55 NFC_PROTO_FELICA_MASK | \
56 NFC_PROTO_ISO14443_MASK | \
57 NFC_PROTO_NFC_DEP_MASK)
59 static const struct usb_device_id pn533_table[] = {
60 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
61 .idVendor = PN533_VENDOR_ID,
62 .idProduct = PN533_PRODUCT_ID,
63 .driver_info = PN533_DEVICE_STD,
65 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
66 .idVendor = SCM_VENDOR_ID,
67 .idProduct = SCL3711_PRODUCT_ID,
68 .driver_info = PN533_DEVICE_STD,
70 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
71 .idVendor = SONY_VENDOR_ID,
72 .idProduct = PASORI_PRODUCT_ID,
73 .driver_info = PN533_DEVICE_PASORI,
75 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
76 .idVendor = ACS_VENDOR_ID,
77 .idProduct = ACR122U_PRODUCT_ID,
78 .driver_info = PN533_DEVICE_ACR122U,
82 MODULE_DEVICE_TABLE(usb, pn533_table);
84 /* How much time we spend listening for initiators */
85 #define PN533_LISTEN_TIME 2
87 /* Standard pn533 frame definitions */
88 #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
89 + 2) /* data[0] TFI, data[1] CC */
90 #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
93 * Max extended frame payload len, excluding TFI and CC
94 * which are already in PN533_FRAME_HEADER_LEN.
96 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
98 #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
100 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
101 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
104 #define PN533_STD_FRAME_SOF 0x00FF
106 /* standard frame identifier: in/out/error */
107 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
108 #define PN533_STD_FRAME_DIR_OUT 0xD4
109 #define PN533_STD_FRAME_DIR_IN 0xD5
111 /* ACS ACR122 pn533 frame definitions */
112 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
114 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
115 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
117 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
118 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
120 /* CCID messages types */
121 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
122 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
124 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
127 #define PN533_STD_FRAME_CMD(f) (f->data[1])
129 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
130 #define PN533_CMD_RF_CONFIGURATION 0x32
131 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
132 #define PN533_CMD_IN_COMM_THRU 0x42
133 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
134 #define PN533_CMD_IN_ATR 0x50
135 #define PN533_CMD_IN_RELEASE 0x52
136 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
138 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
139 #define PN533_CMD_TG_GET_DATA 0x86
140 #define PN533_CMD_TG_SET_DATA 0x8e
141 #define PN533_CMD_UNDEF 0xff
143 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
145 /* PN533 Return codes */
146 #define PN533_CMD_RET_MASK 0x3F
147 #define PN533_CMD_MI_MASK 0x40
148 #define PN533_CMD_RET_SUCCESS 0x00
152 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
153 struct sk_buff *resp);
155 /* structs for pn533 commands */
157 /* PN533_CMD_GET_FIRMWARE_VERSION */
158 struct pn533_fw_version {
165 /* PN533_CMD_RF_CONFIGURATION */
166 #define PN533_CFGITEM_TIMING 0x02
167 #define PN533_CFGITEM_MAX_RETRIES 0x05
168 #define PN533_CFGITEM_PASORI 0x82
170 #define PN533_CONFIG_TIMING_102 0xb
171 #define PN533_CONFIG_TIMING_204 0xc
172 #define PN533_CONFIG_TIMING_409 0xd
173 #define PN533_CONFIG_TIMING_819 0xe
175 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
176 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
178 struct pn533_config_max_retries {
181 u8 mx_rty_passive_act;
184 struct pn533_config_timing {
190 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
192 /* felica commands opcode */
193 #define PN533_FELICA_OPC_SENSF_REQ 0
194 #define PN533_FELICA_OPC_SENSF_RES 1
195 /* felica SENSF_REQ parameters */
196 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
197 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
198 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
199 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
201 /* type B initiator_data values */
202 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
203 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
204 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
206 union pn533_cmd_poll_initdata {
219 /* Poll modulations */
221 PN533_POLL_MOD_106KBPS_A,
222 PN533_POLL_MOD_212KBPS_FELICA,
223 PN533_POLL_MOD_424KBPS_FELICA,
224 PN533_POLL_MOD_106KBPS_JEWEL,
225 PN533_POLL_MOD_847KBPS_B,
228 __PN533_POLL_MOD_AFTER_LAST,
230 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
232 struct pn533_poll_modulations {
236 union pn533_cmd_poll_initdata initiator_data;
241 static const struct pn533_poll_modulations poll_mod[] = {
242 [PN533_POLL_MOD_106KBPS_A] = {
249 [PN533_POLL_MOD_212KBPS_FELICA] = {
253 .initiator_data.felica = {
254 .opcode = PN533_FELICA_OPC_SENSF_REQ,
255 .sc = PN533_FELICA_SENSF_SC_ALL,
256 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
262 [PN533_POLL_MOD_424KBPS_FELICA] = {
266 .initiator_data.felica = {
267 .opcode = PN533_FELICA_OPC_SENSF_REQ,
268 .sc = PN533_FELICA_SENSF_SC_ALL,
269 .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
275 [PN533_POLL_MOD_106KBPS_JEWEL] = {
282 [PN533_POLL_MOD_847KBPS_B] = {
286 .initiator_data.type_b = {
287 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
289 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
294 [PN533_LISTEN_MOD] = {
299 /* PN533_CMD_IN_ATR */
301 struct pn533_cmd_activate_response {
313 struct pn533_cmd_jump_dep_response {
327 /* PN533_TG_INIT_AS_TARGET */
328 #define PN533_INIT_TARGET_PASSIVE 0x1
329 #define PN533_INIT_TARGET_DEP 0x2
331 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
332 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
333 #define PN533_INIT_TARGET_RESP_DEP 0x4
335 enum pn533_protocol_type {
336 PN533_PROTO_REQ_ACK_RESP = 0,
341 struct usb_device *udev;
342 struct usb_interface *interface;
343 struct nfc_dev *nfc_dev;
345 enum pn533_protocol_type protocol_type;
350 struct sk_buff_head resp_q;
352 struct workqueue_struct *wq;
353 struct work_struct cmd_work;
354 struct work_struct cmd_complete_work;
355 struct work_struct poll_work;
356 struct work_struct mi_work;
357 struct work_struct tg_work;
359 struct list_head cmd_queue;
360 struct pn533_cmd *cmd;
362 struct mutex cmd_lock; /* protects cmd queue */
364 void *cmd_complete_mi_arg;
366 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
370 u32 listen_protocols;
371 struct timer_list listen_timer;
377 u8 tgt_available_prots;
381 struct pn533_frame_ops *ops;
385 struct list_head queue;
389 struct sk_buff *resp;
391 pn533_send_async_complete_t complete_cb;
392 void *complete_cb_context;
395 struct pn533_std_frame {
403 struct pn533_frame_ops {
404 void (*tx_frame_init)(void *frame, u8 cmd_code);
405 void (*tx_frame_finish)(void *frame);
406 void (*tx_update_payload_len)(void *frame, int len);
410 bool (*rx_is_frame_valid)(void *frame);
411 int (*rx_frame_size)(void *frame);
416 u8 (*get_cmd_code)(void *frame);
419 struct pn533_acr122_ccid_hdr {
424 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
425 byte for reposnse msg */
426 u8 data[]; /* payload */
429 struct pn533_acr122_apdu_hdr {
436 struct pn533_acr122_tx_frame {
437 struct pn533_acr122_ccid_hdr ccid;
438 struct pn533_acr122_apdu_hdr apdu;
440 u8 data[]; /* pn533 frame: TFI ... */
443 struct pn533_acr122_rx_frame {
444 struct pn533_acr122_ccid_hdr ccid;
445 u8 data[]; /* pn533 frame : TFI ... */
448 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
450 struct pn533_acr122_tx_frame *frame = _frame;
452 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
453 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
455 frame->ccid.slot = 0;
457 frame->ccid.params[0] = 0;
458 frame->ccid.params[1] = 0;
459 frame->ccid.params[2] = 0;
461 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
462 frame->data[1] = cmd_code;
463 frame->datalen = 2; /* data[0] + data[1] */
465 frame->apdu.class = 0xFF;
471 static void pn533_acr122_tx_frame_finish(void *_frame)
473 struct pn533_acr122_tx_frame *frame = _frame;
475 frame->ccid.datalen += frame->datalen;
478 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
480 struct pn533_acr122_tx_frame *frame = _frame;
482 frame->datalen += len;
485 static bool pn533_acr122_is_rx_frame_valid(void *_frame)
487 struct pn533_acr122_rx_frame *frame = _frame;
489 if (frame->ccid.type != 0x83)
492 if (frame->data[frame->ccid.datalen - 2] == 0x63)
498 static int pn533_acr122_rx_frame_size(void *frame)
500 struct pn533_acr122_rx_frame *f = frame;
502 /* f->ccid.datalen already includes tail length */
503 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
506 static u8 pn533_acr122_get_cmd_code(void *frame)
508 struct pn533_acr122_rx_frame *f = frame;
510 return PN533_STD_FRAME_CMD(f);
513 static struct pn533_frame_ops pn533_acr122_frame_ops = {
514 .tx_frame_init = pn533_acr122_tx_frame_init,
515 .tx_frame_finish = pn533_acr122_tx_frame_finish,
516 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
517 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
518 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
520 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
521 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
522 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
523 .rx_frame_size = pn533_acr122_rx_frame_size,
525 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
526 .get_cmd_code = pn533_acr122_get_cmd_code,
529 /* The rule: value + checksum = 0 */
530 static inline u8 pn533_std_checksum(u8 value)
535 /* The rule: sum(data elements) + checksum = 0 */
536 static u8 pn533_std_data_checksum(u8 *data, int datalen)
541 for (i = 0; i < datalen; i++)
544 return pn533_std_checksum(sum);
547 static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
549 struct pn533_std_frame *frame = _frame;
552 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
553 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
554 PN533_STD_FRAME_CMD(frame) = cmd_code;
558 static void pn533_std_tx_frame_finish(void *_frame)
560 struct pn533_std_frame *frame = _frame;
562 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
564 PN533_STD_FRAME_CHECKSUM(frame) =
565 pn533_std_data_checksum(frame->data, frame->datalen);
567 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
570 static void pn533_std_tx_update_payload_len(void *_frame, int len)
572 struct pn533_std_frame *frame = _frame;
574 frame->datalen += len;
577 static bool pn533_std_rx_frame_is_valid(void *_frame)
580 struct pn533_std_frame *frame = _frame;
582 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
585 checksum = pn533_std_checksum(frame->datalen);
586 if (checksum != frame->datalen_checksum)
589 checksum = pn533_std_data_checksum(frame->data, frame->datalen);
590 if (checksum != PN533_STD_FRAME_CHECKSUM(frame))
596 static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
598 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
601 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
607 static inline int pn533_std_rx_frame_size(void *frame)
609 struct pn533_std_frame *f = frame;
611 return sizeof(struct pn533_std_frame) + f->datalen +
612 PN533_STD_FRAME_TAIL_LEN;
615 static u8 pn533_std_get_cmd_code(void *frame)
617 struct pn533_std_frame *f = frame;
619 return PN533_STD_FRAME_CMD(f);
622 static struct pn533_frame_ops pn533_std_frame_ops = {
623 .tx_frame_init = pn533_std_tx_frame_init,
624 .tx_frame_finish = pn533_std_tx_frame_finish,
625 .tx_update_payload_len = pn533_std_tx_update_payload_len,
626 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
627 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
629 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
630 .rx_frame_size = pn533_std_rx_frame_size,
631 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
632 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
634 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
635 .get_cmd_code = pn533_std_get_cmd_code,
638 static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
640 return (dev->ops->get_cmd_code(frame) ==
641 PN533_CMD_RESPONSE(dev->cmd->code));
644 static void pn533_recv_response(struct urb *urb)
646 struct pn533 *dev = urb->context;
647 struct pn533_cmd *cmd = dev->cmd;
650 cmd->status = urb->status;
652 switch (urb->status) {
657 nfc_dev_dbg(&dev->interface->dev,
658 "The urb has been canceled (status %d)",
663 nfc_dev_err(&dev->interface->dev,
664 "Urb failure (status %d)", urb->status);
668 in_frame = dev->in_urb->transfer_buffer;
670 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
671 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
672 dev->ops->rx_frame_size(in_frame), false);
674 if (!dev->ops->rx_is_frame_valid(in_frame)) {
675 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
680 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
681 nfc_dev_err(&dev->interface->dev,
682 "It it not the response to the last command");
688 queue_work(dev->wq, &dev->cmd_complete_work);
691 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
693 dev->in_urb->complete = pn533_recv_response;
695 return usb_submit_urb(dev->in_urb, flags);
698 static void pn533_recv_ack(struct urb *urb)
700 struct pn533 *dev = urb->context;
701 struct pn533_cmd *cmd = dev->cmd;
702 struct pn533_std_frame *in_frame;
705 cmd->status = urb->status;
707 switch (urb->status) {
712 nfc_dev_dbg(&dev->interface->dev,
713 "The urb has been stopped (status %d)",
718 nfc_dev_err(&dev->interface->dev,
719 "Urb failure (status %d)", urb->status);
723 in_frame = dev->in_urb->transfer_buffer;
725 if (!pn533_std_rx_frame_is_ack(in_frame)) {
726 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
731 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
733 nfc_dev_err(&dev->interface->dev,
734 "usb_submit_urb failed with result %d", rc);
742 queue_work(dev->wq, &dev->cmd_complete_work);
745 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
747 dev->in_urb->complete = pn533_recv_ack;
749 return usb_submit_urb(dev->in_urb, flags);
752 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
754 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
755 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
758 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
760 dev->out_urb->transfer_buffer = ack;
761 dev->out_urb->transfer_buffer_length = sizeof(ack);
762 rc = usb_submit_urb(dev->out_urb, flags);
767 static int __pn533_send_frame_async(struct pn533 *dev,
774 dev->out_urb->transfer_buffer = out->data;
775 dev->out_urb->transfer_buffer_length = out->len;
777 dev->in_urb->transfer_buffer = in->data;
778 dev->in_urb->transfer_buffer_length = in_len;
780 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
781 out->data, out->len, false);
783 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
787 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
788 /* request for response for sent packet directly */
789 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
792 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
793 /* request for ACK if that's the case */
794 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
802 usb_unlink_urb(dev->out_urb);
806 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
809 /* payload is already there, just update datalen */
810 int payload_len = skb->len;
811 struct pn533_frame_ops *ops = dev->ops;
814 skb_push(skb, ops->tx_header_len);
815 skb_put(skb, ops->tx_tail_len);
817 ops->tx_frame_init(skb->data, cmd_code);
818 ops->tx_update_payload_len(skb->data, payload_len);
819 ops->tx_frame_finish(skb->data);
822 static int pn533_send_async_complete(struct pn533 *dev)
824 struct pn533_cmd *cmd = dev->cmd;
825 int status = cmd->status;
827 struct sk_buff *req = cmd->req;
828 struct sk_buff *resp = cmd->resp;
835 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
841 skb_put(resp, dev->ops->rx_frame_size(resp->data));
842 skb_pull(resp, dev->ops->rx_header_len);
843 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
845 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
853 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
854 struct sk_buff *req, struct sk_buff *resp,
856 pn533_send_async_complete_t complete_cb,
857 void *complete_cb_context)
859 struct pn533_cmd *cmd;
862 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
864 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
868 cmd->code = cmd_code;
871 cmd->resp_len = resp_len;
872 cmd->complete_cb = complete_cb;
873 cmd->complete_cb_context = complete_cb_context;
875 pn533_build_cmd_frame(dev, cmd_code, req);
877 mutex_lock(&dev->cmd_lock);
879 if (!dev->cmd_pending) {
880 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
884 dev->cmd_pending = 1;
889 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
892 INIT_LIST_HEAD(&cmd->queue);
893 list_add_tail(&cmd->queue, &dev->cmd_queue);
900 mutex_unlock(&dev->cmd_lock);
904 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
906 pn533_send_async_complete_t complete_cb,
907 void *complete_cb_context)
909 struct sk_buff *resp;
911 int resp_len = dev->ops->rx_header_len +
912 dev->ops->max_payload_len +
913 dev->ops->rx_tail_len;
915 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
919 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
920 complete_cb_context);
927 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
929 pn533_send_async_complete_t complete_cb,
930 void *complete_cb_context)
932 struct sk_buff *resp;
934 int resp_len = dev->ops->rx_header_len +
935 dev->ops->max_payload_len +
936 dev->ops->rx_tail_len;
938 resp = alloc_skb(resp_len, GFP_KERNEL);
942 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
943 complete_cb_context);
951 * pn533_send_cmd_direct_async
953 * The function sends a piority cmd directly to the chip omiting the cmd
954 * queue. It's intended to be used by chaining mechanism of received responses
955 * where the host has to request every single chunk of data before scheduling
956 * next cmd from the queue.
958 static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
960 pn533_send_async_complete_t complete_cb,
961 void *complete_cb_context)
963 struct sk_buff *resp;
964 struct pn533_cmd *cmd;
966 int resp_len = dev->ops->rx_header_len +
967 dev->ops->max_payload_len +
968 dev->ops->rx_tail_len;
970 resp = alloc_skb(resp_len, GFP_KERNEL);
974 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
980 cmd->code = cmd_code;
983 cmd->resp_len = resp_len;
984 cmd->complete_cb = complete_cb;
985 cmd->complete_cb_context = complete_cb_context;
987 pn533_build_cmd_frame(dev, cmd_code, req);
989 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
1000 static void pn533_wq_cmd_complete(struct work_struct *work)
1002 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1005 rc = pn533_send_async_complete(dev);
1006 if (rc != -EINPROGRESS)
1007 queue_work(dev->wq, &dev->cmd_work);
1010 static void pn533_wq_cmd(struct work_struct *work)
1012 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1013 struct pn533_cmd *cmd;
1016 mutex_lock(&dev->cmd_lock);
1018 if (list_empty(&dev->cmd_queue)) {
1019 dev->cmd_pending = 0;
1020 mutex_unlock(&dev->cmd_lock);
1024 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1026 list_del(&cmd->queue);
1028 mutex_unlock(&dev->cmd_lock);
1030 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
1032 dev_kfree_skb(cmd->req);
1033 dev_kfree_skb(cmd->resp);
1041 struct pn533_sync_cmd_response {
1042 struct sk_buff *resp;
1043 struct completion done;
1046 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1047 struct sk_buff *resp)
1049 struct pn533_sync_cmd_response *arg = _arg;
1052 complete(&arg->done);
1057 /* pn533_send_cmd_sync
1059 * Please note the req parameter is freed inside the function to
1060 * limit a number of return value interpretations by the caller.
1062 * 1. negative in case of error during TX path -> req should be freed
1064 * 2. negative in case of error during RX path -> req should not be freed
1065 * as it's been already freed at the begining of RX path by
1066 * async_complete_cb.
1068 * 3. valid pointer in case of succesfult RX path
1070 * A caller has to check a return value with IS_ERR macro. If the test pass,
1071 * the returned pointer is valid.
1074 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1075 struct sk_buff *req)
1078 struct pn533_sync_cmd_response arg;
1080 init_completion(&arg.done);
1082 rc = pn533_send_cmd_async(dev, cmd_code, req,
1083 pn533_send_sync_complete, &arg);
1089 wait_for_completion(&arg.done);
1094 static void pn533_send_complete(struct urb *urb)
1096 struct pn533 *dev = urb->context;
1098 switch (urb->status) {
1100 break; /* success */
1103 nfc_dev_dbg(&dev->interface->dev,
1104 "The urb has been stopped (status %d)",
1109 nfc_dev_err(&dev->interface->dev,
1110 "Urb failure (status %d)", urb->status);
1114 static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1116 /* ACR122U does not support any command which aborts last
1117 * issued command i.e. as ACK for standard PN533. Additionally,
1118 * it behaves stange, sending broken or incorrect responses,
1119 * when we cancel urb before the chip will send response.
1121 if (dev->device_type == PN533_DEVICE_ACR122U)
1124 /* An ack will cancel the last issued command */
1125 pn533_send_ack(dev, flags);
1127 /* cancel the urb request */
1128 usb_kill_urb(dev->in_urb);
1131 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
1133 struct sk_buff *skb;
1135 skb = alloc_skb(dev->ops->tx_header_len +
1137 dev->ops->tx_tail_len, GFP_KERNEL);
1140 skb_reserve(skb, dev->ops->tx_header_len);
1145 struct pn533_target_type_a {
1153 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1154 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1155 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1157 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1158 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1160 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1161 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1163 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1164 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1165 #define PN533_TYPE_A_SEL_PROT_DEP 2
1166 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1168 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1169 int target_data_len)
1174 if (target_data_len < sizeof(struct pn533_target_type_a))
1177 /* The lenght check of nfcid[] and ats[] are not being performed because
1178 the values are not being used */
1180 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1181 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1182 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1184 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1185 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1186 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1187 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1190 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1191 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1197 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1200 struct pn533_target_type_a *tgt_type_a;
1202 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1204 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1207 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1208 case PN533_TYPE_A_SEL_PROT_MIFARE:
1209 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1211 case PN533_TYPE_A_SEL_PROT_ISO14443:
1212 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1214 case PN533_TYPE_A_SEL_PROT_DEP:
1215 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1217 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1218 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1219 NFC_PROTO_NFC_DEP_MASK;
1223 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1224 nfc_tgt->sel_res = tgt_type_a->sel_res;
1225 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1226 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1231 struct pn533_target_felica {
1240 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1241 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1243 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1244 int target_data_len)
1246 if (target_data_len < sizeof(struct pn533_target_felica))
1249 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1255 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1258 struct pn533_target_felica *tgt_felica;
1260 tgt_felica = (struct pn533_target_felica *)tgt_data;
1262 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1265 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1266 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1267 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1269 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1271 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1272 nfc_tgt->sensf_res_len = 9;
1277 struct pn533_target_jewel {
1282 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1283 int target_data_len)
1288 if (target_data_len < sizeof(struct pn533_target_jewel))
1291 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1292 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1293 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1295 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1296 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1297 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1298 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1304 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1307 struct pn533_target_jewel *tgt_jewel;
1309 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1311 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1314 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1315 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1316 nfc_tgt->nfcid1_len = 4;
1317 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1322 struct pn533_type_b_prot_info {
1328 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1329 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1330 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1332 struct pn533_type_b_sens_res {
1336 struct pn533_type_b_prot_info prot_info;
1339 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1341 struct pn533_target_type_b {
1342 struct pn533_type_b_sens_res sensb_res;
1347 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1348 int target_data_len)
1350 if (target_data_len < sizeof(struct pn533_target_type_b))
1353 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1356 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1357 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1363 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1366 struct pn533_target_type_b *tgt_type_b;
1368 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1370 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1373 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1378 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1381 struct nfc_target nfc_tgt;
1384 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1385 dev->poll_mod_curr);
1390 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1392 switch (dev->poll_mod_curr) {
1393 case PN533_POLL_MOD_106KBPS_A:
1394 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1396 case PN533_POLL_MOD_212KBPS_FELICA:
1397 case PN533_POLL_MOD_424KBPS_FELICA:
1398 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1400 case PN533_POLL_MOD_106KBPS_JEWEL:
1401 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1403 case PN533_POLL_MOD_847KBPS_B:
1404 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1407 nfc_dev_err(&dev->interface->dev,
1408 "Unknown current poll modulation");
1415 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1416 nfc_dev_dbg(&dev->interface->dev,
1417 "The Tg found doesn't have the desired protocol");
1421 nfc_dev_dbg(&dev->interface->dev,
1422 "Target found - supported protocols: 0x%x",
1423 nfc_tgt.supported_protocols);
1425 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1427 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1432 static inline void pn533_poll_next_mod(struct pn533 *dev)
1434 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1437 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1439 dev->poll_mod_count = 0;
1442 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1444 dev->poll_mod_active[dev->poll_mod_count] =
1445 (struct pn533_poll_modulations *)&poll_mod[mod_index];
1446 dev->poll_mod_count++;
1449 static void pn533_poll_create_mod_list(struct pn533 *dev,
1450 u32 im_protocols, u32 tm_protocols)
1452 pn533_poll_reset_mod_list(dev);
1454 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1455 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1456 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1457 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1459 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1460 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1461 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1462 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1465 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1466 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1468 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1469 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1472 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1475 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1477 u8 nbtg, tg, *tgdata;
1480 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1482 nbtg = resp->data[0];
1484 tgdata = &resp->data[2];
1485 tgdata_len = resp->len - 2; /* nbtg + tg */
1488 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1490 /* We must stop the poll after a valid target found */
1492 pn533_poll_reset_mod_list(dev);
1500 static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1502 struct sk_buff *skb;
1503 u8 *felica, *nfcid3, *gb;
1505 u8 *gbytes = dev->gb;
1506 size_t gbytes_len = dev->gb_len;
1508 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1509 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1510 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1511 0xff, 0xff}; /* System code */
1513 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1515 0x40}; /* SEL_RES for DEP */
1517 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1518 felica (18), nfcid3 (10), gb_len (1) */
1522 skb = pn533_alloc_skb(dev, skb_len);
1526 /* DEP support only */
1527 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1530 memcpy(skb_put(skb, 6), mifare_params, 6);
1533 felica = skb_put(skb, 18);
1534 memcpy(felica, felica_params, 18);
1535 get_random_bytes(felica + 2, 6);
1538 nfcid3 = skb_put(skb, 10);
1539 memset(nfcid3, 0, 10);
1540 memcpy(nfcid3, felica, 8);
1543 *skb_put(skb, 1) = gbytes_len;
1545 gb = skb_put(skb, gbytes_len);
1546 memcpy(gb, gbytes, gbytes_len);
1549 *skb_put(skb, 1) = 0;
1554 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1555 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1556 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1557 struct sk_buff *resp)
1561 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1564 return PTR_ERR(resp);
1566 status = resp->data[0];
1567 skb_pull(resp, sizeof(status));
1570 nfc_tm_deactivated(dev->nfc_dev);
1572 dev_kfree_skb(resp);
1576 return nfc_tm_data_received(dev->nfc_dev, resp);
1579 static void pn533_wq_tg_get_data(struct work_struct *work)
1581 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1583 struct sk_buff *skb;
1586 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1588 skb = pn533_alloc_skb(dev, 0);
1592 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1593 pn533_tm_get_data_complete, NULL);
1601 #define ATR_REQ_GB_OFFSET 17
1602 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1604 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1608 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1610 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1613 mode = resp->data[0];
1614 cmd = &resp->data[1];
1616 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1619 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1620 PN533_INIT_TARGET_RESP_ACTIVE)
1621 comm_mode = NFC_COMM_ACTIVE;
1623 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1626 gb = cmd + ATR_REQ_GB_OFFSET;
1627 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1629 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1630 comm_mode, gb, gb_len);
1632 nfc_dev_err(&dev->interface->dev,
1633 "Error when signaling target activation");
1638 queue_work(dev->wq, &dev->tg_work);
1643 static void pn533_listen_mode_timer(unsigned long data)
1645 struct pn533 *dev = (struct pn533 *)data;
1647 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1649 dev->cancel_listen = 1;
1651 pn533_poll_next_mod(dev);
1653 queue_work(dev->wq, &dev->poll_work);
1656 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1657 struct sk_buff *resp)
1659 struct pn533_poll_modulations *cur_mod;
1662 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1667 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1670 if (rc == -ENOENT) {
1671 if (dev->poll_mod_count != 0)
1675 } else if (rc < 0) {
1676 nfc_dev_err(&dev->interface->dev,
1677 "Error %d when running poll", rc);
1682 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1684 if (cur_mod->len == 0) { /* Target mode */
1685 del_timer(&dev->listen_timer);
1686 rc = pn533_init_target_complete(dev, resp);
1690 /* Initiator mode */
1691 rc = pn533_start_poll_complete(dev, resp);
1695 if (!dev->poll_mod_count) {
1696 nfc_dev_dbg(&dev->interface->dev, "Polling has been stoped.");
1700 pn533_poll_next_mod(dev);
1701 queue_work(dev->wq, &dev->poll_work);
1704 dev_kfree_skb(resp);
1708 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1710 pn533_poll_reset_mod_list(dev);
1711 dev->poll_protocols = 0;
1715 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1716 struct pn533_poll_modulations *mod)
1718 struct sk_buff *skb;
1720 skb = pn533_alloc_skb(dev, mod->len);
1724 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1729 static int pn533_send_poll_frame(struct pn533 *dev)
1731 struct pn533_poll_modulations *mod;
1732 struct sk_buff *skb;
1736 mod = dev->poll_mod_active[dev->poll_mod_curr];
1738 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1739 __func__, mod->len);
1741 if (mod->len == 0) { /* Listen mode */
1742 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1743 skb = pn533_alloc_poll_tg_frame(dev);
1744 } else { /* Polling mode */
1745 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1746 skb = pn533_alloc_poll_in_frame(dev, mod);
1750 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1754 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1758 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1764 static void pn533_wq_poll(struct work_struct *work)
1766 struct pn533 *dev = container_of(work, struct pn533, poll_work);
1767 struct pn533_poll_modulations *cur_mod;
1770 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1772 nfc_dev_dbg(&dev->interface->dev,
1773 "%s cancel_listen %d modulation len %d",
1774 __func__, dev->cancel_listen, cur_mod->len);
1776 if (dev->cancel_listen == 1) {
1777 dev->cancel_listen = 0;
1778 pn533_abort_cmd(dev, GFP_ATOMIC);
1781 rc = pn533_send_poll_frame(dev);
1785 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1786 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1791 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1792 u32 im_protocols, u32 tm_protocols)
1794 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1796 nfc_dev_dbg(&dev->interface->dev,
1797 "%s: im protocols 0x%x tm protocols 0x%x",
1798 __func__, im_protocols, tm_protocols);
1800 if (dev->tgt_active_prot) {
1801 nfc_dev_err(&dev->interface->dev,
1802 "Cannot poll with a target already activated");
1806 if (dev->tgt_mode) {
1807 nfc_dev_err(&dev->interface->dev,
1808 "Cannot poll while already being activated");
1813 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1814 if (dev->gb == NULL)
1818 dev->poll_mod_curr = 0;
1819 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1820 dev->poll_protocols = im_protocols;
1821 dev->listen_protocols = tm_protocols;
1823 return pn533_send_poll_frame(dev);
1826 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1828 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1830 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1832 del_timer(&dev->listen_timer);
1834 if (!dev->poll_mod_count) {
1835 nfc_dev_dbg(&dev->interface->dev,
1836 "Polling operation was not running");
1840 pn533_abort_cmd(dev, GFP_KERNEL);
1841 pn533_poll_reset_mod_list(dev);
1844 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1846 struct pn533_cmd_activate_response *rsp;
1850 struct sk_buff *skb;
1851 struct sk_buff *resp;
1853 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1855 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1859 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1860 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1862 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1864 return PTR_ERR(resp);
1866 rsp = (struct pn533_cmd_activate_response *)resp->data;
1867 rc = rsp->status & PN533_CMD_RET_MASK;
1868 if (rc != PN533_CMD_RET_SUCCESS) {
1869 nfc_dev_err(&dev->interface->dev,
1870 "Target activation failed (error 0x%x)", rc);
1871 dev_kfree_skb(resp);
1875 /* ATR_RES general bytes are located at offset 16 */
1876 gt_len = resp->len - 16;
1877 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1879 dev_kfree_skb(resp);
1883 static int pn533_activate_target(struct nfc_dev *nfc_dev,
1884 struct nfc_target *target, u32 protocol)
1886 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1889 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1892 if (dev->poll_mod_count) {
1893 nfc_dev_err(&dev->interface->dev,
1894 "Cannot activate while polling");
1898 if (dev->tgt_active_prot) {
1899 nfc_dev_err(&dev->interface->dev,
1900 "There is already an active target");
1904 if (!dev->tgt_available_prots) {
1905 nfc_dev_err(&dev->interface->dev,
1906 "There is no available target to activate");
1910 if (!(dev->tgt_available_prots & (1 << protocol))) {
1911 nfc_dev_err(&dev->interface->dev,
1912 "Target doesn't support requested proto %u",
1917 if (protocol == NFC_PROTO_NFC_DEP) {
1918 rc = pn533_activate_target_nfcdep(dev);
1920 nfc_dev_err(&dev->interface->dev,
1921 "Activating target with DEP failed %d", rc);
1926 dev->tgt_active_prot = protocol;
1927 dev->tgt_available_prots = 0;
1932 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1933 struct nfc_target *target)
1935 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1937 struct sk_buff *skb;
1938 struct sk_buff *resp;
1942 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1944 if (!dev->tgt_active_prot) {
1945 nfc_dev_err(&dev->interface->dev, "There is no active target");
1949 dev->tgt_active_prot = 0;
1950 skb_queue_purge(&dev->resp_q);
1952 skb = pn533_alloc_skb(dev, sizeof(u8));
1956 *skb_put(skb, 1) = 1; /* TG*/
1958 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1962 rc = resp->data[0] & PN533_CMD_RET_MASK;
1963 if (rc != PN533_CMD_RET_SUCCESS)
1964 nfc_dev_err(&dev->interface->dev,
1965 "Error 0x%x when releasing the target", rc);
1967 dev_kfree_skb(resp);
1972 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1973 struct sk_buff *resp)
1975 struct pn533_cmd_jump_dep_response *rsp;
1978 u8 active = *(u8 *)arg;
1983 return PTR_ERR(resp);
1985 if (dev->tgt_available_prots &&
1986 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1987 nfc_dev_err(&dev->interface->dev,
1988 "The target does not support DEP");
1993 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1995 rc = rsp->status & PN533_CMD_RET_MASK;
1996 if (rc != PN533_CMD_RET_SUCCESS) {
1997 nfc_dev_err(&dev->interface->dev,
1998 "Bringing DEP link up failed (error 0x%x)", rc);
2002 if (!dev->tgt_available_prots) {
2003 struct nfc_target nfc_target;
2005 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
2007 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2008 nfc_target.nfcid1_len = 10;
2009 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
2010 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2014 dev->tgt_available_prots = 0;
2017 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2019 /* ATR_RES general bytes are located at offset 17 */
2020 target_gt_len = resp->len - 17;
2021 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
2022 rsp->gt, target_gt_len);
2024 rc = nfc_dep_link_is_up(dev->nfc_dev,
2025 dev->nfc_dev->targets[0].idx,
2026 !active, NFC_RF_INITIATOR);
2029 dev_kfree_skb(resp);
2033 static int pn533_mod_to_baud(struct pn533 *dev)
2035 switch (dev->poll_mod_curr) {
2036 case PN533_POLL_MOD_106KBPS_A:
2038 case PN533_POLL_MOD_212KBPS_FELICA:
2040 case PN533_POLL_MOD_424KBPS_FELICA:
2047 #define PASSIVE_DATA_LEN 5
2048 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2049 u8 comm_mode, u8 *gb, size_t gb_len)
2051 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2052 struct sk_buff *skb;
2053 int rc, baud, skb_len;
2056 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2058 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2060 if (dev->poll_mod_count) {
2061 nfc_dev_err(&dev->interface->dev,
2062 "Cannot bring the DEP link up while polling");
2066 if (dev->tgt_active_prot) {
2067 nfc_dev_err(&dev->interface->dev,
2068 "There is already an active target");
2072 baud = pn533_mod_to_baud(dev);
2074 nfc_dev_err(&dev->interface->dev,
2075 "Invalid curr modulation %d", dev->poll_mod_curr);
2079 skb_len = 3 + gb_len; /* ActPass + BR + Next */
2080 if (comm_mode == NFC_COMM_PASSIVE)
2081 skb_len += PASSIVE_DATA_LEN;
2083 skb = pn533_alloc_skb(dev, skb_len);
2087 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2088 *skb_put(skb, 1) = baud; /* Baud rate */
2090 next = skb_put(skb, 1); /* Next */
2093 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
2094 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
2099 if (gb != NULL && gb_len > 0) {
2100 memcpy(skb_put(skb, gb_len), gb, gb_len);
2101 *next |= 4; /* We have some Gi */
2106 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2114 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2115 pn533_in_dep_link_up_complete, arg);
2125 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2127 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2129 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2131 pn533_poll_reset_mod_list(dev);
2133 if (dev->tgt_mode || dev->tgt_active_prot)
2134 pn533_abort_cmd(dev, GFP_KERNEL);
2136 dev->tgt_active_prot = 0;
2139 skb_queue_purge(&dev->resp_q);
2144 struct pn533_data_exchange_arg {
2145 data_exchange_cb_t cb;
2149 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2151 struct sk_buff *skb, *tmp, *t;
2152 unsigned int skb_len = 0, tmp_len = 0;
2154 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2156 if (skb_queue_empty(&dev->resp_q))
2159 if (skb_queue_len(&dev->resp_q) == 1) {
2160 skb = skb_dequeue(&dev->resp_q);
2164 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2165 skb_len += tmp->len;
2167 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2170 skb = alloc_skb(skb_len, GFP_KERNEL);
2174 skb_put(skb, skb_len);
2176 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2177 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2178 tmp_len += tmp->len;
2182 skb_queue_purge(&dev->resp_q);
2187 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2188 struct sk_buff *resp)
2190 struct pn533_data_exchange_arg *arg = _arg;
2191 struct sk_buff *skb;
2195 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2202 status = resp->data[0];
2203 ret = status & PN533_CMD_RET_MASK;
2204 mi = status & PN533_CMD_MI_MASK;
2206 skb_pull(resp, sizeof(status));
2208 if (ret != PN533_CMD_RET_SUCCESS) {
2209 nfc_dev_err(&dev->interface->dev,
2210 "Exchanging data failed (error 0x%x)", ret);
2215 skb_queue_tail(&dev->resp_q, resp);
2218 dev->cmd_complete_mi_arg = arg;
2219 queue_work(dev->wq, &dev->mi_work);
2220 return -EINPROGRESS;
2223 skb = pn533_build_response(dev);
2227 arg->cb(arg->cb_context, skb, 0);
2232 dev_kfree_skb(resp);
2234 skb_queue_purge(&dev->resp_q);
2235 arg->cb(arg->cb_context, NULL, rc);
2240 static int pn533_transceive(struct nfc_dev *nfc_dev,
2241 struct nfc_target *target, struct sk_buff *skb,
2242 data_exchange_cb_t cb, void *cb_context)
2244 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2245 struct pn533_data_exchange_arg *arg = NULL;
2248 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2250 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2251 /* TODO: Implement support to multi-part data exchange */
2252 nfc_dev_err(&dev->interface->dev,
2253 "Data length greater than the max allowed: %d",
2254 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2259 if (!dev->tgt_active_prot) {
2260 nfc_dev_err(&dev->interface->dev,
2261 "Can't exchange data if there is no active target");
2266 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2273 arg->cb_context = cb_context;
2275 switch (dev->device_type) {
2276 case PN533_DEVICE_PASORI:
2277 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2278 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2280 pn533_data_exchange_complete,
2286 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2288 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2289 skb, pn533_data_exchange_complete,
2295 if (rc < 0) /* rc from send_async */
2306 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2307 struct sk_buff *resp)
2311 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2314 return PTR_ERR(resp);
2316 status = resp->data[0];
2318 dev_kfree_skb(resp);
2321 nfc_tm_deactivated(dev->nfc_dev);
2328 queue_work(dev->wq, &dev->tg_work);
2333 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2335 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2338 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2340 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2341 nfc_dev_err(&dev->interface->dev,
2342 "Data length greater than the max allowed: %d",
2343 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2347 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2348 pn533_tm_send_complete, NULL);
2355 static void pn533_wq_mi_recv(struct work_struct *work)
2357 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2359 struct sk_buff *skb;
2362 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2364 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2368 switch (dev->device_type) {
2369 case PN533_DEVICE_PASORI:
2370 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2371 rc = pn533_send_cmd_direct_async(dev,
2372 PN533_CMD_IN_COMM_THRU,
2374 pn533_data_exchange_complete,
2375 dev->cmd_complete_mi_arg);
2380 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
2382 rc = pn533_send_cmd_direct_async(dev,
2383 PN533_CMD_IN_DATA_EXCHANGE,
2385 pn533_data_exchange_complete,
2386 dev->cmd_complete_mi_arg);
2391 if (rc == 0) /* success */
2394 nfc_dev_err(&dev->interface->dev,
2395 "Error %d when trying to perform data_exchange", rc);
2398 kfree(dev->cmd_complete_mi_arg);
2401 pn533_send_ack(dev, GFP_KERNEL);
2402 queue_work(dev->wq, &dev->cmd_work);
2405 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2408 struct sk_buff *skb;
2409 struct sk_buff *resp;
2413 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2415 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2417 skb = pn533_alloc_skb(dev, skb_len);
2421 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2422 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2424 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2426 return PTR_ERR(resp);
2428 dev_kfree_skb(resp);
2432 static int pn533_get_firmware_version(struct pn533 *dev,
2433 struct pn533_fw_version *fv)
2435 struct sk_buff *skb;
2436 struct sk_buff *resp;
2438 skb = pn533_alloc_skb(dev, 0);
2442 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2444 return PTR_ERR(resp);
2446 fv->ic = resp->data[0];
2447 fv->ver = resp->data[1];
2448 fv->rev = resp->data[2];
2449 fv->support = resp->data[3];
2451 dev_kfree_skb(resp);
2455 static int pn533_pasori_fw_reset(struct pn533 *dev)
2457 struct sk_buff *skb;
2458 struct sk_buff *resp;
2460 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2462 skb = pn533_alloc_skb(dev, sizeof(u8));
2466 *skb_put(skb, sizeof(u8)) = 0x1;
2468 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2470 return PTR_ERR(resp);
2472 dev_kfree_skb(resp);
2477 struct pn533_acr122_poweron_rdr_arg {
2479 struct completion done;
2482 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2484 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2486 nfc_dev_dbg(&urb->dev->dev, "%s", __func__);
2488 print_hex_dump(KERN_ERR, "ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2489 urb->transfer_buffer, urb->transfer_buffer_length,
2492 arg->rc = urb->status;
2493 complete(&arg->done);
2496 static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2498 /* Power on th reader (CCID cmd) */
2499 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2500 0, 0, 0, 0, 0, 0, 3, 0, 0};
2504 struct pn533_acr122_poweron_rdr_arg arg;
2506 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2508 init_completion(&arg.done);
2509 cntx = dev->in_urb->context; /* backup context */
2511 dev->in_urb->transfer_buffer = buf;
2512 dev->in_urb->transfer_buffer_length = 255;
2513 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2514 dev->in_urb->context = &arg;
2516 dev->out_urb->transfer_buffer = cmd;
2517 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2519 print_hex_dump(KERN_ERR, "ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2520 cmd, sizeof(cmd), false);
2522 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2524 nfc_dev_err(&dev->interface->dev,
2525 "Reader power on cmd error %d", rc);
2529 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2531 nfc_dev_err(&dev->interface->dev,
2532 "Can't submit for reader power on cmd response %d",
2537 wait_for_completion(&arg.done);
2538 dev->in_urb->context = cntx; /* restore context */
2543 static struct nfc_ops pn533_nfc_ops = {
2546 .dep_link_up = pn533_dep_link_up,
2547 .dep_link_down = pn533_dep_link_down,
2548 .start_poll = pn533_start_poll,
2549 .stop_poll = pn533_stop_poll,
2550 .activate_target = pn533_activate_target,
2551 .deactivate_target = pn533_deactivate_target,
2552 .im_transceive = pn533_transceive,
2553 .tm_send = pn533_tm_send,
2556 static int pn533_setup(struct pn533 *dev)
2558 struct pn533_config_max_retries max_retries;
2559 struct pn533_config_timing timing;
2560 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2563 switch (dev->device_type) {
2564 case PN533_DEVICE_STD:
2565 max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
2566 max_retries.mx_rty_psl = 2;
2567 max_retries.mx_rty_passive_act =
2568 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2570 timing.rfu = PN533_CONFIG_TIMING_102;
2571 timing.atr_res_timeout = PN533_CONFIG_TIMING_204;
2572 timing.dep_timeout = PN533_CONFIG_TIMING_409;
2576 case PN533_DEVICE_PASORI:
2577 case PN533_DEVICE_ACR122U:
2578 max_retries.mx_rty_atr = 0x2;
2579 max_retries.mx_rty_psl = 0x1;
2580 max_retries.mx_rty_passive_act =
2581 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2583 timing.rfu = PN533_CONFIG_TIMING_102;
2584 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2585 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2590 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2595 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2596 (u8 *)&max_retries, sizeof(max_retries));
2598 nfc_dev_err(&dev->interface->dev,
2599 "Error on setting MAX_RETRIES config");
2604 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2605 (u8 *)&timing, sizeof(timing));
2607 nfc_dev_err(&dev->interface->dev,
2608 "Error on setting RF timings");
2612 switch (dev->device_type) {
2613 case PN533_DEVICE_STD:
2616 case PN533_DEVICE_PASORI:
2617 pn533_pasori_fw_reset(dev);
2619 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2622 nfc_dev_err(&dev->interface->dev,
2623 "Error while settings PASORI config");
2627 pn533_pasori_fw_reset(dev);
2635 static int pn533_probe(struct usb_interface *interface,
2636 const struct usb_device_id *id)
2638 struct pn533_fw_version fw_ver;
2640 struct usb_host_interface *iface_desc;
2641 struct usb_endpoint_descriptor *endpoint;
2642 int in_endpoint = 0;
2643 int out_endpoint = 0;
2648 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2652 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2653 dev->interface = interface;
2654 mutex_init(&dev->cmd_lock);
2656 iface_desc = interface->cur_altsetting;
2657 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2658 endpoint = &iface_desc->endpoint[i].desc;
2660 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2661 in_endpoint = endpoint->bEndpointAddress;
2663 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2664 out_endpoint = endpoint->bEndpointAddress;
2667 if (!in_endpoint || !out_endpoint) {
2668 nfc_dev_err(&interface->dev,
2669 "Could not find bulk-in or bulk-out endpoint");
2674 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2675 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2677 if (!dev->in_urb || !dev->out_urb)
2680 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2681 usb_rcvbulkpipe(dev->udev, in_endpoint),
2682 NULL, 0, NULL, dev);
2683 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2684 usb_sndbulkpipe(dev->udev, out_endpoint),
2685 NULL, 0, pn533_send_complete, dev);
2687 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2688 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2689 INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
2690 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2691 INIT_WORK(&dev->poll_work, pn533_wq_poll);
2692 dev->wq = alloc_ordered_workqueue("pn533", 0);
2693 if (dev->wq == NULL)
2696 init_timer(&dev->listen_timer);
2697 dev->listen_timer.data = (unsigned long) dev;
2698 dev->listen_timer.function = pn533_listen_mode_timer;
2700 skb_queue_head_init(&dev->resp_q);
2702 INIT_LIST_HEAD(&dev->cmd_queue);
2704 usb_set_intfdata(interface, dev);
2706 dev->ops = &pn533_std_frame_ops;
2708 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
2709 dev->device_type = id->driver_info;
2710 switch (dev->device_type) {
2711 case PN533_DEVICE_STD:
2712 protocols = PN533_ALL_PROTOCOLS;
2715 case PN533_DEVICE_PASORI:
2716 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2719 case PN533_DEVICE_ACR122U:
2720 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2721 dev->ops = &pn533_acr122_frame_ops;
2722 dev->protocol_type = PN533_PROTO_REQ_RESP,
2724 rc = pn533_acr122_poweron_rdr(dev);
2726 nfc_dev_err(&dev->interface->dev,
2727 "Couldn't poweron the reader (error %d)",
2734 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2740 memset(&fw_ver, 0, sizeof(fw_ver));
2741 rc = pn533_get_firmware_version(dev, &fw_ver);
2745 nfc_dev_info(&dev->interface->dev,
2746 "NXP PN533 firmware ver %d.%d now attached",
2747 fw_ver.ver, fw_ver.rev);
2750 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2752 dev->ops->tx_header_len +
2753 PN533_CMD_DATAEXCH_HEAD_LEN,
2754 dev->ops->tx_tail_len);
2758 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
2759 nfc_set_drvdata(dev->nfc_dev, dev);
2761 rc = nfc_register_device(dev->nfc_dev);
2765 rc = pn533_setup(dev);
2767 goto unregister_nfc_dev;
2772 nfc_unregister_device(dev->nfc_dev);
2775 nfc_free_device(dev->nfc_dev);
2778 destroy_workqueue(dev->wq);
2780 usb_free_urb(dev->in_urb);
2781 usb_free_urb(dev->out_urb);
2782 usb_put_dev(dev->udev);
2787 static void pn533_disconnect(struct usb_interface *interface)
2790 struct pn533_cmd *cmd, *n;
2792 dev = usb_get_intfdata(interface);
2793 usb_set_intfdata(interface, NULL);
2795 nfc_unregister_device(dev->nfc_dev);
2796 nfc_free_device(dev->nfc_dev);
2798 usb_kill_urb(dev->in_urb);
2799 usb_kill_urb(dev->out_urb);
2801 destroy_workqueue(dev->wq);
2803 skb_queue_purge(&dev->resp_q);
2805 del_timer(&dev->listen_timer);
2807 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
2808 list_del(&cmd->queue);
2812 usb_free_urb(dev->in_urb);
2813 usb_free_urb(dev->out_urb);
2816 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
2819 static struct usb_driver pn533_driver = {
2821 .probe = pn533_probe,
2822 .disconnect = pn533_disconnect,
2823 .id_table = pn533_table,
2826 module_usb_driver(pn533_driver);
2828 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2829 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2830 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2831 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
2832 MODULE_VERSION(VERSION);
2833 MODULE_LICENSE("GPL");