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, see <http://www.gnu.org/licenses/>.
19 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/usb.h>
24 #include <linux/nfc.h>
25 #include <linux/netdevice.h>
26 #include <net/nfc/nfc.h>
30 #define PN533_VENDOR_ID 0x4CC
31 #define PN533_PRODUCT_ID 0x2533
33 #define SCM_VENDOR_ID 0x4E6
34 #define SCL3711_PRODUCT_ID 0x5591
36 #define SONY_VENDOR_ID 0x054c
37 #define PASORI_PRODUCT_ID 0x02e1
39 #define ACS_VENDOR_ID 0x072f
40 #define ACR122U_PRODUCT_ID 0x2200
42 #define PN533_DEVICE_STD 0x1
43 #define PN533_DEVICE_PASORI 0x2
44 #define PN533_DEVICE_ACR122U 0x3
46 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
47 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
48 NFC_PROTO_NFC_DEP_MASK |\
49 NFC_PROTO_ISO14443_B_MASK)
51 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
52 NFC_PROTO_MIFARE_MASK | \
53 NFC_PROTO_FELICA_MASK | \
54 NFC_PROTO_ISO14443_MASK | \
55 NFC_PROTO_NFC_DEP_MASK)
57 static const struct usb_device_id pn533_table[] = {
58 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
59 .idVendor = PN533_VENDOR_ID,
60 .idProduct = PN533_PRODUCT_ID,
61 .driver_info = PN533_DEVICE_STD,
63 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
64 .idVendor = SCM_VENDOR_ID,
65 .idProduct = SCL3711_PRODUCT_ID,
66 .driver_info = PN533_DEVICE_STD,
68 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
69 .idVendor = SONY_VENDOR_ID,
70 .idProduct = PASORI_PRODUCT_ID,
71 .driver_info = PN533_DEVICE_PASORI,
73 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
74 .idVendor = ACS_VENDOR_ID,
75 .idProduct = ACR122U_PRODUCT_ID,
76 .driver_info = PN533_DEVICE_ACR122U,
80 MODULE_DEVICE_TABLE(usb, pn533_table);
82 /* How much time we spend listening for initiators */
83 #define PN533_LISTEN_TIME 2
84 /* Delay between each poll frame (ms) */
85 #define PN533_POLL_INTERVAL 10
87 /* Standard pn533 frame definitions (standard and extended)*/
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*/
92 #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
93 + 2) /* data[0] TFI, data[1] CC */
95 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
96 #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
99 * Max extended frame payload len, excluding TFI and CC
100 * which are already in PN533_FRAME_HEADER_LEN.
102 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
104 #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
106 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
107 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
108 /* Half start code (3), LEN (4) should be 0xffff for extended frame */
109 #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
110 && (hdr)->datalen_checksum == 0xFF)
111 #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
114 #define PN533_STD_FRAME_SOF 0x00FF
116 /* standard frame identifier: in/out/error */
117 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
118 #define PN533_STD_FRAME_DIR_OUT 0xD4
119 #define PN533_STD_FRAME_DIR_IN 0xD5
121 /* ACS ACR122 pn533 frame definitions */
122 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
124 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
125 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
127 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
128 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
130 /* CCID messages types */
131 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
132 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
134 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
137 #define PN533_FRAME_CMD(f) (f->data[1])
139 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
140 #define PN533_CMD_RF_CONFIGURATION 0x32
141 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
142 #define PN533_CMD_IN_COMM_THRU 0x42
143 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
144 #define PN533_CMD_IN_ATR 0x50
145 #define PN533_CMD_IN_RELEASE 0x52
146 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
148 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
149 #define PN533_CMD_TG_GET_DATA 0x86
150 #define PN533_CMD_TG_SET_DATA 0x8e
151 #define PN533_CMD_TG_SET_META_DATA 0x94
152 #define PN533_CMD_UNDEF 0xff
154 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
156 /* PN533 Return codes */
157 #define PN533_CMD_RET_MASK 0x3F
158 #define PN533_CMD_MI_MASK 0x40
159 #define PN533_CMD_RET_SUCCESS 0x00
163 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
164 struct sk_buff *resp);
166 /* structs for pn533 commands */
168 /* PN533_CMD_GET_FIRMWARE_VERSION */
169 struct pn533_fw_version {
176 /* PN533_CMD_RF_CONFIGURATION */
177 #define PN533_CFGITEM_RF_FIELD 0x01
178 #define PN533_CFGITEM_TIMING 0x02
179 #define PN533_CFGITEM_MAX_RETRIES 0x05
180 #define PN533_CFGITEM_PASORI 0x82
182 #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
183 #define PN533_CFGITEM_RF_FIELD_ON 0x1
184 #define PN533_CFGITEM_RF_FIELD_OFF 0x0
186 #define PN533_CONFIG_TIMING_102 0xb
187 #define PN533_CONFIG_TIMING_204 0xc
188 #define PN533_CONFIG_TIMING_409 0xd
189 #define PN533_CONFIG_TIMING_819 0xe
191 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
192 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
194 struct pn533_config_max_retries {
197 u8 mx_rty_passive_act;
200 struct pn533_config_timing {
206 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
208 /* felica commands opcode */
209 #define PN533_FELICA_OPC_SENSF_REQ 0
210 #define PN533_FELICA_OPC_SENSF_RES 1
211 /* felica SENSF_REQ parameters */
212 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
213 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
214 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
215 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
217 /* type B initiator_data values */
218 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
219 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
220 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
222 union pn533_cmd_poll_initdata {
235 /* Poll modulations */
237 PN533_POLL_MOD_106KBPS_A,
238 PN533_POLL_MOD_212KBPS_FELICA,
239 PN533_POLL_MOD_424KBPS_FELICA,
240 PN533_POLL_MOD_106KBPS_JEWEL,
241 PN533_POLL_MOD_847KBPS_B,
244 __PN533_POLL_MOD_AFTER_LAST,
246 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
248 struct pn533_poll_modulations {
252 union pn533_cmd_poll_initdata initiator_data;
257 static const struct pn533_poll_modulations poll_mod[] = {
258 [PN533_POLL_MOD_106KBPS_A] = {
265 [PN533_POLL_MOD_212KBPS_FELICA] = {
269 .initiator_data.felica = {
270 .opcode = PN533_FELICA_OPC_SENSF_REQ,
271 .sc = PN533_FELICA_SENSF_SC_ALL,
272 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
278 [PN533_POLL_MOD_424KBPS_FELICA] = {
282 .initiator_data.felica = {
283 .opcode = PN533_FELICA_OPC_SENSF_REQ,
284 .sc = PN533_FELICA_SENSF_SC_ALL,
285 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
291 [PN533_POLL_MOD_106KBPS_JEWEL] = {
298 [PN533_POLL_MOD_847KBPS_B] = {
302 .initiator_data.type_b = {
303 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
305 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
310 [PN533_LISTEN_MOD] = {
315 /* PN533_CMD_IN_ATR */
317 struct pn533_cmd_activate_response {
329 struct pn533_cmd_jump_dep_response {
343 /* PN533_TG_INIT_AS_TARGET */
344 #define PN533_INIT_TARGET_PASSIVE 0x1
345 #define PN533_INIT_TARGET_DEP 0x2
347 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
348 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
349 #define PN533_INIT_TARGET_RESP_DEP 0x4
351 enum pn533_protocol_type {
352 PN533_PROTO_REQ_ACK_RESP = 0,
357 struct usb_device *udev;
358 struct usb_interface *interface;
359 struct nfc_dev *nfc_dev;
361 enum pn533_protocol_type protocol_type;
366 struct sk_buff_head resp_q;
367 struct sk_buff_head fragment_skb;
369 struct workqueue_struct *wq;
370 struct work_struct cmd_work;
371 struct work_struct cmd_complete_work;
372 struct delayed_work poll_work;
373 struct work_struct mi_rx_work;
374 struct work_struct mi_tx_work;
375 struct work_struct mi_tm_rx_work;
376 struct work_struct mi_tm_tx_work;
377 struct work_struct tg_work;
378 struct work_struct rf_work;
380 struct list_head cmd_queue;
381 struct pn533_cmd *cmd;
383 struct mutex cmd_lock; /* protects cmd queue */
385 void *cmd_complete_mi_arg;
386 void *cmd_complete_dep_arg;
388 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
393 u32 listen_protocols;
394 struct timer_list listen_timer;
400 u8 tgt_available_prots;
404 struct pn533_frame_ops *ops;
408 struct list_head queue;
412 struct sk_buff *resp;
414 pn533_send_async_complete_t complete_cb;
415 void *complete_cb_context;
418 struct pn533_std_frame {
426 struct pn533_ext_frame { /* Extended Information frame */
429 __be16 eif_flag; /* fixed to 0xFFFF */
435 struct pn533_frame_ops {
436 void (*tx_frame_init)(void *frame, u8 cmd_code);
437 void (*tx_frame_finish)(void *frame);
438 void (*tx_update_payload_len)(void *frame, int len);
442 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
443 int (*rx_frame_size)(void *frame);
448 u8 (*get_cmd_code)(void *frame);
451 struct pn533_acr122_ccid_hdr {
456 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
457 byte for reposnse msg */
458 u8 data[]; /* payload */
461 struct pn533_acr122_apdu_hdr {
468 struct pn533_acr122_tx_frame {
469 struct pn533_acr122_ccid_hdr ccid;
470 struct pn533_acr122_apdu_hdr apdu;
472 u8 data[]; /* pn533 frame: TFI ... */
475 struct pn533_acr122_rx_frame {
476 struct pn533_acr122_ccid_hdr ccid;
477 u8 data[]; /* pn533 frame : TFI ... */
480 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
482 struct pn533_acr122_tx_frame *frame = _frame;
484 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
485 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
487 frame->ccid.slot = 0;
489 frame->ccid.params[0] = 0;
490 frame->ccid.params[1] = 0;
491 frame->ccid.params[2] = 0;
493 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
494 frame->data[1] = cmd_code;
495 frame->datalen = 2; /* data[0] + data[1] */
497 frame->apdu.class = 0xFF;
503 static void pn533_acr122_tx_frame_finish(void *_frame)
505 struct pn533_acr122_tx_frame *frame = _frame;
507 frame->ccid.datalen += frame->datalen;
510 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
512 struct pn533_acr122_tx_frame *frame = _frame;
514 frame->datalen += len;
517 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
519 struct pn533_acr122_rx_frame *frame = _frame;
521 if (frame->ccid.type != 0x83)
524 if (!frame->ccid.datalen)
527 if (frame->data[frame->ccid.datalen - 2] == 0x63)
533 static int pn533_acr122_rx_frame_size(void *frame)
535 struct pn533_acr122_rx_frame *f = frame;
537 /* f->ccid.datalen already includes tail length */
538 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
541 static u8 pn533_acr122_get_cmd_code(void *frame)
543 struct pn533_acr122_rx_frame *f = frame;
545 return PN533_FRAME_CMD(f);
548 static struct pn533_frame_ops pn533_acr122_frame_ops = {
549 .tx_frame_init = pn533_acr122_tx_frame_init,
550 .tx_frame_finish = pn533_acr122_tx_frame_finish,
551 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
552 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
553 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
555 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
556 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
557 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
558 .rx_frame_size = pn533_acr122_rx_frame_size,
560 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
561 .get_cmd_code = pn533_acr122_get_cmd_code,
564 /* The rule: value(high byte) + value(low byte) + checksum = 0 */
565 static inline u8 pn533_ext_checksum(u16 value)
567 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
570 /* The rule: value + checksum = 0 */
571 static inline u8 pn533_std_checksum(u8 value)
576 /* The rule: sum(data elements) + checksum = 0 */
577 static u8 pn533_std_data_checksum(u8 *data, int datalen)
582 for (i = 0; i < datalen; i++)
585 return pn533_std_checksum(sum);
588 static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
590 struct pn533_std_frame *frame = _frame;
593 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
594 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
595 PN533_FRAME_CMD(frame) = cmd_code;
599 static void pn533_std_tx_frame_finish(void *_frame)
601 struct pn533_std_frame *frame = _frame;
603 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
605 PN533_STD_FRAME_CHECKSUM(frame) =
606 pn533_std_data_checksum(frame->data, frame->datalen);
608 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
611 static void pn533_std_tx_update_payload_len(void *_frame, int len)
613 struct pn533_std_frame *frame = _frame;
615 frame->datalen += len;
618 static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
621 struct pn533_std_frame *stdf = _frame;
623 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
626 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
627 /* Standard frame code */
628 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
630 checksum = pn533_std_checksum(stdf->datalen);
631 if (checksum != stdf->datalen_checksum)
634 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
635 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
639 struct pn533_ext_frame *eif = _frame;
641 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
643 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
644 if (checksum != eif->datalen_checksum)
647 /* check data checksum */
648 checksum = pn533_std_data_checksum(eif->data,
649 be16_to_cpu(eif->datalen));
650 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
657 static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
659 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
662 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
668 static inline int pn533_std_rx_frame_size(void *frame)
670 struct pn533_std_frame *f = frame;
672 /* check for Extended Information frame */
673 if (PN533_STD_IS_EXTENDED(f)) {
674 struct pn533_ext_frame *eif = frame;
676 return sizeof(struct pn533_ext_frame)
677 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
680 return sizeof(struct pn533_std_frame) + f->datalen +
681 PN533_STD_FRAME_TAIL_LEN;
684 static u8 pn533_std_get_cmd_code(void *frame)
686 struct pn533_std_frame *f = frame;
687 struct pn533_ext_frame *eif = frame;
689 if (PN533_STD_IS_EXTENDED(f))
690 return PN533_FRAME_CMD(eif);
692 return PN533_FRAME_CMD(f);
695 static struct pn533_frame_ops pn533_std_frame_ops = {
696 .tx_frame_init = pn533_std_tx_frame_init,
697 .tx_frame_finish = pn533_std_tx_frame_finish,
698 .tx_update_payload_len = pn533_std_tx_update_payload_len,
699 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
700 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
702 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
703 .rx_frame_size = pn533_std_rx_frame_size,
704 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
705 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
707 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
708 .get_cmd_code = pn533_std_get_cmd_code,
711 static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
713 return (dev->ops->get_cmd_code(frame) ==
714 PN533_CMD_RESPONSE(dev->cmd->code));
717 static void pn533_recv_response(struct urb *urb)
719 struct pn533 *dev = urb->context;
720 struct pn533_cmd *cmd = dev->cmd;
723 cmd->status = urb->status;
725 switch (urb->status) {
730 dev_dbg(&dev->interface->dev,
731 "The urb has been canceled (status %d)\n",
736 nfc_err(&dev->interface->dev,
737 "Urb failure (status %d)\n", urb->status);
741 in_frame = dev->in_urb->transfer_buffer;
743 dev_dbg(&dev->interface->dev, "Received a frame\n");
744 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
745 dev->ops->rx_frame_size(in_frame), false);
747 if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
748 nfc_err(&dev->interface->dev, "Received an invalid frame\n");
753 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
754 nfc_err(&dev->interface->dev,
755 "It it not the response to the last command\n");
761 queue_work(dev->wq, &dev->cmd_complete_work);
764 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
766 dev->in_urb->complete = pn533_recv_response;
768 return usb_submit_urb(dev->in_urb, flags);
771 static void pn533_recv_ack(struct urb *urb)
773 struct pn533 *dev = urb->context;
774 struct pn533_cmd *cmd = dev->cmd;
775 struct pn533_std_frame *in_frame;
778 cmd->status = urb->status;
780 switch (urb->status) {
785 dev_dbg(&dev->interface->dev,
786 "The urb has been stopped (status %d)\n",
791 nfc_err(&dev->interface->dev,
792 "Urb failure (status %d)\n", urb->status);
796 in_frame = dev->in_urb->transfer_buffer;
798 if (!pn533_std_rx_frame_is_ack(in_frame)) {
799 nfc_err(&dev->interface->dev, "Received an invalid ack\n");
804 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
806 nfc_err(&dev->interface->dev,
807 "usb_submit_urb failed with result %d\n", rc);
815 queue_work(dev->wq, &dev->cmd_complete_work);
818 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
820 dev->in_urb->complete = pn533_recv_ack;
822 return usb_submit_urb(dev->in_urb, flags);
825 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
827 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
828 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
831 dev->out_urb->transfer_buffer = ack;
832 dev->out_urb->transfer_buffer_length = sizeof(ack);
833 rc = usb_submit_urb(dev->out_urb, flags);
838 static int __pn533_send_frame_async(struct pn533 *dev,
845 dev->out_urb->transfer_buffer = out->data;
846 dev->out_urb->transfer_buffer_length = out->len;
848 dev->in_urb->transfer_buffer = in->data;
849 dev->in_urb->transfer_buffer_length = in_len;
851 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
852 out->data, out->len, false);
854 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
858 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
859 /* request for response for sent packet directly */
860 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
863 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
864 /* request for ACK if that's the case */
865 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
873 usb_unlink_urb(dev->out_urb);
877 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
880 /* payload is already there, just update datalen */
881 int payload_len = skb->len;
882 struct pn533_frame_ops *ops = dev->ops;
885 skb_push(skb, ops->tx_header_len);
886 skb_put(skb, ops->tx_tail_len);
888 ops->tx_frame_init(skb->data, cmd_code);
889 ops->tx_update_payload_len(skb->data, payload_len);
890 ops->tx_frame_finish(skb->data);
893 static int pn533_send_async_complete(struct pn533 *dev)
895 struct pn533_cmd *cmd = dev->cmd;
896 int status = cmd->status;
898 struct sk_buff *req = cmd->req;
899 struct sk_buff *resp = cmd->resp;
906 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
912 skb_put(resp, dev->ops->rx_frame_size(resp->data));
913 skb_pull(resp, dev->ops->rx_header_len);
914 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
916 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
924 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
925 struct sk_buff *req, struct sk_buff *resp,
927 pn533_send_async_complete_t complete_cb,
928 void *complete_cb_context)
930 struct pn533_cmd *cmd;
933 dev_dbg(&dev->interface->dev, "Sending command 0x%x\n", cmd_code);
935 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
939 cmd->code = cmd_code;
942 cmd->resp_len = resp_len;
943 cmd->complete_cb = complete_cb;
944 cmd->complete_cb_context = complete_cb_context;
946 pn533_build_cmd_frame(dev, cmd_code, req);
948 mutex_lock(&dev->cmd_lock);
950 if (!dev->cmd_pending) {
951 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
955 dev->cmd_pending = 1;
960 dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x\n",
963 INIT_LIST_HEAD(&cmd->queue);
964 list_add_tail(&cmd->queue, &dev->cmd_queue);
971 mutex_unlock(&dev->cmd_lock);
975 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
977 pn533_send_async_complete_t complete_cb,
978 void *complete_cb_context)
980 struct sk_buff *resp;
982 int resp_len = dev->ops->rx_header_len +
983 dev->ops->max_payload_len +
984 dev->ops->rx_tail_len;
986 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
990 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
991 complete_cb_context);
998 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
1000 pn533_send_async_complete_t complete_cb,
1001 void *complete_cb_context)
1003 struct sk_buff *resp;
1005 int resp_len = dev->ops->rx_header_len +
1006 dev->ops->max_payload_len +
1007 dev->ops->rx_tail_len;
1009 resp = alloc_skb(resp_len, GFP_KERNEL);
1013 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
1014 complete_cb_context);
1016 dev_kfree_skb(resp);
1022 * pn533_send_cmd_direct_async
1024 * The function sends a piority cmd directly to the chip omiting the cmd
1025 * queue. It's intended to be used by chaining mechanism of received responses
1026 * where the host has to request every single chunk of data before scheduling
1027 * next cmd from the queue.
1029 static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
1030 struct sk_buff *req,
1031 pn533_send_async_complete_t complete_cb,
1032 void *complete_cb_context)
1034 struct sk_buff *resp;
1035 struct pn533_cmd *cmd;
1037 int resp_len = dev->ops->rx_header_len +
1038 dev->ops->max_payload_len +
1039 dev->ops->rx_tail_len;
1041 resp = alloc_skb(resp_len, GFP_KERNEL);
1045 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1047 dev_kfree_skb(resp);
1051 cmd->code = cmd_code;
1054 cmd->resp_len = resp_len;
1055 cmd->complete_cb = complete_cb;
1056 cmd->complete_cb_context = complete_cb_context;
1058 pn533_build_cmd_frame(dev, cmd_code, req);
1060 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
1062 dev_kfree_skb(resp);
1071 static void pn533_wq_cmd_complete(struct work_struct *work)
1073 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1076 rc = pn533_send_async_complete(dev);
1077 if (rc != -EINPROGRESS)
1078 queue_work(dev->wq, &dev->cmd_work);
1081 static void pn533_wq_cmd(struct work_struct *work)
1083 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1084 struct pn533_cmd *cmd;
1087 mutex_lock(&dev->cmd_lock);
1089 if (list_empty(&dev->cmd_queue)) {
1090 dev->cmd_pending = 0;
1091 mutex_unlock(&dev->cmd_lock);
1095 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1097 list_del(&cmd->queue);
1099 mutex_unlock(&dev->cmd_lock);
1101 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
1103 dev_kfree_skb(cmd->req);
1104 dev_kfree_skb(cmd->resp);
1112 struct pn533_sync_cmd_response {
1113 struct sk_buff *resp;
1114 struct completion done;
1117 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1118 struct sk_buff *resp)
1120 struct pn533_sync_cmd_response *arg = _arg;
1123 complete(&arg->done);
1128 /* pn533_send_cmd_sync
1130 * Please note the req parameter is freed inside the function to
1131 * limit a number of return value interpretations by the caller.
1133 * 1. negative in case of error during TX path -> req should be freed
1135 * 2. negative in case of error during RX path -> req should not be freed
1136 * as it's been already freed at the begining of RX path by
1137 * async_complete_cb.
1139 * 3. valid pointer in case of succesfult RX path
1141 * A caller has to check a return value with IS_ERR macro. If the test pass,
1142 * the returned pointer is valid.
1145 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1146 struct sk_buff *req)
1149 struct pn533_sync_cmd_response arg;
1151 init_completion(&arg.done);
1153 rc = pn533_send_cmd_async(dev, cmd_code, req,
1154 pn533_send_sync_complete, &arg);
1160 wait_for_completion(&arg.done);
1165 static void pn533_send_complete(struct urb *urb)
1167 struct pn533 *dev = urb->context;
1169 switch (urb->status) {
1171 break; /* success */
1174 dev_dbg(&dev->interface->dev,
1175 "The urb has been stopped (status %d)\n",
1180 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
1185 static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1187 /* ACR122U does not support any command which aborts last
1188 * issued command i.e. as ACK for standard PN533. Additionally,
1189 * it behaves stange, sending broken or incorrect responses,
1190 * when we cancel urb before the chip will send response.
1192 if (dev->device_type == PN533_DEVICE_ACR122U)
1195 /* An ack will cancel the last issued command */
1196 pn533_send_ack(dev, flags);
1198 /* cancel the urb request */
1199 usb_kill_urb(dev->in_urb);
1202 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
1204 struct sk_buff *skb;
1206 skb = alloc_skb(dev->ops->tx_header_len +
1208 dev->ops->tx_tail_len, GFP_KERNEL);
1211 skb_reserve(skb, dev->ops->tx_header_len);
1216 struct pn533_target_type_a {
1224 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1225 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1226 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1228 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1229 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1231 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1232 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1234 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1235 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1236 #define PN533_TYPE_A_SEL_PROT_DEP 2
1237 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1239 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1240 int target_data_len)
1245 if (target_data_len < sizeof(struct pn533_target_type_a))
1248 /* The lenght check of nfcid[] and ats[] are not being performed because
1249 the values are not being used */
1251 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1252 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1253 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1255 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1256 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1257 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1258 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1261 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1262 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1268 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1271 struct pn533_target_type_a *tgt_type_a;
1273 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1275 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1278 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1279 case PN533_TYPE_A_SEL_PROT_MIFARE:
1280 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1282 case PN533_TYPE_A_SEL_PROT_ISO14443:
1283 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1285 case PN533_TYPE_A_SEL_PROT_DEP:
1286 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1288 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1289 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1290 NFC_PROTO_NFC_DEP_MASK;
1294 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1295 nfc_tgt->sel_res = tgt_type_a->sel_res;
1296 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1297 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1302 struct pn533_target_felica {
1305 u8 nfcid2[NFC_NFCID2_MAXSIZE];
1311 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1312 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1314 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1315 int target_data_len)
1317 if (target_data_len < sizeof(struct pn533_target_felica))
1320 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1326 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1329 struct pn533_target_felica *tgt_felica;
1331 tgt_felica = (struct pn533_target_felica *)tgt_data;
1333 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1336 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1337 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1338 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1340 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1342 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1343 nfc_tgt->sensf_res_len = 9;
1345 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
1346 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
1351 struct pn533_target_jewel {
1356 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1357 int target_data_len)
1362 if (target_data_len < sizeof(struct pn533_target_jewel))
1365 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1366 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1367 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1369 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1370 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1371 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1372 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1378 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1381 struct pn533_target_jewel *tgt_jewel;
1383 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1385 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1388 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1389 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1390 nfc_tgt->nfcid1_len = 4;
1391 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1396 struct pn533_type_b_prot_info {
1402 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1403 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1404 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1406 struct pn533_type_b_sens_res {
1410 struct pn533_type_b_prot_info prot_info;
1413 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1415 struct pn533_target_type_b {
1416 struct pn533_type_b_sens_res sensb_res;
1421 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1422 int target_data_len)
1424 if (target_data_len < sizeof(struct pn533_target_type_b))
1427 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1430 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1431 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1437 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1440 struct pn533_target_type_b *tgt_type_b;
1442 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1444 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1447 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1452 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1455 struct nfc_target nfc_tgt;
1458 dev_dbg(&dev->interface->dev, "%s: modulation=%d\n",
1459 __func__, dev->poll_mod_curr);
1464 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1466 switch (dev->poll_mod_curr) {
1467 case PN533_POLL_MOD_106KBPS_A:
1468 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1470 case PN533_POLL_MOD_212KBPS_FELICA:
1471 case PN533_POLL_MOD_424KBPS_FELICA:
1472 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1474 case PN533_POLL_MOD_106KBPS_JEWEL:
1475 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1477 case PN533_POLL_MOD_847KBPS_B:
1478 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1481 nfc_err(&dev->interface->dev,
1482 "Unknown current poll modulation\n");
1489 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1490 dev_dbg(&dev->interface->dev,
1491 "The Tg found doesn't have the desired protocol\n");
1495 dev_dbg(&dev->interface->dev,
1496 "Target found - supported protocols: 0x%x\n",
1497 nfc_tgt.supported_protocols);
1499 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1501 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1506 static inline void pn533_poll_next_mod(struct pn533 *dev)
1508 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1511 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1513 dev->poll_mod_count = 0;
1516 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1518 dev->poll_mod_active[dev->poll_mod_count] =
1519 (struct pn533_poll_modulations *)&poll_mod[mod_index];
1520 dev->poll_mod_count++;
1523 static void pn533_poll_create_mod_list(struct pn533 *dev,
1524 u32 im_protocols, u32 tm_protocols)
1526 pn533_poll_reset_mod_list(dev);
1528 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1529 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1530 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1531 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1533 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1534 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1535 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1536 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1539 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1540 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1542 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1543 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1546 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1549 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1551 u8 nbtg, tg, *tgdata;
1554 /* Toggle the DEP polling */
1557 nbtg = resp->data[0];
1559 tgdata = &resp->data[2];
1560 tgdata_len = resp->len - 2; /* nbtg + tg */
1563 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1565 /* We must stop the poll after a valid target found */
1567 pn533_poll_reset_mod_list(dev);
1575 static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1577 struct sk_buff *skb;
1578 u8 *felica, *nfcid3, *gb;
1580 u8 *gbytes = dev->gb;
1581 size_t gbytes_len = dev->gb_len;
1583 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1584 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1585 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1586 0xff, 0xff}; /* System code */
1588 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1590 0x40}; /* SEL_RES for DEP */
1592 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1593 felica (18), nfcid3 (10), gb_len (1) */
1597 skb = pn533_alloc_skb(dev, skb_len);
1601 /* DEP support only */
1602 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1605 memcpy(skb_put(skb, 6), mifare_params, 6);
1608 felica = skb_put(skb, 18);
1609 memcpy(felica, felica_params, 18);
1610 get_random_bytes(felica + 2, 6);
1613 nfcid3 = skb_put(skb, 10);
1614 memset(nfcid3, 0, 10);
1615 memcpy(nfcid3, felica, 8);
1618 *skb_put(skb, 1) = gbytes_len;
1620 gb = skb_put(skb, gbytes_len);
1621 memcpy(gb, gbytes, gbytes_len);
1624 *skb_put(skb, 1) = 0;
1629 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1630 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1631 static void pn533_wq_tm_mi_recv(struct work_struct *work);
1632 static struct sk_buff *pn533_build_response(struct pn533 *dev);
1634 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1635 struct sk_buff *resp)
1637 struct sk_buff *skb;
1641 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1644 skb_queue_purge(&dev->resp_q);
1645 return PTR_ERR(resp);
1648 status = resp->data[0];
1650 ret = status & PN533_CMD_RET_MASK;
1651 mi = status & PN533_CMD_MI_MASK;
1653 skb_pull(resp, sizeof(status));
1655 if (ret != PN533_CMD_RET_SUCCESS) {
1660 skb_queue_tail(&dev->resp_q, resp);
1663 queue_work(dev->wq, &dev->mi_tm_rx_work);
1664 return -EINPROGRESS;
1667 skb = pn533_build_response(dev);
1673 return nfc_tm_data_received(dev->nfc_dev, skb);
1676 nfc_tm_deactivated(dev->nfc_dev);
1678 skb_queue_purge(&dev->resp_q);
1679 dev_kfree_skb(resp);
1684 static void pn533_wq_tm_mi_recv(struct work_struct *work)
1686 struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1687 struct sk_buff *skb;
1690 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1692 skb = pn533_alloc_skb(dev, 0);
1696 rc = pn533_send_cmd_direct_async(dev,
1697 PN533_CMD_TG_GET_DATA,
1699 pn533_tm_get_data_complete,
1708 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1709 struct sk_buff *resp);
1710 static void pn533_wq_tm_mi_send(struct work_struct *work)
1712 struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1713 struct sk_buff *skb;
1716 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1718 /* Grab the first skb in the queue */
1719 skb = skb_dequeue(&dev->fragment_skb);
1720 if (skb == NULL) { /* No more data */
1721 /* Reset the queue for future use */
1722 skb_queue_head_init(&dev->fragment_skb);
1726 /* last entry - remove MI bit */
1727 if (skb_queue_len(&dev->fragment_skb) == 0) {
1728 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1729 skb, pn533_tm_send_complete, NULL);
1731 rc = pn533_send_cmd_direct_async(dev,
1732 PN533_CMD_TG_SET_META_DATA,
1733 skb, pn533_tm_send_complete, NULL);
1735 if (rc == 0) /* success */
1738 dev_err(&dev->interface->dev,
1739 "Error %d when trying to perform set meta data_exchange", rc);
1744 pn533_send_ack(dev, GFP_KERNEL);
1745 queue_work(dev->wq, &dev->cmd_work);
1748 static void pn533_wq_tg_get_data(struct work_struct *work)
1750 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1751 struct sk_buff *skb;
1754 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1756 skb = pn533_alloc_skb(dev, 0);
1760 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1761 pn533_tm_get_data_complete, NULL);
1769 #define ATR_REQ_GB_OFFSET 17
1770 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1772 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1776 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1778 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1781 mode = resp->data[0];
1782 cmd = &resp->data[1];
1784 dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1787 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1788 PN533_INIT_TARGET_RESP_ACTIVE)
1789 comm_mode = NFC_COMM_ACTIVE;
1791 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1794 gb = cmd + ATR_REQ_GB_OFFSET;
1795 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1797 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1798 comm_mode, gb, gb_len);
1800 nfc_err(&dev->interface->dev,
1801 "Error when signaling target activation\n");
1806 queue_work(dev->wq, &dev->tg_work);
1811 static void pn533_listen_mode_timer(unsigned long data)
1813 struct pn533 *dev = (struct pn533 *)data;
1815 dev_dbg(&dev->interface->dev, "Listen mode timeout\n");
1817 dev->cancel_listen = 1;
1819 pn533_poll_next_mod(dev);
1821 queue_delayed_work(dev->wq, &dev->poll_work,
1822 msecs_to_jiffies(PN533_POLL_INTERVAL));
1825 static int pn533_rf_complete(struct pn533 *dev, void *arg,
1826 struct sk_buff *resp)
1830 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1835 nfc_err(&dev->interface->dev, "RF setting error %d", rc);
1840 queue_delayed_work(dev->wq, &dev->poll_work,
1841 msecs_to_jiffies(PN533_POLL_INTERVAL));
1843 dev_kfree_skb(resp);
1847 static void pn533_wq_rf(struct work_struct *work)
1849 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1850 struct sk_buff *skb;
1853 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1855 skb = pn533_alloc_skb(dev, 2);
1859 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
1860 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
1862 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1863 pn533_rf_complete, NULL);
1866 nfc_err(&dev->interface->dev, "RF setting error %d\n", rc);
1872 static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1873 struct sk_buff *resp)
1875 struct pn533_cmd_jump_dep_response *rsp;
1876 struct nfc_target nfc_target;
1881 return PTR_ERR(resp);
1883 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1885 rc = rsp->status & PN533_CMD_RET_MASK;
1886 if (rc != PN533_CMD_RET_SUCCESS) {
1887 /* Not target found, turn radio off */
1888 queue_work(dev->wq, &dev->rf_work);
1890 dev_kfree_skb(resp);
1894 dev_dbg(&dev->interface->dev, "Creating new target");
1896 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1897 nfc_target.nfcid1_len = 10;
1898 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1899 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1903 dev->tgt_available_prots = 0;
1904 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1906 /* ATR_RES general bytes are located at offset 17 */
1907 target_gt_len = resp->len - 17;
1908 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1909 rsp->gt, target_gt_len);
1911 rc = nfc_dep_link_is_up(dev->nfc_dev,
1912 dev->nfc_dev->targets[0].idx,
1913 0, NFC_RF_INITIATOR);
1916 pn533_poll_reset_mod_list(dev);
1919 dev_kfree_skb(resp);
1923 #define PASSIVE_DATA_LEN 5
1924 static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1926 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1927 struct sk_buff *skb;
1929 u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1930 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1932 dev_dbg(&dev->interface->dev, "%s", __func__);
1935 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1937 if (!dev->gb || !dev->gb_len) {
1939 queue_work(dev->wq, &dev->rf_work);
1943 skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1944 skb_len += PASSIVE_DATA_LEN;
1947 skb_len += NFC_NFCID3_MAXSIZE;
1950 get_random_bytes(nfcid3 + 2, 6);
1952 skb = pn533_alloc_skb(dev, skb_len);
1956 *skb_put(skb, 1) = 0x01; /* Active */
1957 *skb_put(skb, 1) = 0x02; /* 424 kbps */
1959 next = skb_put(skb, 1); /* Next */
1962 /* Copy passive data */
1963 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
1966 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1967 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
1968 NFC_NFCID3_MAXSIZE);
1971 memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
1972 *next |= 4; /* We have some Gi */
1974 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1975 pn533_poll_dep_complete, NULL);
1983 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1984 struct sk_buff *resp)
1986 struct pn533_poll_modulations *cur_mod;
1989 dev_dbg(&dev->interface->dev, "%s\n", __func__);
1994 nfc_err(&dev->interface->dev, "%s Poll complete error %d\n",
1997 if (rc == -ENOENT) {
1998 if (dev->poll_mod_count != 0)
2002 } else if (rc < 0) {
2003 nfc_err(&dev->interface->dev,
2004 "Error %d when running poll\n", rc);
2009 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2011 if (cur_mod->len == 0) { /* Target mode */
2012 del_timer(&dev->listen_timer);
2013 rc = pn533_init_target_complete(dev, resp);
2017 /* Initiator mode */
2018 rc = pn533_start_poll_complete(dev, resp);
2022 if (!dev->poll_mod_count) {
2023 dev_dbg(&dev->interface->dev, "Polling has been stopped\n");
2027 pn533_poll_next_mod(dev);
2028 /* Not target found, turn radio off */
2029 queue_work(dev->wq, &dev->rf_work);
2032 dev_kfree_skb(resp);
2036 nfc_err(&dev->interface->dev, "Polling operation has been stopped\n");
2038 pn533_poll_reset_mod_list(dev);
2039 dev->poll_protocols = 0;
2043 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
2044 struct pn533_poll_modulations *mod)
2046 struct sk_buff *skb;
2048 skb = pn533_alloc_skb(dev, mod->len);
2052 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
2057 static int pn533_send_poll_frame(struct pn533 *dev)
2059 struct pn533_poll_modulations *mod;
2060 struct sk_buff *skb;
2064 mod = dev->poll_mod_active[dev->poll_mod_curr];
2066 dev_dbg(&dev->interface->dev, "%s mod len %d\n",
2067 __func__, mod->len);
2069 if (dev->poll_dep) {
2071 return pn533_poll_dep(dev->nfc_dev);
2074 if (mod->len == 0) { /* Listen mode */
2075 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
2076 skb = pn533_alloc_poll_tg_frame(dev);
2077 } else { /* Polling mode */
2078 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
2079 skb = pn533_alloc_poll_in_frame(dev, mod);
2083 nfc_err(&dev->interface->dev, "Failed to allocate skb\n");
2087 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
2091 nfc_err(&dev->interface->dev, "Polling loop error %d\n", rc);
2097 static void pn533_wq_poll(struct work_struct *work)
2099 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
2100 struct pn533_poll_modulations *cur_mod;
2103 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2105 dev_dbg(&dev->interface->dev,
2106 "%s cancel_listen %d modulation len %d\n",
2107 __func__, dev->cancel_listen, cur_mod->len);
2109 if (dev->cancel_listen == 1) {
2110 dev->cancel_listen = 0;
2111 pn533_abort_cmd(dev, GFP_ATOMIC);
2114 rc = pn533_send_poll_frame(dev);
2118 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
2119 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
2124 static int pn533_start_poll(struct nfc_dev *nfc_dev,
2125 u32 im_protocols, u32 tm_protocols)
2127 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2128 struct pn533_poll_modulations *cur_mod;
2132 dev_dbg(&dev->interface->dev,
2133 "%s: im protocols 0x%x tm protocols 0x%x\n",
2134 __func__, im_protocols, tm_protocols);
2136 if (dev->tgt_active_prot) {
2137 nfc_err(&dev->interface->dev,
2138 "Cannot poll with a target already activated\n");
2142 if (dev->tgt_mode) {
2143 nfc_err(&dev->interface->dev,
2144 "Cannot poll while already being activated\n");
2149 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
2150 if (dev->gb == NULL)
2154 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
2155 dev->poll_protocols = im_protocols;
2156 dev->listen_protocols = tm_protocols;
2158 /* Do not always start polling from the same modulation */
2159 get_random_bytes(&rand_mod, sizeof(rand_mod));
2160 rand_mod %= dev->poll_mod_count;
2161 dev->poll_mod_curr = rand_mod;
2163 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
2165 rc = pn533_send_poll_frame(dev);
2167 /* Start listen timer */
2168 if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
2169 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
2174 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
2176 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2178 del_timer(&dev->listen_timer);
2180 if (!dev->poll_mod_count) {
2181 dev_dbg(&dev->interface->dev,
2182 "Polling operation was not running\n");
2186 pn533_abort_cmd(dev, GFP_KERNEL);
2187 flush_delayed_work(&dev->poll_work);
2188 pn533_poll_reset_mod_list(dev);
2191 static int pn533_activate_target_nfcdep(struct pn533 *dev)
2193 struct pn533_cmd_activate_response *rsp;
2196 struct sk_buff *skb;
2197 struct sk_buff *resp;
2199 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2201 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
2205 *skb_put(skb, sizeof(u8)) = 1; /* TG */
2206 *skb_put(skb, sizeof(u8)) = 0; /* Next */
2208 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
2210 return PTR_ERR(resp);
2212 rsp = (struct pn533_cmd_activate_response *)resp->data;
2213 rc = rsp->status & PN533_CMD_RET_MASK;
2214 if (rc != PN533_CMD_RET_SUCCESS) {
2215 nfc_err(&dev->interface->dev,
2216 "Target activation failed (error 0x%x)\n", rc);
2217 dev_kfree_skb(resp);
2221 /* ATR_RES general bytes are located at offset 16 */
2222 gt_len = resp->len - 16;
2223 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
2225 dev_kfree_skb(resp);
2229 static int pn533_activate_target(struct nfc_dev *nfc_dev,
2230 struct nfc_target *target, u32 protocol)
2232 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2235 dev_dbg(&dev->interface->dev, "%s: protocol=%u\n", __func__, protocol);
2237 if (dev->poll_mod_count) {
2238 nfc_err(&dev->interface->dev,
2239 "Cannot activate while polling\n");
2243 if (dev->tgt_active_prot) {
2244 nfc_err(&dev->interface->dev,
2245 "There is already an active target\n");
2249 if (!dev->tgt_available_prots) {
2250 nfc_err(&dev->interface->dev,
2251 "There is no available target to activate\n");
2255 if (!(dev->tgt_available_prots & (1 << protocol))) {
2256 nfc_err(&dev->interface->dev,
2257 "Target doesn't support requested proto %u\n",
2262 if (protocol == NFC_PROTO_NFC_DEP) {
2263 rc = pn533_activate_target_nfcdep(dev);
2265 nfc_err(&dev->interface->dev,
2266 "Activating target with DEP failed %d\n", rc);
2271 dev->tgt_active_prot = protocol;
2272 dev->tgt_available_prots = 0;
2277 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
2278 struct nfc_target *target)
2280 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2281 struct sk_buff *skb;
2282 struct sk_buff *resp;
2285 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2287 if (!dev->tgt_active_prot) {
2288 nfc_err(&dev->interface->dev, "There is no active target\n");
2292 dev->tgt_active_prot = 0;
2293 skb_queue_purge(&dev->resp_q);
2295 skb = pn533_alloc_skb(dev, sizeof(u8));
2299 *skb_put(skb, 1) = 1; /* TG*/
2301 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
2305 rc = resp->data[0] & PN533_CMD_RET_MASK;
2306 if (rc != PN533_CMD_RET_SUCCESS)
2307 nfc_err(&dev->interface->dev,
2308 "Error 0x%x when releasing the target\n", rc);
2310 dev_kfree_skb(resp);
2315 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
2316 struct sk_buff *resp)
2318 struct pn533_cmd_jump_dep_response *rsp;
2321 u8 active = *(u8 *)arg;
2326 return PTR_ERR(resp);
2328 if (dev->tgt_available_prots &&
2329 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
2330 nfc_err(&dev->interface->dev,
2331 "The target does not support DEP\n");
2336 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
2338 rc = rsp->status & PN533_CMD_RET_MASK;
2339 if (rc != PN533_CMD_RET_SUCCESS) {
2340 nfc_err(&dev->interface->dev,
2341 "Bringing DEP link up failed (error 0x%x)\n", rc);
2345 if (!dev->tgt_available_prots) {
2346 struct nfc_target nfc_target;
2348 dev_dbg(&dev->interface->dev, "Creating new target\n");
2350 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2351 nfc_target.nfcid1_len = 10;
2352 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
2353 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2357 dev->tgt_available_prots = 0;
2360 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2362 /* ATR_RES general bytes are located at offset 17 */
2363 target_gt_len = resp->len - 17;
2364 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
2365 rsp->gt, target_gt_len);
2367 rc = nfc_dep_link_is_up(dev->nfc_dev,
2368 dev->nfc_dev->targets[0].idx,
2369 !active, NFC_RF_INITIATOR);
2372 dev_kfree_skb(resp);
2376 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
2377 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2378 u8 comm_mode, u8 *gb, size_t gb_len)
2380 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2381 struct sk_buff *skb;
2383 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
2384 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2386 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2388 if (dev->poll_mod_count) {
2389 nfc_err(&dev->interface->dev,
2390 "Cannot bring the DEP link up while polling\n");
2394 if (dev->tgt_active_prot) {
2395 nfc_err(&dev->interface->dev,
2396 "There is already an active target\n");
2400 skb_len = 3 + gb_len; /* ActPass + BR + Next */
2401 skb_len += PASSIVE_DATA_LEN;
2404 skb_len += NFC_NFCID3_MAXSIZE;
2405 if (target && !target->nfcid2_len) {
2408 get_random_bytes(nfcid3 + 2, 6);
2411 skb = pn533_alloc_skb(dev, skb_len);
2415 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2416 *skb_put(skb, 1) = 0x02; /* 424 kbps */
2418 next = skb_put(skb, 1); /* Next */
2421 /* Copy passive data */
2422 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
2425 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2426 if (target && target->nfcid2_len)
2427 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2428 target->nfcid2_len);
2430 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
2431 NFC_NFCID3_MAXSIZE);
2434 if (gb != NULL && gb_len > 0) {
2435 memcpy(skb_put(skb, gb_len), gb, gb_len);
2436 *next |= 4; /* We have some Gi */
2441 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2449 pn533_rf_field(dev->nfc_dev, 0);
2451 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2452 pn533_in_dep_link_up_complete, arg);
2462 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2464 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2466 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2468 pn533_poll_reset_mod_list(dev);
2470 if (dev->tgt_mode || dev->tgt_active_prot)
2471 pn533_abort_cmd(dev, GFP_KERNEL);
2473 dev->tgt_active_prot = 0;
2476 skb_queue_purge(&dev->resp_q);
2481 struct pn533_data_exchange_arg {
2482 data_exchange_cb_t cb;
2486 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2488 struct sk_buff *skb, *tmp, *t;
2489 unsigned int skb_len = 0, tmp_len = 0;
2491 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2493 if (skb_queue_empty(&dev->resp_q))
2496 if (skb_queue_len(&dev->resp_q) == 1) {
2497 skb = skb_dequeue(&dev->resp_q);
2501 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2502 skb_len += tmp->len;
2504 dev_dbg(&dev->interface->dev, "%s total length %d\n",
2507 skb = alloc_skb(skb_len, GFP_KERNEL);
2511 skb_put(skb, skb_len);
2513 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2514 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2515 tmp_len += tmp->len;
2519 skb_queue_purge(&dev->resp_q);
2524 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2525 struct sk_buff *resp)
2527 struct pn533_data_exchange_arg *arg = _arg;
2528 struct sk_buff *skb;
2532 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2539 status = resp->data[0];
2540 ret = status & PN533_CMD_RET_MASK;
2541 mi = status & PN533_CMD_MI_MASK;
2543 skb_pull(resp, sizeof(status));
2545 if (ret != PN533_CMD_RET_SUCCESS) {
2546 nfc_err(&dev->interface->dev,
2547 "Exchanging data failed (error 0x%x)\n", ret);
2552 skb_queue_tail(&dev->resp_q, resp);
2555 dev->cmd_complete_mi_arg = arg;
2556 queue_work(dev->wq, &dev->mi_rx_work);
2557 return -EINPROGRESS;
2560 /* Prepare for the next round */
2561 if (skb_queue_len(&dev->fragment_skb) > 0) {
2562 dev->cmd_complete_dep_arg = arg;
2563 queue_work(dev->wq, &dev->mi_tx_work);
2565 return -EINPROGRESS;
2568 skb = pn533_build_response(dev);
2572 arg->cb(arg->cb_context, skb, 0);
2577 dev_kfree_skb(resp);
2579 skb_queue_purge(&dev->resp_q);
2580 arg->cb(arg->cb_context, NULL, rc);
2585 /* Split the Tx skb into small chunks */
2586 static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2588 struct sk_buff *frag;
2592 /* Remaining size */
2593 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2594 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2596 frag_size = skb->len;
2598 /* Allocate and reserve */
2599 frag = pn533_alloc_skb(dev, frag_size);
2601 skb_queue_purge(&dev->fragment_skb);
2605 if (!dev->tgt_mode) {
2606 /* Reserve the TG/MI byte */
2607 skb_reserve(frag, 1);
2610 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2611 *skb_push(frag, sizeof(u8)) =
2612 (PN533_CMD_MI_MASK | 1);
2614 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2617 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2619 /* Reduce the size of incoming buffer */
2620 skb_pull(skb, frag_size);
2622 /* Add this to skb_queue */
2623 skb_queue_tail(&dev->fragment_skb, frag);
2625 } while (skb->len > 0);
2629 return skb_queue_len(&dev->fragment_skb);
2632 static int pn533_transceive(struct nfc_dev *nfc_dev,
2633 struct nfc_target *target, struct sk_buff *skb,
2634 data_exchange_cb_t cb, void *cb_context)
2636 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2637 struct pn533_data_exchange_arg *arg = NULL;
2640 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2642 if (!dev->tgt_active_prot) {
2643 nfc_err(&dev->interface->dev,
2644 "Can't exchange data if there is no active target\n");
2649 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2656 arg->cb_context = cb_context;
2658 switch (dev->device_type) {
2659 case PN533_DEVICE_PASORI:
2660 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2661 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2663 pn533_data_exchange_complete,
2670 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2671 rc = pn533_fill_fragment_skbs(dev, skb);
2675 skb = skb_dequeue(&dev->fragment_skb);
2681 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2684 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2685 skb, pn533_data_exchange_complete,
2691 if (rc < 0) /* rc from send_async */
2702 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2703 struct sk_buff *resp)
2707 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2710 return PTR_ERR(resp);
2712 status = resp->data[0];
2714 /* Prepare for the next round */
2715 if (skb_queue_len(&dev->fragment_skb) > 0) {
2716 queue_work(dev->wq, &dev->mi_tm_tx_work);
2717 return -EINPROGRESS;
2719 dev_kfree_skb(resp);
2722 nfc_tm_deactivated(dev->nfc_dev);
2729 queue_work(dev->wq, &dev->tg_work);
2734 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2736 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2739 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2741 /* let's split in multiple chunks if size's too big */
2742 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2743 rc = pn533_fill_fragment_skbs(dev, skb);
2747 /* get the first skb */
2748 skb = skb_dequeue(&dev->fragment_skb);
2754 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2755 pn533_tm_send_complete, NULL);
2758 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2759 pn533_tm_send_complete, NULL);
2765 skb_queue_purge(&dev->fragment_skb);
2771 static void pn533_wq_mi_recv(struct work_struct *work)
2773 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2774 struct sk_buff *skb;
2777 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2779 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2783 switch (dev->device_type) {
2784 case PN533_DEVICE_PASORI:
2785 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2786 rc = pn533_send_cmd_direct_async(dev,
2787 PN533_CMD_IN_COMM_THRU,
2789 pn533_data_exchange_complete,
2790 dev->cmd_complete_mi_arg);
2795 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
2797 rc = pn533_send_cmd_direct_async(dev,
2798 PN533_CMD_IN_DATA_EXCHANGE,
2800 pn533_data_exchange_complete,
2801 dev->cmd_complete_mi_arg);
2806 if (rc == 0) /* success */
2809 nfc_err(&dev->interface->dev,
2810 "Error %d when trying to perform data_exchange\n", rc);
2813 kfree(dev->cmd_complete_mi_arg);
2816 pn533_send_ack(dev, GFP_KERNEL);
2817 queue_work(dev->wq, &dev->cmd_work);
2820 static void pn533_wq_mi_send(struct work_struct *work)
2822 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2823 struct sk_buff *skb;
2826 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2828 /* Grab the first skb in the queue */
2829 skb = skb_dequeue(&dev->fragment_skb);
2831 if (skb == NULL) { /* No more data */
2832 /* Reset the queue for future use */
2833 skb_queue_head_init(&dev->fragment_skb);
2837 switch (dev->device_type) {
2838 case PN533_DEVICE_PASORI:
2839 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2844 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2846 pn533_data_exchange_complete,
2847 dev->cmd_complete_dep_arg);
2852 /* Still some fragments? */
2853 rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
2855 pn533_data_exchange_complete,
2856 dev->cmd_complete_dep_arg);
2861 if (rc == 0) /* success */
2864 nfc_err(&dev->interface->dev,
2865 "Error %d when trying to perform data_exchange\n", rc);
2868 kfree(dev->cmd_complete_dep_arg);
2871 pn533_send_ack(dev, GFP_KERNEL);
2872 queue_work(dev->wq, &dev->cmd_work);
2875 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2878 struct sk_buff *skb;
2879 struct sk_buff *resp;
2882 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2884 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2886 skb = pn533_alloc_skb(dev, skb_len);
2890 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2891 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2893 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2895 return PTR_ERR(resp);
2897 dev_kfree_skb(resp);
2901 static int pn533_get_firmware_version(struct pn533 *dev,
2902 struct pn533_fw_version *fv)
2904 struct sk_buff *skb;
2905 struct sk_buff *resp;
2907 skb = pn533_alloc_skb(dev, 0);
2911 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2913 return PTR_ERR(resp);
2915 fv->ic = resp->data[0];
2916 fv->ver = resp->data[1];
2917 fv->rev = resp->data[2];
2918 fv->support = resp->data[3];
2920 dev_kfree_skb(resp);
2924 static int pn533_pasori_fw_reset(struct pn533 *dev)
2926 struct sk_buff *skb;
2927 struct sk_buff *resp;
2929 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2931 skb = pn533_alloc_skb(dev, sizeof(u8));
2935 *skb_put(skb, sizeof(u8)) = 0x1;
2937 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2939 return PTR_ERR(resp);
2941 dev_kfree_skb(resp);
2946 struct pn533_acr122_poweron_rdr_arg {
2948 struct completion done;
2951 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2953 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2955 dev_dbg(&urb->dev->dev, "%s\n", __func__);
2957 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2958 urb->transfer_buffer, urb->transfer_buffer_length,
2961 arg->rc = urb->status;
2962 complete(&arg->done);
2965 static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2967 /* Power on th reader (CCID cmd) */
2968 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2969 0, 0, 0, 0, 0, 0, 3, 0, 0};
2973 struct pn533_acr122_poweron_rdr_arg arg;
2975 dev_dbg(&dev->interface->dev, "%s\n", __func__);
2977 init_completion(&arg.done);
2978 cntx = dev->in_urb->context; /* backup context */
2980 dev->in_urb->transfer_buffer = buf;
2981 dev->in_urb->transfer_buffer_length = 255;
2982 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2983 dev->in_urb->context = &arg;
2985 dev->out_urb->transfer_buffer = cmd;
2986 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2988 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2989 cmd, sizeof(cmd), false);
2991 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2993 nfc_err(&dev->interface->dev,
2994 "Reader power on cmd error %d\n", rc);
2998 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
3000 nfc_err(&dev->interface->dev,
3001 "Can't submit reader poweron cmd response %d\n", rc);
3005 wait_for_completion(&arg.done);
3006 dev->in_urb->context = cntx; /* restore context */
3011 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
3013 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
3017 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
3019 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
3020 (u8 *)&rf_field, 1);
3022 nfc_err(&dev->interface->dev, "Error on setting RF field\n");
3029 static int pn533_dev_up(struct nfc_dev *nfc_dev)
3031 return pn533_rf_field(nfc_dev, 1);
3034 static int pn533_dev_down(struct nfc_dev *nfc_dev)
3036 return pn533_rf_field(nfc_dev, 0);
3039 static struct nfc_ops pn533_nfc_ops = {
3040 .dev_up = pn533_dev_up,
3041 .dev_down = pn533_dev_down,
3042 .dep_link_up = pn533_dep_link_up,
3043 .dep_link_down = pn533_dep_link_down,
3044 .start_poll = pn533_start_poll,
3045 .stop_poll = pn533_stop_poll,
3046 .activate_target = pn533_activate_target,
3047 .deactivate_target = pn533_deactivate_target,
3048 .im_transceive = pn533_transceive,
3049 .tm_send = pn533_tm_send,
3052 static int pn533_setup(struct pn533 *dev)
3054 struct pn533_config_max_retries max_retries;
3055 struct pn533_config_timing timing;
3056 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
3059 switch (dev->device_type) {
3060 case PN533_DEVICE_STD:
3061 case PN533_DEVICE_PASORI:
3062 case PN533_DEVICE_ACR122U:
3063 max_retries.mx_rty_atr = 0x2;
3064 max_retries.mx_rty_psl = 0x1;
3065 max_retries.mx_rty_passive_act =
3066 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
3068 timing.rfu = PN533_CONFIG_TIMING_102;
3069 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
3070 timing.dep_timeout = PN533_CONFIG_TIMING_204;
3075 nfc_err(&dev->interface->dev, "Unknown device type %d\n",
3080 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
3081 (u8 *)&max_retries, sizeof(max_retries));
3083 nfc_err(&dev->interface->dev,
3084 "Error on setting MAX_RETRIES config\n");
3089 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
3090 (u8 *)&timing, sizeof(timing));
3092 nfc_err(&dev->interface->dev, "Error on setting RF timings\n");
3096 switch (dev->device_type) {
3097 case PN533_DEVICE_STD:
3100 case PN533_DEVICE_PASORI:
3101 pn533_pasori_fw_reset(dev);
3103 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
3106 nfc_err(&dev->interface->dev,
3107 "Error while settings PASORI config\n");
3111 pn533_pasori_fw_reset(dev);
3119 static int pn533_probe(struct usb_interface *interface,
3120 const struct usb_device_id *id)
3122 struct pn533_fw_version fw_ver;
3124 struct usb_host_interface *iface_desc;
3125 struct usb_endpoint_descriptor *endpoint;
3126 int in_endpoint = 0;
3127 int out_endpoint = 0;
3132 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3136 dev->udev = usb_get_dev(interface_to_usbdev(interface));
3137 dev->interface = interface;
3138 mutex_init(&dev->cmd_lock);
3140 iface_desc = interface->cur_altsetting;
3141 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
3142 endpoint = &iface_desc->endpoint[i].desc;
3144 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
3145 in_endpoint = endpoint->bEndpointAddress;
3147 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
3148 out_endpoint = endpoint->bEndpointAddress;
3151 if (!in_endpoint || !out_endpoint) {
3152 nfc_err(&interface->dev,
3153 "Could not find bulk-in or bulk-out endpoint\n");
3158 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
3159 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
3161 if (!dev->in_urb || !dev->out_urb)
3164 usb_fill_bulk_urb(dev->in_urb, dev->udev,
3165 usb_rcvbulkpipe(dev->udev, in_endpoint),
3166 NULL, 0, NULL, dev);
3167 usb_fill_bulk_urb(dev->out_urb, dev->udev,
3168 usb_sndbulkpipe(dev->udev, out_endpoint),
3169 NULL, 0, pn533_send_complete, dev);
3171 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
3172 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
3173 INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
3174 INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
3175 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
3176 INIT_WORK(&dev->mi_tm_rx_work, pn533_wq_tm_mi_recv);
3177 INIT_WORK(&dev->mi_tm_tx_work, pn533_wq_tm_mi_send);
3178 INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
3179 INIT_WORK(&dev->rf_work, pn533_wq_rf);
3180 dev->wq = alloc_ordered_workqueue("pn533", 0);
3181 if (dev->wq == NULL)
3184 init_timer(&dev->listen_timer);
3185 dev->listen_timer.data = (unsigned long) dev;
3186 dev->listen_timer.function = pn533_listen_mode_timer;
3188 skb_queue_head_init(&dev->resp_q);
3189 skb_queue_head_init(&dev->fragment_skb);
3191 INIT_LIST_HEAD(&dev->cmd_queue);
3193 usb_set_intfdata(interface, dev);
3195 dev->ops = &pn533_std_frame_ops;
3197 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
3198 dev->device_type = id->driver_info;
3199 switch (dev->device_type) {
3200 case PN533_DEVICE_STD:
3201 protocols = PN533_ALL_PROTOCOLS;
3204 case PN533_DEVICE_PASORI:
3205 protocols = PN533_NO_TYPE_B_PROTOCOLS;
3208 case PN533_DEVICE_ACR122U:
3209 protocols = PN533_NO_TYPE_B_PROTOCOLS;
3210 dev->ops = &pn533_acr122_frame_ops;
3211 dev->protocol_type = PN533_PROTO_REQ_RESP,
3213 rc = pn533_acr122_poweron_rdr(dev);
3215 nfc_err(&dev->interface->dev,
3216 "Couldn't poweron the reader (error %d)\n", rc);
3222 nfc_err(&dev->interface->dev, "Unknown device type %d\n",
3228 memset(&fw_ver, 0, sizeof(fw_ver));
3229 rc = pn533_get_firmware_version(dev, &fw_ver);
3233 nfc_info(&dev->interface->dev,
3234 "NXP PN5%02X firmware ver %d.%d now attached\n",
3235 fw_ver.ic, fw_ver.ver, fw_ver.rev);
3238 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
3239 dev->ops->tx_header_len +
3240 PN533_CMD_DATAEXCH_HEAD_LEN,
3241 dev->ops->tx_tail_len);
3242 if (!dev->nfc_dev) {
3247 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
3248 nfc_set_drvdata(dev->nfc_dev, dev);
3250 rc = nfc_register_device(dev->nfc_dev);
3254 rc = pn533_setup(dev);
3256 goto unregister_nfc_dev;
3261 nfc_unregister_device(dev->nfc_dev);
3264 nfc_free_device(dev->nfc_dev);
3267 destroy_workqueue(dev->wq);
3269 usb_free_urb(dev->in_urb);
3270 usb_free_urb(dev->out_urb);
3271 usb_put_dev(dev->udev);
3276 static void pn533_disconnect(struct usb_interface *interface)
3279 struct pn533_cmd *cmd, *n;
3281 dev = usb_get_intfdata(interface);
3282 usb_set_intfdata(interface, NULL);
3284 nfc_unregister_device(dev->nfc_dev);
3285 nfc_free_device(dev->nfc_dev);
3287 usb_kill_urb(dev->in_urb);
3288 usb_kill_urb(dev->out_urb);
3290 flush_delayed_work(&dev->poll_work);
3291 destroy_workqueue(dev->wq);
3293 skb_queue_purge(&dev->resp_q);
3295 del_timer(&dev->listen_timer);
3297 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
3298 list_del(&cmd->queue);
3302 usb_free_urb(dev->in_urb);
3303 usb_free_urb(dev->out_urb);
3306 nfc_info(&interface->dev, "NXP PN533 NFC device disconnected\n");
3309 static struct usb_driver pn533_driver = {
3311 .probe = pn533_probe,
3312 .disconnect = pn533_disconnect,
3313 .id_table = pn533_table,
3316 module_usb_driver(pn533_driver);
3318 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
3319 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
3320 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
3321 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
3322 MODULE_VERSION(VERSION);
3323 MODULE_LICENSE("GPL");