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
86 /* Delay between each poll frame (ms) */
87 #define PN533_POLL_INTERVAL 10
89 /* Standard pn533 frame definitions (standard and extended)*/
90 #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
91 + 2) /* data[0] TFI, data[1] CC */
92 #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
94 #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
95 + 2) /* data[0] TFI, data[1] CC */
97 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
98 #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
101 * Max extended frame payload len, excluding TFI and CC
102 * which are already in PN533_FRAME_HEADER_LEN.
104 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
106 #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
108 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
109 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
110 /* Half start code (3), LEN (4) should be 0xffff for extended frame */
111 #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
112 && (hdr)->datalen_checksum == 0xFF)
113 #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
116 #define PN533_STD_FRAME_SOF 0x00FF
118 /* standard frame identifier: in/out/error */
119 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
120 #define PN533_STD_FRAME_DIR_OUT 0xD4
121 #define PN533_STD_FRAME_DIR_IN 0xD5
123 /* ACS ACR122 pn533 frame definitions */
124 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
126 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
127 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
129 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
130 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
132 /* CCID messages types */
133 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
134 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
136 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
139 #define PN533_FRAME_CMD(f) (f->data[1])
141 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
142 #define PN533_CMD_RF_CONFIGURATION 0x32
143 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
144 #define PN533_CMD_IN_COMM_THRU 0x42
145 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
146 #define PN533_CMD_IN_ATR 0x50
147 #define PN533_CMD_IN_RELEASE 0x52
148 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
150 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
151 #define PN533_CMD_TG_GET_DATA 0x86
152 #define PN533_CMD_TG_SET_DATA 0x8e
153 #define PN533_CMD_UNDEF 0xff
155 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
157 /* PN533 Return codes */
158 #define PN533_CMD_RET_MASK 0x3F
159 #define PN533_CMD_MI_MASK 0x40
160 #define PN533_CMD_RET_SUCCESS 0x00
164 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
165 struct sk_buff *resp);
167 /* structs for pn533 commands */
169 /* PN533_CMD_GET_FIRMWARE_VERSION */
170 struct pn533_fw_version {
177 /* PN533_CMD_RF_CONFIGURATION */
178 #define PN533_CFGITEM_RF_FIELD 0x01
179 #define PN533_CFGITEM_TIMING 0x02
180 #define PN533_CFGITEM_MAX_RETRIES 0x05
181 #define PN533_CFGITEM_PASORI 0x82
183 #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
184 #define PN533_CFGITEM_RF_FIELD_ON 0x1
185 #define PN533_CFGITEM_RF_FIELD_OFF 0x0
187 #define PN533_CONFIG_TIMING_102 0xb
188 #define PN533_CONFIG_TIMING_204 0xc
189 #define PN533_CONFIG_TIMING_409 0xd
190 #define PN533_CONFIG_TIMING_819 0xe
192 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
193 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
195 struct pn533_config_max_retries {
198 u8 mx_rty_passive_act;
201 struct pn533_config_timing {
207 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
209 /* felica commands opcode */
210 #define PN533_FELICA_OPC_SENSF_REQ 0
211 #define PN533_FELICA_OPC_SENSF_RES 1
212 /* felica SENSF_REQ parameters */
213 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
214 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
215 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
216 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
218 /* type B initiator_data values */
219 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
220 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
221 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
223 union pn533_cmd_poll_initdata {
236 /* Poll modulations */
238 PN533_POLL_MOD_106KBPS_A,
239 PN533_POLL_MOD_212KBPS_FELICA,
240 PN533_POLL_MOD_424KBPS_FELICA,
241 PN533_POLL_MOD_106KBPS_JEWEL,
242 PN533_POLL_MOD_847KBPS_B,
245 __PN533_POLL_MOD_AFTER_LAST,
247 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
249 struct pn533_poll_modulations {
253 union pn533_cmd_poll_initdata initiator_data;
258 static const struct pn533_poll_modulations poll_mod[] = {
259 [PN533_POLL_MOD_106KBPS_A] = {
266 [PN533_POLL_MOD_212KBPS_FELICA] = {
270 .initiator_data.felica = {
271 .opcode = PN533_FELICA_OPC_SENSF_REQ,
272 .sc = PN533_FELICA_SENSF_SC_ALL,
273 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
279 [PN533_POLL_MOD_424KBPS_FELICA] = {
283 .initiator_data.felica = {
284 .opcode = PN533_FELICA_OPC_SENSF_REQ,
285 .sc = PN533_FELICA_SENSF_SC_ALL,
286 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
292 [PN533_POLL_MOD_106KBPS_JEWEL] = {
299 [PN533_POLL_MOD_847KBPS_B] = {
303 .initiator_data.type_b = {
304 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
306 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
311 [PN533_LISTEN_MOD] = {
316 /* PN533_CMD_IN_ATR */
318 struct pn533_cmd_activate_response {
330 struct pn533_cmd_jump_dep_response {
344 /* PN533_TG_INIT_AS_TARGET */
345 #define PN533_INIT_TARGET_PASSIVE 0x1
346 #define PN533_INIT_TARGET_DEP 0x2
348 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
349 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
350 #define PN533_INIT_TARGET_RESP_DEP 0x4
352 enum pn533_protocol_type {
353 PN533_PROTO_REQ_ACK_RESP = 0,
358 struct usb_device *udev;
359 struct usb_interface *interface;
360 struct nfc_dev *nfc_dev;
362 enum pn533_protocol_type protocol_type;
367 struct sk_buff_head resp_q;
368 struct sk_buff_head fragment_skb;
370 struct workqueue_struct *wq;
371 struct work_struct cmd_work;
372 struct work_struct cmd_complete_work;
373 struct delayed_work poll_work;
374 struct work_struct mi_rx_work;
375 struct work_struct mi_tx_work;
376 struct work_struct tg_work;
377 struct work_struct rf_work;
379 struct list_head cmd_queue;
380 struct pn533_cmd *cmd;
382 struct mutex cmd_lock; /* protects cmd queue */
384 void *cmd_complete_mi_arg;
385 void *cmd_complete_dep_arg;
387 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
391 u32 listen_protocols;
392 struct timer_list listen_timer;
398 u8 tgt_available_prots;
402 struct pn533_frame_ops *ops;
406 struct list_head queue;
410 struct sk_buff *resp;
412 pn533_send_async_complete_t complete_cb;
413 void *complete_cb_context;
416 struct pn533_std_frame {
424 struct pn533_ext_frame { /* Extended Information frame */
427 __be16 eif_flag; /* fixed to 0xFFFF */
433 struct pn533_frame_ops {
434 void (*tx_frame_init)(void *frame, u8 cmd_code);
435 void (*tx_frame_finish)(void *frame);
436 void (*tx_update_payload_len)(void *frame, int len);
440 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
441 int (*rx_frame_size)(void *frame);
446 u8 (*get_cmd_code)(void *frame);
449 struct pn533_acr122_ccid_hdr {
454 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
455 byte for reposnse msg */
456 u8 data[]; /* payload */
459 struct pn533_acr122_apdu_hdr {
466 struct pn533_acr122_tx_frame {
467 struct pn533_acr122_ccid_hdr ccid;
468 struct pn533_acr122_apdu_hdr apdu;
470 u8 data[]; /* pn533 frame: TFI ... */
473 struct pn533_acr122_rx_frame {
474 struct pn533_acr122_ccid_hdr ccid;
475 u8 data[]; /* pn533 frame : TFI ... */
478 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
480 struct pn533_acr122_tx_frame *frame = _frame;
482 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
483 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
485 frame->ccid.slot = 0;
487 frame->ccid.params[0] = 0;
488 frame->ccid.params[1] = 0;
489 frame->ccid.params[2] = 0;
491 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
492 frame->data[1] = cmd_code;
493 frame->datalen = 2; /* data[0] + data[1] */
495 frame->apdu.class = 0xFF;
501 static void pn533_acr122_tx_frame_finish(void *_frame)
503 struct pn533_acr122_tx_frame *frame = _frame;
505 frame->ccid.datalen += frame->datalen;
508 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
510 struct pn533_acr122_tx_frame *frame = _frame;
512 frame->datalen += len;
515 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
517 struct pn533_acr122_rx_frame *frame = _frame;
519 if (frame->ccid.type != 0x83)
522 if (frame->data[frame->ccid.datalen - 2] == 0x63)
528 static int pn533_acr122_rx_frame_size(void *frame)
530 struct pn533_acr122_rx_frame *f = frame;
532 /* f->ccid.datalen already includes tail length */
533 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
536 static u8 pn533_acr122_get_cmd_code(void *frame)
538 struct pn533_acr122_rx_frame *f = frame;
540 return PN533_FRAME_CMD(f);
543 static struct pn533_frame_ops pn533_acr122_frame_ops = {
544 .tx_frame_init = pn533_acr122_tx_frame_init,
545 .tx_frame_finish = pn533_acr122_tx_frame_finish,
546 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
547 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
548 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
550 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
551 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
552 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
553 .rx_frame_size = pn533_acr122_rx_frame_size,
555 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
556 .get_cmd_code = pn533_acr122_get_cmd_code,
559 /* The rule: value(high byte) + value(low byte) + checksum = 0 */
560 static inline u8 pn533_ext_checksum(u16 value)
562 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
565 /* The rule: value + checksum = 0 */
566 static inline u8 pn533_std_checksum(u8 value)
571 /* The rule: sum(data elements) + checksum = 0 */
572 static u8 pn533_std_data_checksum(u8 *data, int datalen)
577 for (i = 0; i < datalen; i++)
580 return pn533_std_checksum(sum);
583 static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
585 struct pn533_std_frame *frame = _frame;
588 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
589 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
590 PN533_FRAME_CMD(frame) = cmd_code;
594 static void pn533_std_tx_frame_finish(void *_frame)
596 struct pn533_std_frame *frame = _frame;
598 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
600 PN533_STD_FRAME_CHECKSUM(frame) =
601 pn533_std_data_checksum(frame->data, frame->datalen);
603 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
606 static void pn533_std_tx_update_payload_len(void *_frame, int len)
608 struct pn533_std_frame *frame = _frame;
610 frame->datalen += len;
613 static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
616 struct pn533_std_frame *stdf = _frame;
618 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
621 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
622 /* Standard frame code */
623 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
625 checksum = pn533_std_checksum(stdf->datalen);
626 if (checksum != stdf->datalen_checksum)
629 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
630 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
634 struct pn533_ext_frame *eif = _frame;
636 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
638 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
639 if (checksum != eif->datalen_checksum)
642 /* check data checksum */
643 checksum = pn533_std_data_checksum(eif->data,
644 be16_to_cpu(eif->datalen));
645 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
652 static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
654 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
657 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
663 static inline int pn533_std_rx_frame_size(void *frame)
665 struct pn533_std_frame *f = frame;
667 /* check for Extended Information frame */
668 if (PN533_STD_IS_EXTENDED(f)) {
669 struct pn533_ext_frame *eif = frame;
671 return sizeof(struct pn533_ext_frame)
672 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
675 return sizeof(struct pn533_std_frame) + f->datalen +
676 PN533_STD_FRAME_TAIL_LEN;
679 static u8 pn533_std_get_cmd_code(void *frame)
681 struct pn533_std_frame *f = frame;
682 struct pn533_ext_frame *eif = frame;
684 if (PN533_STD_IS_EXTENDED(f))
685 return PN533_FRAME_CMD(eif);
687 return PN533_FRAME_CMD(f);
690 static struct pn533_frame_ops pn533_std_frame_ops = {
691 .tx_frame_init = pn533_std_tx_frame_init,
692 .tx_frame_finish = pn533_std_tx_frame_finish,
693 .tx_update_payload_len = pn533_std_tx_update_payload_len,
694 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
695 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
697 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
698 .rx_frame_size = pn533_std_rx_frame_size,
699 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
700 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
702 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
703 .get_cmd_code = pn533_std_get_cmd_code,
706 static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
708 return (dev->ops->get_cmd_code(frame) ==
709 PN533_CMD_RESPONSE(dev->cmd->code));
712 static void pn533_recv_response(struct urb *urb)
714 struct pn533 *dev = urb->context;
715 struct pn533_cmd *cmd = dev->cmd;
718 cmd->status = urb->status;
720 switch (urb->status) {
725 nfc_dev_dbg(&dev->interface->dev,
726 "The urb has been canceled (status %d)",
731 nfc_dev_err(&dev->interface->dev,
732 "Urb failure (status %d)", urb->status);
736 in_frame = dev->in_urb->transfer_buffer;
738 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
739 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
740 dev->ops->rx_frame_size(in_frame), false);
742 if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
743 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
748 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
749 nfc_dev_err(&dev->interface->dev,
750 "It it not the response to the last command");
756 queue_work(dev->wq, &dev->cmd_complete_work);
759 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
761 dev->in_urb->complete = pn533_recv_response;
763 return usb_submit_urb(dev->in_urb, flags);
766 static void pn533_recv_ack(struct urb *urb)
768 struct pn533 *dev = urb->context;
769 struct pn533_cmd *cmd = dev->cmd;
770 struct pn533_std_frame *in_frame;
773 cmd->status = urb->status;
775 switch (urb->status) {
780 nfc_dev_dbg(&dev->interface->dev,
781 "The urb has been stopped (status %d)",
786 nfc_dev_err(&dev->interface->dev,
787 "Urb failure (status %d)", urb->status);
791 in_frame = dev->in_urb->transfer_buffer;
793 if (!pn533_std_rx_frame_is_ack(in_frame)) {
794 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
799 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
801 nfc_dev_err(&dev->interface->dev,
802 "usb_submit_urb failed with result %d", rc);
810 queue_work(dev->wq, &dev->cmd_complete_work);
813 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
815 dev->in_urb->complete = pn533_recv_ack;
817 return usb_submit_urb(dev->in_urb, flags);
820 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
822 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
823 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
826 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
828 dev->out_urb->transfer_buffer = ack;
829 dev->out_urb->transfer_buffer_length = sizeof(ack);
830 rc = usb_submit_urb(dev->out_urb, flags);
835 static int __pn533_send_frame_async(struct pn533 *dev,
842 dev->out_urb->transfer_buffer = out->data;
843 dev->out_urb->transfer_buffer_length = out->len;
845 dev->in_urb->transfer_buffer = in->data;
846 dev->in_urb->transfer_buffer_length = in_len;
848 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
849 out->data, out->len, false);
851 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
855 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
856 /* request for response for sent packet directly */
857 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
860 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
861 /* request for ACK if that's the case */
862 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
870 usb_unlink_urb(dev->out_urb);
874 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
877 /* payload is already there, just update datalen */
878 int payload_len = skb->len;
879 struct pn533_frame_ops *ops = dev->ops;
882 skb_push(skb, ops->tx_header_len);
883 skb_put(skb, ops->tx_tail_len);
885 ops->tx_frame_init(skb->data, cmd_code);
886 ops->tx_update_payload_len(skb->data, payload_len);
887 ops->tx_frame_finish(skb->data);
890 static int pn533_send_async_complete(struct pn533 *dev)
892 struct pn533_cmd *cmd = dev->cmd;
893 int status = cmd->status;
895 struct sk_buff *req = cmd->req;
896 struct sk_buff *resp = cmd->resp;
903 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
909 skb_put(resp, dev->ops->rx_frame_size(resp->data));
910 skb_pull(resp, dev->ops->rx_header_len);
911 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
913 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
921 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
922 struct sk_buff *req, struct sk_buff *resp,
924 pn533_send_async_complete_t complete_cb,
925 void *complete_cb_context)
927 struct pn533_cmd *cmd;
930 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
932 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
936 cmd->code = cmd_code;
939 cmd->resp_len = resp_len;
940 cmd->complete_cb = complete_cb;
941 cmd->complete_cb_context = complete_cb_context;
943 pn533_build_cmd_frame(dev, cmd_code, req);
945 mutex_lock(&dev->cmd_lock);
947 if (!dev->cmd_pending) {
948 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
952 dev->cmd_pending = 1;
957 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
960 INIT_LIST_HEAD(&cmd->queue);
961 list_add_tail(&cmd->queue, &dev->cmd_queue);
968 mutex_unlock(&dev->cmd_lock);
972 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
974 pn533_send_async_complete_t complete_cb,
975 void *complete_cb_context)
977 struct sk_buff *resp;
979 int resp_len = dev->ops->rx_header_len +
980 dev->ops->max_payload_len +
981 dev->ops->rx_tail_len;
983 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
987 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
988 complete_cb_context);
995 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
997 pn533_send_async_complete_t complete_cb,
998 void *complete_cb_context)
1000 struct sk_buff *resp;
1002 int resp_len = dev->ops->rx_header_len +
1003 dev->ops->max_payload_len +
1004 dev->ops->rx_tail_len;
1006 resp = alloc_skb(resp_len, GFP_KERNEL);
1010 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
1011 complete_cb_context);
1013 dev_kfree_skb(resp);
1019 * pn533_send_cmd_direct_async
1021 * The function sends a piority cmd directly to the chip omiting the cmd
1022 * queue. It's intended to be used by chaining mechanism of received responses
1023 * where the host has to request every single chunk of data before scheduling
1024 * next cmd from the queue.
1026 static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
1027 struct sk_buff *req,
1028 pn533_send_async_complete_t complete_cb,
1029 void *complete_cb_context)
1031 struct sk_buff *resp;
1032 struct pn533_cmd *cmd;
1034 int resp_len = dev->ops->rx_header_len +
1035 dev->ops->max_payload_len +
1036 dev->ops->rx_tail_len;
1038 resp = alloc_skb(resp_len, GFP_KERNEL);
1042 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1044 dev_kfree_skb(resp);
1048 cmd->code = cmd_code;
1051 cmd->resp_len = resp_len;
1052 cmd->complete_cb = complete_cb;
1053 cmd->complete_cb_context = complete_cb_context;
1055 pn533_build_cmd_frame(dev, cmd_code, req);
1057 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
1059 dev_kfree_skb(resp);
1068 static void pn533_wq_cmd_complete(struct work_struct *work)
1070 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1073 rc = pn533_send_async_complete(dev);
1074 if (rc != -EINPROGRESS)
1075 queue_work(dev->wq, &dev->cmd_work);
1078 static void pn533_wq_cmd(struct work_struct *work)
1080 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1081 struct pn533_cmd *cmd;
1084 mutex_lock(&dev->cmd_lock);
1086 if (list_empty(&dev->cmd_queue)) {
1087 dev->cmd_pending = 0;
1088 mutex_unlock(&dev->cmd_lock);
1092 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1094 list_del(&cmd->queue);
1096 mutex_unlock(&dev->cmd_lock);
1098 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
1100 dev_kfree_skb(cmd->req);
1101 dev_kfree_skb(cmd->resp);
1109 struct pn533_sync_cmd_response {
1110 struct sk_buff *resp;
1111 struct completion done;
1114 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1115 struct sk_buff *resp)
1117 struct pn533_sync_cmd_response *arg = _arg;
1120 complete(&arg->done);
1125 /* pn533_send_cmd_sync
1127 * Please note the req parameter is freed inside the function to
1128 * limit a number of return value interpretations by the caller.
1130 * 1. negative in case of error during TX path -> req should be freed
1132 * 2. negative in case of error during RX path -> req should not be freed
1133 * as it's been already freed at the begining of RX path by
1134 * async_complete_cb.
1136 * 3. valid pointer in case of succesfult RX path
1138 * A caller has to check a return value with IS_ERR macro. If the test pass,
1139 * the returned pointer is valid.
1142 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1143 struct sk_buff *req)
1146 struct pn533_sync_cmd_response arg;
1148 init_completion(&arg.done);
1150 rc = pn533_send_cmd_async(dev, cmd_code, req,
1151 pn533_send_sync_complete, &arg);
1157 wait_for_completion(&arg.done);
1162 static void pn533_send_complete(struct urb *urb)
1164 struct pn533 *dev = urb->context;
1166 switch (urb->status) {
1168 break; /* success */
1171 nfc_dev_dbg(&dev->interface->dev,
1172 "The urb has been stopped (status %d)",
1177 nfc_dev_err(&dev->interface->dev,
1178 "Urb failure (status %d)", urb->status);
1182 static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1184 /* ACR122U does not support any command which aborts last
1185 * issued command i.e. as ACK for standard PN533. Additionally,
1186 * it behaves stange, sending broken or incorrect responses,
1187 * when we cancel urb before the chip will send response.
1189 if (dev->device_type == PN533_DEVICE_ACR122U)
1192 /* An ack will cancel the last issued command */
1193 pn533_send_ack(dev, flags);
1195 /* cancel the urb request */
1196 usb_kill_urb(dev->in_urb);
1199 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
1201 struct sk_buff *skb;
1203 skb = alloc_skb(dev->ops->tx_header_len +
1205 dev->ops->tx_tail_len, GFP_KERNEL);
1208 skb_reserve(skb, dev->ops->tx_header_len);
1213 struct pn533_target_type_a {
1221 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1222 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1223 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1225 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1226 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1228 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1229 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1231 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1232 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1233 #define PN533_TYPE_A_SEL_PROT_DEP 2
1234 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1236 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1237 int target_data_len)
1242 if (target_data_len < sizeof(struct pn533_target_type_a))
1245 /* The lenght check of nfcid[] and ats[] are not being performed because
1246 the values are not being used */
1248 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1249 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1250 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1252 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1253 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1254 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1255 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1258 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1259 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1265 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1268 struct pn533_target_type_a *tgt_type_a;
1270 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1272 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1275 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1276 case PN533_TYPE_A_SEL_PROT_MIFARE:
1277 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1279 case PN533_TYPE_A_SEL_PROT_ISO14443:
1280 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1282 case PN533_TYPE_A_SEL_PROT_DEP:
1283 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1285 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1286 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1287 NFC_PROTO_NFC_DEP_MASK;
1291 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1292 nfc_tgt->sel_res = tgt_type_a->sel_res;
1293 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1294 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1299 struct pn533_target_felica {
1302 u8 nfcid2[NFC_NFCID2_MAXSIZE];
1308 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1309 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1311 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1312 int target_data_len)
1314 if (target_data_len < sizeof(struct pn533_target_felica))
1317 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1323 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1326 struct pn533_target_felica *tgt_felica;
1328 tgt_felica = (struct pn533_target_felica *)tgt_data;
1330 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1333 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1334 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1335 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1337 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1339 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1340 nfc_tgt->sensf_res_len = 9;
1342 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
1343 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
1348 struct pn533_target_jewel {
1353 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1354 int target_data_len)
1359 if (target_data_len < sizeof(struct pn533_target_jewel))
1362 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1363 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1364 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1366 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1367 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1368 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1369 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1375 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1378 struct pn533_target_jewel *tgt_jewel;
1380 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1382 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1385 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1386 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1387 nfc_tgt->nfcid1_len = 4;
1388 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1393 struct pn533_type_b_prot_info {
1399 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1400 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1401 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1403 struct pn533_type_b_sens_res {
1407 struct pn533_type_b_prot_info prot_info;
1410 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1412 struct pn533_target_type_b {
1413 struct pn533_type_b_sens_res sensb_res;
1418 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1419 int target_data_len)
1421 if (target_data_len < sizeof(struct pn533_target_type_b))
1424 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1427 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1428 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1434 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1437 struct pn533_target_type_b *tgt_type_b;
1439 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1441 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1444 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1449 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1452 struct nfc_target nfc_tgt;
1455 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1456 dev->poll_mod_curr);
1461 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1463 switch (dev->poll_mod_curr) {
1464 case PN533_POLL_MOD_106KBPS_A:
1465 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1467 case PN533_POLL_MOD_212KBPS_FELICA:
1468 case PN533_POLL_MOD_424KBPS_FELICA:
1469 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1471 case PN533_POLL_MOD_106KBPS_JEWEL:
1472 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1474 case PN533_POLL_MOD_847KBPS_B:
1475 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1478 nfc_dev_err(&dev->interface->dev,
1479 "Unknown current poll modulation");
1486 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1487 nfc_dev_dbg(&dev->interface->dev,
1488 "The Tg found doesn't have the desired protocol");
1492 nfc_dev_dbg(&dev->interface->dev,
1493 "Target found - supported protocols: 0x%x",
1494 nfc_tgt.supported_protocols);
1496 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1498 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1503 static inline void pn533_poll_next_mod(struct pn533 *dev)
1505 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1508 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1510 dev->poll_mod_count = 0;
1513 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1515 dev->poll_mod_active[dev->poll_mod_count] =
1516 (struct pn533_poll_modulations *)&poll_mod[mod_index];
1517 dev->poll_mod_count++;
1520 static void pn533_poll_create_mod_list(struct pn533 *dev,
1521 u32 im_protocols, u32 tm_protocols)
1523 pn533_poll_reset_mod_list(dev);
1525 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1526 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1527 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1528 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1530 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1531 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1532 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1533 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1536 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1537 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1539 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1540 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1543 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1546 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1548 u8 nbtg, tg, *tgdata;
1551 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1553 nbtg = resp->data[0];
1555 tgdata = &resp->data[2];
1556 tgdata_len = resp->len - 2; /* nbtg + tg */
1559 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1561 /* We must stop the poll after a valid target found */
1563 pn533_poll_reset_mod_list(dev);
1571 static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1573 struct sk_buff *skb;
1574 u8 *felica, *nfcid3, *gb;
1576 u8 *gbytes = dev->gb;
1577 size_t gbytes_len = dev->gb_len;
1579 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1580 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1581 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1582 0xff, 0xff}; /* System code */
1584 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1586 0x40}; /* SEL_RES for DEP */
1588 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1589 felica (18), nfcid3 (10), gb_len (1) */
1593 skb = pn533_alloc_skb(dev, skb_len);
1597 /* DEP support only */
1598 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1601 memcpy(skb_put(skb, 6), mifare_params, 6);
1604 felica = skb_put(skb, 18);
1605 memcpy(felica, felica_params, 18);
1606 get_random_bytes(felica + 2, 6);
1609 nfcid3 = skb_put(skb, 10);
1610 memset(nfcid3, 0, 10);
1611 memcpy(nfcid3, felica, 8);
1614 *skb_put(skb, 1) = gbytes_len;
1616 gb = skb_put(skb, gbytes_len);
1617 memcpy(gb, gbytes, gbytes_len);
1620 *skb_put(skb, 1) = 0;
1625 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1626 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1627 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1628 struct sk_buff *resp)
1632 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1635 return PTR_ERR(resp);
1637 status = resp->data[0];
1638 skb_pull(resp, sizeof(status));
1641 nfc_tm_deactivated(dev->nfc_dev);
1643 dev_kfree_skb(resp);
1647 return nfc_tm_data_received(dev->nfc_dev, resp);
1650 static void pn533_wq_tg_get_data(struct work_struct *work)
1652 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1654 struct sk_buff *skb;
1657 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1659 skb = pn533_alloc_skb(dev, 0);
1663 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1664 pn533_tm_get_data_complete, NULL);
1672 #define ATR_REQ_GB_OFFSET 17
1673 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1675 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1679 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1681 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1684 mode = resp->data[0];
1685 cmd = &resp->data[1];
1687 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1690 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1691 PN533_INIT_TARGET_RESP_ACTIVE)
1692 comm_mode = NFC_COMM_ACTIVE;
1694 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1697 gb = cmd + ATR_REQ_GB_OFFSET;
1698 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1700 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1701 comm_mode, gb, gb_len);
1703 nfc_dev_err(&dev->interface->dev,
1704 "Error when signaling target activation");
1709 queue_work(dev->wq, &dev->tg_work);
1714 static void pn533_listen_mode_timer(unsigned long data)
1716 struct pn533 *dev = (struct pn533 *)data;
1718 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1720 dev->cancel_listen = 1;
1722 pn533_poll_next_mod(dev);
1724 queue_delayed_work(dev->wq, &dev->poll_work,
1725 msecs_to_jiffies(PN533_POLL_INTERVAL));
1728 static int pn533_rf_complete(struct pn533 *dev, void *arg,
1729 struct sk_buff *resp)
1733 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1738 nfc_dev_err(&dev->interface->dev, "%s RF setting error %d",
1744 queue_delayed_work(dev->wq, &dev->poll_work,
1745 msecs_to_jiffies(PN533_POLL_INTERVAL));
1747 dev_kfree_skb(resp);
1751 static void pn533_wq_rf(struct work_struct *work)
1753 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1754 struct sk_buff *skb;
1757 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1759 skb = pn533_alloc_skb(dev, 2);
1763 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
1764 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
1766 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1767 pn533_rf_complete, NULL);
1770 nfc_dev_err(&dev->interface->dev, "RF setting error %d", rc);
1776 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1777 struct sk_buff *resp)
1779 struct pn533_poll_modulations *cur_mod;
1782 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1787 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1790 if (rc == -ENOENT) {
1791 if (dev->poll_mod_count != 0)
1795 } else if (rc < 0) {
1796 nfc_dev_err(&dev->interface->dev,
1797 "Error %d when running poll", rc);
1802 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1804 if (cur_mod->len == 0) { /* Target mode */
1805 del_timer(&dev->listen_timer);
1806 rc = pn533_init_target_complete(dev, resp);
1810 /* Initiator mode */
1811 rc = pn533_start_poll_complete(dev, resp);
1815 if (!dev->poll_mod_count) {
1816 nfc_dev_dbg(&dev->interface->dev, "Polling has been stopped.");
1820 pn533_poll_next_mod(dev);
1821 /* Not target found, turn radio off */
1822 queue_work(dev->wq, &dev->rf_work);
1825 dev_kfree_skb(resp);
1829 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1831 pn533_poll_reset_mod_list(dev);
1832 dev->poll_protocols = 0;
1836 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1837 struct pn533_poll_modulations *mod)
1839 struct sk_buff *skb;
1841 skb = pn533_alloc_skb(dev, mod->len);
1845 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1850 static int pn533_send_poll_frame(struct pn533 *dev)
1852 struct pn533_poll_modulations *mod;
1853 struct sk_buff *skb;
1857 mod = dev->poll_mod_active[dev->poll_mod_curr];
1859 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1860 __func__, mod->len);
1862 if (mod->len == 0) { /* Listen mode */
1863 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1864 skb = pn533_alloc_poll_tg_frame(dev);
1865 } else { /* Polling mode */
1866 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1867 skb = pn533_alloc_poll_in_frame(dev, mod);
1871 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1875 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1879 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1885 static void pn533_wq_poll(struct work_struct *work)
1887 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
1888 struct pn533_poll_modulations *cur_mod;
1891 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1893 nfc_dev_dbg(&dev->interface->dev,
1894 "%s cancel_listen %d modulation len %d",
1895 __func__, dev->cancel_listen, cur_mod->len);
1897 if (dev->cancel_listen == 1) {
1898 dev->cancel_listen = 0;
1899 pn533_abort_cmd(dev, GFP_ATOMIC);
1902 rc = pn533_send_poll_frame(dev);
1906 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1907 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1912 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1913 u32 im_protocols, u32 tm_protocols)
1915 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1918 nfc_dev_dbg(&dev->interface->dev,
1919 "%s: im protocols 0x%x tm protocols 0x%x",
1920 __func__, im_protocols, tm_protocols);
1922 if (dev->tgt_active_prot) {
1923 nfc_dev_err(&dev->interface->dev,
1924 "Cannot poll with a target already activated");
1928 if (dev->tgt_mode) {
1929 nfc_dev_err(&dev->interface->dev,
1930 "Cannot poll while already being activated");
1935 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1936 if (dev->gb == NULL)
1940 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1941 dev->poll_protocols = im_protocols;
1942 dev->listen_protocols = tm_protocols;
1944 /* Do not always start polling from the same modulation */
1945 get_random_bytes(&rand_mod, sizeof(rand_mod));
1946 rand_mod %= dev->poll_mod_count;
1947 dev->poll_mod_curr = rand_mod;
1949 return pn533_send_poll_frame(dev);
1952 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1954 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1956 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1958 del_timer(&dev->listen_timer);
1960 if (!dev->poll_mod_count) {
1961 nfc_dev_dbg(&dev->interface->dev,
1962 "Polling operation was not running");
1966 pn533_abort_cmd(dev, GFP_KERNEL);
1967 flush_delayed_work(&dev->poll_work);
1968 pn533_poll_reset_mod_list(dev);
1971 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1973 struct pn533_cmd_activate_response *rsp;
1977 struct sk_buff *skb;
1978 struct sk_buff *resp;
1980 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1982 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1986 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1987 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1989 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1991 return PTR_ERR(resp);
1993 rsp = (struct pn533_cmd_activate_response *)resp->data;
1994 rc = rsp->status & PN533_CMD_RET_MASK;
1995 if (rc != PN533_CMD_RET_SUCCESS) {
1996 nfc_dev_err(&dev->interface->dev,
1997 "Target activation failed (error 0x%x)", rc);
1998 dev_kfree_skb(resp);
2002 /* ATR_RES general bytes are located at offset 16 */
2003 gt_len = resp->len - 16;
2004 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
2006 dev_kfree_skb(resp);
2010 static int pn533_activate_target(struct nfc_dev *nfc_dev,
2011 struct nfc_target *target, u32 protocol)
2013 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2016 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
2019 if (dev->poll_mod_count) {
2020 nfc_dev_err(&dev->interface->dev,
2021 "Cannot activate while polling");
2025 if (dev->tgt_active_prot) {
2026 nfc_dev_err(&dev->interface->dev,
2027 "There is already an active target");
2031 if (!dev->tgt_available_prots) {
2032 nfc_dev_err(&dev->interface->dev,
2033 "There is no available target to activate");
2037 if (!(dev->tgt_available_prots & (1 << protocol))) {
2038 nfc_dev_err(&dev->interface->dev,
2039 "Target doesn't support requested proto %u",
2044 if (protocol == NFC_PROTO_NFC_DEP) {
2045 rc = pn533_activate_target_nfcdep(dev);
2047 nfc_dev_err(&dev->interface->dev,
2048 "Activating target with DEP failed %d", rc);
2053 dev->tgt_active_prot = protocol;
2054 dev->tgt_available_prots = 0;
2059 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
2060 struct nfc_target *target)
2062 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2064 struct sk_buff *skb;
2065 struct sk_buff *resp;
2069 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2071 if (!dev->tgt_active_prot) {
2072 nfc_dev_err(&dev->interface->dev, "There is no active target");
2076 dev->tgt_active_prot = 0;
2077 skb_queue_purge(&dev->resp_q);
2079 skb = pn533_alloc_skb(dev, sizeof(u8));
2083 *skb_put(skb, 1) = 1; /* TG*/
2085 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
2089 rc = resp->data[0] & PN533_CMD_RET_MASK;
2090 if (rc != PN533_CMD_RET_SUCCESS)
2091 nfc_dev_err(&dev->interface->dev,
2092 "Error 0x%x when releasing the target", rc);
2094 dev_kfree_skb(resp);
2099 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
2100 struct sk_buff *resp)
2102 struct pn533_cmd_jump_dep_response *rsp;
2105 u8 active = *(u8 *)arg;
2110 return PTR_ERR(resp);
2112 if (dev->tgt_available_prots &&
2113 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
2114 nfc_dev_err(&dev->interface->dev,
2115 "The target does not support DEP");
2120 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
2122 rc = rsp->status & PN533_CMD_RET_MASK;
2123 if (rc != PN533_CMD_RET_SUCCESS) {
2124 nfc_dev_err(&dev->interface->dev,
2125 "Bringing DEP link up failed (error 0x%x)", rc);
2129 if (!dev->tgt_available_prots) {
2130 struct nfc_target nfc_target;
2132 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
2134 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2135 nfc_target.nfcid1_len = 10;
2136 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
2137 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2141 dev->tgt_available_prots = 0;
2144 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2146 /* ATR_RES general bytes are located at offset 17 */
2147 target_gt_len = resp->len - 17;
2148 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
2149 rsp->gt, target_gt_len);
2151 rc = nfc_dep_link_is_up(dev->nfc_dev,
2152 dev->nfc_dev->targets[0].idx,
2153 !active, NFC_RF_INITIATOR);
2156 dev_kfree_skb(resp);
2160 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
2161 #define PASSIVE_DATA_LEN 5
2162 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2163 u8 comm_mode, u8 *gb, size_t gb_len)
2165 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2166 struct sk_buff *skb;
2168 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
2170 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2172 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2174 if (dev->poll_mod_count) {
2175 nfc_dev_err(&dev->interface->dev,
2176 "Cannot bring the DEP link up while polling");
2180 if (dev->tgt_active_prot) {
2181 nfc_dev_err(&dev->interface->dev,
2182 "There is already an active target");
2186 skb_len = 3 + gb_len; /* ActPass + BR + Next */
2187 skb_len += PASSIVE_DATA_LEN;
2190 skb_len += NFC_NFCID3_MAXSIZE;
2191 if (target && !target->nfcid2_len) {
2194 get_random_bytes(nfcid3 + 2, 6);
2197 skb = pn533_alloc_skb(dev, skb_len);
2201 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2202 *skb_put(skb, 1) = 0x02; /* 424 kbps */
2204 next = skb_put(skb, 1); /* Next */
2207 /* Copy passive data */
2208 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
2211 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2212 if (target && target->nfcid2_len)
2213 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2214 target->nfcid2_len);
2216 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
2217 NFC_NFCID3_MAXSIZE);
2220 if (gb != NULL && gb_len > 0) {
2221 memcpy(skb_put(skb, gb_len), gb, gb_len);
2222 *next |= 4; /* We have some Gi */
2227 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2235 pn533_rf_field(dev->nfc_dev, 0);
2237 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2238 pn533_in_dep_link_up_complete, arg);
2248 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2250 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2252 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2254 pn533_poll_reset_mod_list(dev);
2256 if (dev->tgt_mode || dev->tgt_active_prot)
2257 pn533_abort_cmd(dev, GFP_KERNEL);
2259 dev->tgt_active_prot = 0;
2262 skb_queue_purge(&dev->resp_q);
2267 struct pn533_data_exchange_arg {
2268 data_exchange_cb_t cb;
2272 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2274 struct sk_buff *skb, *tmp, *t;
2275 unsigned int skb_len = 0, tmp_len = 0;
2277 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2279 if (skb_queue_empty(&dev->resp_q))
2282 if (skb_queue_len(&dev->resp_q) == 1) {
2283 skb = skb_dequeue(&dev->resp_q);
2287 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2288 skb_len += tmp->len;
2290 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2293 skb = alloc_skb(skb_len, GFP_KERNEL);
2297 skb_put(skb, skb_len);
2299 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2300 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2301 tmp_len += tmp->len;
2305 skb_queue_purge(&dev->resp_q);
2310 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2311 struct sk_buff *resp)
2313 struct pn533_data_exchange_arg *arg = _arg;
2314 struct sk_buff *skb;
2318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2325 status = resp->data[0];
2326 ret = status & PN533_CMD_RET_MASK;
2327 mi = status & PN533_CMD_MI_MASK;
2329 skb_pull(resp, sizeof(status));
2331 if (ret != PN533_CMD_RET_SUCCESS) {
2332 nfc_dev_err(&dev->interface->dev,
2333 "Exchanging data failed (error 0x%x)", ret);
2338 skb_queue_tail(&dev->resp_q, resp);
2341 dev->cmd_complete_mi_arg = arg;
2342 queue_work(dev->wq, &dev->mi_rx_work);
2343 return -EINPROGRESS;
2346 /* Prepare for the next round */
2347 if (skb_queue_len(&dev->fragment_skb) > 0) {
2348 dev->cmd_complete_dep_arg = arg;
2349 queue_work(dev->wq, &dev->mi_tx_work);
2351 return -EINPROGRESS;
2354 skb = pn533_build_response(dev);
2358 arg->cb(arg->cb_context, skb, 0);
2363 dev_kfree_skb(resp);
2365 skb_queue_purge(&dev->resp_q);
2366 arg->cb(arg->cb_context, NULL, rc);
2371 /* Split the Tx skb into small chunks */
2372 static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2374 struct sk_buff *frag;
2378 /* Remaining size */
2379 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2380 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2382 frag_size = skb->len;
2384 /* Allocate and reserve */
2385 frag = pn533_alloc_skb(dev, frag_size);
2387 skb_queue_purge(&dev->fragment_skb);
2391 /* Reserve the TG/MI byte */
2392 skb_reserve(frag, 1);
2395 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2396 *skb_push(frag, sizeof(u8)) = (PN533_CMD_MI_MASK | 1);
2398 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2400 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2402 /* Reduce the size of incoming buffer */
2403 skb_pull(skb, frag_size);
2405 /* Add this to skb_queue */
2406 skb_queue_tail(&dev->fragment_skb, frag);
2408 } while (skb->len > 0);
2412 return skb_queue_len(&dev->fragment_skb);
2415 static int pn533_transceive(struct nfc_dev *nfc_dev,
2416 struct nfc_target *target, struct sk_buff *skb,
2417 data_exchange_cb_t cb, void *cb_context)
2419 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2420 struct pn533_data_exchange_arg *arg = NULL;
2423 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2425 if (!dev->tgt_active_prot) {
2426 nfc_dev_err(&dev->interface->dev,
2427 "Can't exchange data if there is no active target");
2432 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2439 arg->cb_context = cb_context;
2441 switch (dev->device_type) {
2442 case PN533_DEVICE_PASORI:
2443 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2444 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2446 pn533_data_exchange_complete,
2453 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2454 rc = pn533_fill_fragment_skbs(dev, skb);
2458 skb = skb_dequeue(&dev->fragment_skb);
2464 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2467 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2468 skb, pn533_data_exchange_complete,
2474 if (rc < 0) /* rc from send_async */
2485 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2486 struct sk_buff *resp)
2490 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2493 return PTR_ERR(resp);
2495 status = resp->data[0];
2497 dev_kfree_skb(resp);
2500 nfc_tm_deactivated(dev->nfc_dev);
2507 queue_work(dev->wq, &dev->tg_work);
2512 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2514 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2517 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2519 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2520 nfc_dev_err(&dev->interface->dev,
2521 "Data length greater than the max allowed: %d",
2522 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2526 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2527 pn533_tm_send_complete, NULL);
2534 static void pn533_wq_mi_recv(struct work_struct *work)
2536 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2538 struct sk_buff *skb;
2541 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2543 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2547 switch (dev->device_type) {
2548 case PN533_DEVICE_PASORI:
2549 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2550 rc = pn533_send_cmd_direct_async(dev,
2551 PN533_CMD_IN_COMM_THRU,
2553 pn533_data_exchange_complete,
2554 dev->cmd_complete_mi_arg);
2559 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
2561 rc = pn533_send_cmd_direct_async(dev,
2562 PN533_CMD_IN_DATA_EXCHANGE,
2564 pn533_data_exchange_complete,
2565 dev->cmd_complete_mi_arg);
2570 if (rc == 0) /* success */
2573 nfc_dev_err(&dev->interface->dev,
2574 "Error %d when trying to perform data_exchange", rc);
2577 kfree(dev->cmd_complete_mi_arg);
2580 pn533_send_ack(dev, GFP_KERNEL);
2581 queue_work(dev->wq, &dev->cmd_work);
2584 static void pn533_wq_mi_send(struct work_struct *work)
2586 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2587 struct sk_buff *skb;
2590 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2592 /* Grab the first skb in the queue */
2593 skb = skb_dequeue(&dev->fragment_skb);
2595 if (skb == NULL) { /* No more data */
2596 /* Reset the queue for future use */
2597 skb_queue_head_init(&dev->fragment_skb);
2601 switch (dev->device_type) {
2602 case PN533_DEVICE_PASORI:
2603 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2608 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2610 pn533_data_exchange_complete,
2611 dev->cmd_complete_dep_arg);
2616 /* Still some fragments? */
2617 rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
2619 pn533_data_exchange_complete,
2620 dev->cmd_complete_dep_arg);
2625 if (rc == 0) /* success */
2628 nfc_dev_err(&dev->interface->dev,
2629 "Error %d when trying to perform data_exchange", rc);
2632 kfree(dev->cmd_complete_dep_arg);
2635 pn533_send_ack(dev, GFP_KERNEL);
2636 queue_work(dev->wq, &dev->cmd_work);
2639 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2642 struct sk_buff *skb;
2643 struct sk_buff *resp;
2647 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2649 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2651 skb = pn533_alloc_skb(dev, skb_len);
2655 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2656 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2658 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2660 return PTR_ERR(resp);
2662 dev_kfree_skb(resp);
2666 static int pn533_get_firmware_version(struct pn533 *dev,
2667 struct pn533_fw_version *fv)
2669 struct sk_buff *skb;
2670 struct sk_buff *resp;
2672 skb = pn533_alloc_skb(dev, 0);
2676 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2678 return PTR_ERR(resp);
2680 fv->ic = resp->data[0];
2681 fv->ver = resp->data[1];
2682 fv->rev = resp->data[2];
2683 fv->support = resp->data[3];
2685 dev_kfree_skb(resp);
2689 static int pn533_pasori_fw_reset(struct pn533 *dev)
2691 struct sk_buff *skb;
2692 struct sk_buff *resp;
2694 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2696 skb = pn533_alloc_skb(dev, sizeof(u8));
2700 *skb_put(skb, sizeof(u8)) = 0x1;
2702 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2704 return PTR_ERR(resp);
2706 dev_kfree_skb(resp);
2711 struct pn533_acr122_poweron_rdr_arg {
2713 struct completion done;
2716 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2718 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2720 nfc_dev_dbg(&urb->dev->dev, "%s", __func__);
2722 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2723 urb->transfer_buffer, urb->transfer_buffer_length,
2726 arg->rc = urb->status;
2727 complete(&arg->done);
2730 static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2732 /* Power on th reader (CCID cmd) */
2733 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2734 0, 0, 0, 0, 0, 0, 3, 0, 0};
2738 struct pn533_acr122_poweron_rdr_arg arg;
2740 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2742 init_completion(&arg.done);
2743 cntx = dev->in_urb->context; /* backup context */
2745 dev->in_urb->transfer_buffer = buf;
2746 dev->in_urb->transfer_buffer_length = 255;
2747 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2748 dev->in_urb->context = &arg;
2750 dev->out_urb->transfer_buffer = cmd;
2751 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2753 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2754 cmd, sizeof(cmd), false);
2756 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2758 nfc_dev_err(&dev->interface->dev,
2759 "Reader power on cmd error %d", rc);
2763 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2765 nfc_dev_err(&dev->interface->dev,
2766 "Can't submit for reader power on cmd response %d",
2771 wait_for_completion(&arg.done);
2772 dev->in_urb->context = cntx; /* restore context */
2777 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2779 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2783 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2785 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2786 (u8 *)&rf_field, 1);
2788 nfc_dev_err(&dev->interface->dev,
2789 "Error on setting RF field");
2796 int pn533_dev_up(struct nfc_dev *nfc_dev)
2798 return pn533_rf_field(nfc_dev, 1);
2801 int pn533_dev_down(struct nfc_dev *nfc_dev)
2803 return pn533_rf_field(nfc_dev, 0);
2806 static struct nfc_ops pn533_nfc_ops = {
2807 .dev_up = pn533_dev_up,
2808 .dev_down = pn533_dev_down,
2809 .dep_link_up = pn533_dep_link_up,
2810 .dep_link_down = pn533_dep_link_down,
2811 .start_poll = pn533_start_poll,
2812 .stop_poll = pn533_stop_poll,
2813 .activate_target = pn533_activate_target,
2814 .deactivate_target = pn533_deactivate_target,
2815 .im_transceive = pn533_transceive,
2816 .tm_send = pn533_tm_send,
2819 static int pn533_setup(struct pn533 *dev)
2821 struct pn533_config_max_retries max_retries;
2822 struct pn533_config_timing timing;
2823 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2826 switch (dev->device_type) {
2827 case PN533_DEVICE_STD:
2828 case PN533_DEVICE_PASORI:
2829 case PN533_DEVICE_ACR122U:
2830 max_retries.mx_rty_atr = 0x2;
2831 max_retries.mx_rty_psl = 0x1;
2832 max_retries.mx_rty_passive_act =
2833 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2835 timing.rfu = PN533_CONFIG_TIMING_102;
2836 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2837 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2842 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2847 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2848 (u8 *)&max_retries, sizeof(max_retries));
2850 nfc_dev_err(&dev->interface->dev,
2851 "Error on setting MAX_RETRIES config");
2856 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2857 (u8 *)&timing, sizeof(timing));
2859 nfc_dev_err(&dev->interface->dev,
2860 "Error on setting RF timings");
2864 switch (dev->device_type) {
2865 case PN533_DEVICE_STD:
2868 case PN533_DEVICE_PASORI:
2869 pn533_pasori_fw_reset(dev);
2871 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2874 nfc_dev_err(&dev->interface->dev,
2875 "Error while settings PASORI config");
2879 pn533_pasori_fw_reset(dev);
2887 static int pn533_probe(struct usb_interface *interface,
2888 const struct usb_device_id *id)
2890 struct pn533_fw_version fw_ver;
2892 struct usb_host_interface *iface_desc;
2893 struct usb_endpoint_descriptor *endpoint;
2894 int in_endpoint = 0;
2895 int out_endpoint = 0;
2900 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2904 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2905 dev->interface = interface;
2906 mutex_init(&dev->cmd_lock);
2908 iface_desc = interface->cur_altsetting;
2909 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2910 endpoint = &iface_desc->endpoint[i].desc;
2912 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2913 in_endpoint = endpoint->bEndpointAddress;
2915 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2916 out_endpoint = endpoint->bEndpointAddress;
2919 if (!in_endpoint || !out_endpoint) {
2920 nfc_dev_err(&interface->dev,
2921 "Could not find bulk-in or bulk-out endpoint");
2926 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2927 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2929 if (!dev->in_urb || !dev->out_urb)
2932 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2933 usb_rcvbulkpipe(dev->udev, in_endpoint),
2934 NULL, 0, NULL, dev);
2935 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2936 usb_sndbulkpipe(dev->udev, out_endpoint),
2937 NULL, 0, pn533_send_complete, dev);
2939 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2940 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2941 INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
2942 INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
2943 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2944 INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
2945 INIT_WORK(&dev->rf_work, pn533_wq_rf);
2946 dev->wq = alloc_ordered_workqueue("pn533", 0);
2947 if (dev->wq == NULL)
2950 init_timer(&dev->listen_timer);
2951 dev->listen_timer.data = (unsigned long) dev;
2952 dev->listen_timer.function = pn533_listen_mode_timer;
2954 skb_queue_head_init(&dev->resp_q);
2955 skb_queue_head_init(&dev->fragment_skb);
2957 INIT_LIST_HEAD(&dev->cmd_queue);
2959 usb_set_intfdata(interface, dev);
2961 dev->ops = &pn533_std_frame_ops;
2963 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
2964 dev->device_type = id->driver_info;
2965 switch (dev->device_type) {
2966 case PN533_DEVICE_STD:
2967 protocols = PN533_ALL_PROTOCOLS;
2970 case PN533_DEVICE_PASORI:
2971 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2974 case PN533_DEVICE_ACR122U:
2975 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2976 dev->ops = &pn533_acr122_frame_ops;
2977 dev->protocol_type = PN533_PROTO_REQ_RESP,
2979 rc = pn533_acr122_poweron_rdr(dev);
2981 nfc_dev_err(&dev->interface->dev,
2982 "Couldn't poweron the reader (error %d)",
2989 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2995 memset(&fw_ver, 0, sizeof(fw_ver));
2996 rc = pn533_get_firmware_version(dev, &fw_ver);
3000 nfc_dev_info(&dev->interface->dev,
3001 "NXP PN5%02X firmware ver %d.%d now attached",
3002 fw_ver.ic, fw_ver.ver, fw_ver.rev);
3005 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
3006 dev->ops->tx_header_len +
3007 PN533_CMD_DATAEXCH_HEAD_LEN,
3008 dev->ops->tx_tail_len);
3009 if (!dev->nfc_dev) {
3014 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
3015 nfc_set_drvdata(dev->nfc_dev, dev);
3017 rc = nfc_register_device(dev->nfc_dev);
3021 rc = pn533_setup(dev);
3023 goto unregister_nfc_dev;
3028 nfc_unregister_device(dev->nfc_dev);
3031 nfc_free_device(dev->nfc_dev);
3034 destroy_workqueue(dev->wq);
3036 usb_free_urb(dev->in_urb);
3037 usb_free_urb(dev->out_urb);
3038 usb_put_dev(dev->udev);
3043 static void pn533_disconnect(struct usb_interface *interface)
3046 struct pn533_cmd *cmd, *n;
3048 dev = usb_get_intfdata(interface);
3049 usb_set_intfdata(interface, NULL);
3051 nfc_unregister_device(dev->nfc_dev);
3052 nfc_free_device(dev->nfc_dev);
3054 usb_kill_urb(dev->in_urb);
3055 usb_kill_urb(dev->out_urb);
3057 flush_delayed_work(&dev->poll_work);
3058 destroy_workqueue(dev->wq);
3060 skb_queue_purge(&dev->resp_q);
3062 del_timer(&dev->listen_timer);
3064 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
3065 list_del(&cmd->queue);
3069 usb_free_urb(dev->in_urb);
3070 usb_free_urb(dev->out_urb);
3073 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
3076 static struct usb_driver pn533_driver = {
3078 .probe = pn533_probe,
3079 .disconnect = pn533_disconnect,
3080 .id_table = pn533_table,
3083 module_usb_driver(pn533_driver);
3085 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
3086 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
3087 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
3088 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
3089 MODULE_VERSION(VERSION);
3090 MODULE_LICENSE("GPL");