2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 * Copyright (C) 2012-2013 Tieto Poland
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/usb.h>
26 #include <linux/nfc.h>
27 #include <linux/netdevice.h>
28 #include <net/nfc/nfc.h>
32 #define PN533_VENDOR_ID 0x4CC
33 #define PN533_PRODUCT_ID 0x2533
35 #define SCM_VENDOR_ID 0x4E6
36 #define SCL3711_PRODUCT_ID 0x5591
38 #define SONY_VENDOR_ID 0x054c
39 #define PASORI_PRODUCT_ID 0x02e1
41 #define ACS_VENDOR_ID 0x072f
42 #define ACR122U_PRODUCT_ID 0x2200
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
46 #define PN533_DEVICE_ACR122U 0x3
48 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
49 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
50 NFC_PROTO_NFC_DEP_MASK |\
51 NFC_PROTO_ISO14443_B_MASK)
53 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
54 NFC_PROTO_MIFARE_MASK | \
55 NFC_PROTO_FELICA_MASK | \
56 NFC_PROTO_ISO14443_MASK | \
57 NFC_PROTO_NFC_DEP_MASK)
59 static const struct usb_device_id pn533_table
[] = {
60 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
61 .idVendor
= PN533_VENDOR_ID
,
62 .idProduct
= PN533_PRODUCT_ID
,
63 .driver_info
= PN533_DEVICE_STD
,
65 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
66 .idVendor
= SCM_VENDOR_ID
,
67 .idProduct
= SCL3711_PRODUCT_ID
,
68 .driver_info
= PN533_DEVICE_STD
,
70 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
71 .idVendor
= SONY_VENDOR_ID
,
72 .idProduct
= PASORI_PRODUCT_ID
,
73 .driver_info
= PN533_DEVICE_PASORI
,
75 { .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
76 .idVendor
= ACS_VENDOR_ID
,
77 .idProduct
= ACR122U_PRODUCT_ID
,
78 .driver_info
= PN533_DEVICE_ACR122U
,
82 MODULE_DEVICE_TABLE(usb
, pn533_table
);
84 /* How much time we spend listening for initiators */
85 #define PN533_LISTEN_TIME 2
87 /* Standard pn533 frame definitions */
88 #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
89 + 2) /* data[0] TFI, data[1] CC */
90 #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
93 * Max extended frame payload len, excluding TFI and CC
94 * which are already in PN533_FRAME_HEADER_LEN.
96 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
98 #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
100 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
101 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
104 #define PN533_STD_FRAME_SOF 0x00FF
106 /* standard frame identifier: in/out/error */
107 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
108 #define PN533_STD_FRAME_DIR_OUT 0xD4
109 #define PN533_STD_FRAME_DIR_IN 0xD5
111 /* ACS ACR122 pn533 frame definitions */
112 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
114 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
115 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
117 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
118 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
120 /* CCID messages types */
121 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
122 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
124 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
127 #define PN533_STD_FRAME_CMD(f) (f->data[1])
129 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
130 #define PN533_CMD_RF_CONFIGURATION 0x32
131 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
132 #define PN533_CMD_IN_COMM_THRU 0x42
133 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
134 #define PN533_CMD_IN_ATR 0x50
135 #define PN533_CMD_IN_RELEASE 0x52
136 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
138 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
139 #define PN533_CMD_TG_GET_DATA 0x86
140 #define PN533_CMD_TG_SET_DATA 0x8e
141 #define PN533_CMD_UNDEF 0xff
143 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
145 /* PN533 Return codes */
146 #define PN533_CMD_RET_MASK 0x3F
147 #define PN533_CMD_MI_MASK 0x40
148 #define PN533_CMD_RET_SUCCESS 0x00
152 typedef int (*pn533_send_async_complete_t
) (struct pn533
*dev
, void *arg
,
153 struct sk_buff
*resp
);
155 /* structs for pn533 commands */
157 /* PN533_CMD_GET_FIRMWARE_VERSION */
158 struct pn533_fw_version
{
165 /* PN533_CMD_RF_CONFIGURATION */
166 #define PN533_CFGITEM_RF_FIELD 0x01
167 #define PN533_CFGITEM_TIMING 0x02
168 #define PN533_CFGITEM_MAX_RETRIES 0x05
169 #define PN533_CFGITEM_PASORI 0x82
171 #define PN533_CFGITEM_RF_FIELD_ON 0x1
172 #define PN533_CFGITEM_RF_FIELD_OFF 0x0
174 #define PN533_CONFIG_TIMING_102 0xb
175 #define PN533_CONFIG_TIMING_204 0xc
176 #define PN533_CONFIG_TIMING_409 0xd
177 #define PN533_CONFIG_TIMING_819 0xe
179 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
180 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
182 struct pn533_config_max_retries
{
185 u8 mx_rty_passive_act
;
188 struct pn533_config_timing
{
194 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
196 /* felica commands opcode */
197 #define PN533_FELICA_OPC_SENSF_REQ 0
198 #define PN533_FELICA_OPC_SENSF_RES 1
199 /* felica SENSF_REQ parameters */
200 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
201 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
202 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
203 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
205 /* type B initiator_data values */
206 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
207 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
208 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
210 union pn533_cmd_poll_initdata
{
223 /* Poll modulations */
225 PN533_POLL_MOD_106KBPS_A
,
226 PN533_POLL_MOD_212KBPS_FELICA
,
227 PN533_POLL_MOD_424KBPS_FELICA
,
228 PN533_POLL_MOD_106KBPS_JEWEL
,
229 PN533_POLL_MOD_847KBPS_B
,
232 __PN533_POLL_MOD_AFTER_LAST
,
234 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
236 struct pn533_poll_modulations
{
240 union pn533_cmd_poll_initdata initiator_data
;
245 static const struct pn533_poll_modulations poll_mod
[] = {
246 [PN533_POLL_MOD_106KBPS_A
] = {
253 [PN533_POLL_MOD_212KBPS_FELICA
] = {
257 .initiator_data
.felica
= {
258 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
259 .sc
= PN533_FELICA_SENSF_SC_ALL
,
260 .rc
= PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE
,
266 [PN533_POLL_MOD_424KBPS_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_NO_SYSTEM_CODE
,
279 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
286 [PN533_POLL_MOD_847KBPS_B
] = {
290 .initiator_data
.type_b
= {
291 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
293 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
298 [PN533_LISTEN_MOD
] = {
303 /* PN533_CMD_IN_ATR */
305 struct pn533_cmd_activate_response
{
317 struct pn533_cmd_jump_dep_response
{
331 /* PN533_TG_INIT_AS_TARGET */
332 #define PN533_INIT_TARGET_PASSIVE 0x1
333 #define PN533_INIT_TARGET_DEP 0x2
335 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
336 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
337 #define PN533_INIT_TARGET_RESP_DEP 0x4
339 enum pn533_protocol_type
{
340 PN533_PROTO_REQ_ACK_RESP
= 0,
345 struct usb_device
*udev
;
346 struct usb_interface
*interface
;
347 struct nfc_dev
*nfc_dev
;
349 enum pn533_protocol_type protocol_type
;
354 struct sk_buff_head resp_q
;
356 struct workqueue_struct
*wq
;
357 struct work_struct cmd_work
;
358 struct work_struct cmd_complete_work
;
359 struct work_struct poll_work
;
360 struct work_struct mi_work
;
361 struct work_struct tg_work
;
363 struct list_head cmd_queue
;
364 struct pn533_cmd
*cmd
;
366 struct mutex cmd_lock
; /* protects cmd queue */
368 void *cmd_complete_mi_arg
;
370 struct pn533_poll_modulations
*poll_mod_active
[PN533_POLL_MOD_MAX
+ 1];
374 u32 listen_protocols
;
375 struct timer_list listen_timer
;
381 u8 tgt_available_prots
;
385 struct pn533_frame_ops
*ops
;
389 struct list_head queue
;
393 struct sk_buff
*resp
;
395 pn533_send_async_complete_t complete_cb
;
396 void *complete_cb_context
;
399 struct pn533_std_frame
{
407 struct pn533_frame_ops
{
408 void (*tx_frame_init
)(void *frame
, u8 cmd_code
);
409 void (*tx_frame_finish
)(void *frame
);
410 void (*tx_update_payload_len
)(void *frame
, int len
);
414 bool (*rx_is_frame_valid
)(void *frame
);
415 int (*rx_frame_size
)(void *frame
);
420 u8 (*get_cmd_code
)(void *frame
);
423 struct pn533_acr122_ccid_hdr
{
428 u8 params
[3]; /* 3 msg specific bytes or status, error and 1 specific
429 byte for reposnse msg */
430 u8 data
[]; /* payload */
433 struct pn533_acr122_apdu_hdr
{
440 struct pn533_acr122_tx_frame
{
441 struct pn533_acr122_ccid_hdr ccid
;
442 struct pn533_acr122_apdu_hdr apdu
;
444 u8 data
[]; /* pn533 frame: TFI ... */
447 struct pn533_acr122_rx_frame
{
448 struct pn533_acr122_ccid_hdr ccid
;
449 u8 data
[]; /* pn533 frame : TFI ... */
452 static void pn533_acr122_tx_frame_init(void *_frame
, u8 cmd_code
)
454 struct pn533_acr122_tx_frame
*frame
= _frame
;
456 frame
->ccid
.type
= PN533_ACR122_PC_TO_RDR_ESCAPE
;
457 frame
->ccid
.datalen
= sizeof(frame
->apdu
) + 1; /* sizeof(apdu_hdr) +
459 frame
->ccid
.slot
= 0;
461 frame
->ccid
.params
[0] = 0;
462 frame
->ccid
.params
[1] = 0;
463 frame
->ccid
.params
[2] = 0;
465 frame
->data
[0] = PN533_STD_FRAME_DIR_OUT
;
466 frame
->data
[1] = cmd_code
;
467 frame
->datalen
= 2; /* data[0] + data[1] */
469 frame
->apdu
.class = 0xFF;
475 static void pn533_acr122_tx_frame_finish(void *_frame
)
477 struct pn533_acr122_tx_frame
*frame
= _frame
;
479 frame
->ccid
.datalen
+= frame
->datalen
;
482 static void pn533_acr122_tx_update_payload_len(void *_frame
, int len
)
484 struct pn533_acr122_tx_frame
*frame
= _frame
;
486 frame
->datalen
+= len
;
489 static bool pn533_acr122_is_rx_frame_valid(void *_frame
)
491 struct pn533_acr122_rx_frame
*frame
= _frame
;
493 if (frame
->ccid
.type
!= 0x83)
496 if (frame
->data
[frame
->ccid
.datalen
- 2] == 0x63)
502 static int pn533_acr122_rx_frame_size(void *frame
)
504 struct pn533_acr122_rx_frame
*f
= frame
;
506 /* f->ccid.datalen already includes tail length */
507 return sizeof(struct pn533_acr122_rx_frame
) + f
->ccid
.datalen
;
510 static u8
pn533_acr122_get_cmd_code(void *frame
)
512 struct pn533_acr122_rx_frame
*f
= frame
;
514 return PN533_STD_FRAME_CMD(f
);
517 static struct pn533_frame_ops pn533_acr122_frame_ops
= {
518 .tx_frame_init
= pn533_acr122_tx_frame_init
,
519 .tx_frame_finish
= pn533_acr122_tx_frame_finish
,
520 .tx_update_payload_len
= pn533_acr122_tx_update_payload_len
,
521 .tx_header_len
= PN533_ACR122_TX_FRAME_HEADER_LEN
,
522 .tx_tail_len
= PN533_ACR122_TX_FRAME_TAIL_LEN
,
524 .rx_is_frame_valid
= pn533_acr122_is_rx_frame_valid
,
525 .rx_header_len
= PN533_ACR122_RX_FRAME_HEADER_LEN
,
526 .rx_tail_len
= PN533_ACR122_RX_FRAME_TAIL_LEN
,
527 .rx_frame_size
= pn533_acr122_rx_frame_size
,
529 .max_payload_len
= PN533_ACR122_FRAME_MAX_PAYLOAD_LEN
,
530 .get_cmd_code
= pn533_acr122_get_cmd_code
,
533 /* The rule: value + checksum = 0 */
534 static inline u8
pn533_std_checksum(u8 value
)
539 /* The rule: sum(data elements) + checksum = 0 */
540 static u8
pn533_std_data_checksum(u8
*data
, int datalen
)
545 for (i
= 0; i
< datalen
; i
++)
548 return pn533_std_checksum(sum
);
551 static void pn533_std_tx_frame_init(void *_frame
, u8 cmd_code
)
553 struct pn533_std_frame
*frame
= _frame
;
556 frame
->start_frame
= cpu_to_be16(PN533_STD_FRAME_SOF
);
557 PN533_STD_FRAME_IDENTIFIER(frame
) = PN533_STD_FRAME_DIR_OUT
;
558 PN533_STD_FRAME_CMD(frame
) = cmd_code
;
562 static void pn533_std_tx_frame_finish(void *_frame
)
564 struct pn533_std_frame
*frame
= _frame
;
566 frame
->datalen_checksum
= pn533_std_checksum(frame
->datalen
);
568 PN533_STD_FRAME_CHECKSUM(frame
) =
569 pn533_std_data_checksum(frame
->data
, frame
->datalen
);
571 PN533_STD_FRAME_POSTAMBLE(frame
) = 0;
574 static void pn533_std_tx_update_payload_len(void *_frame
, int len
)
576 struct pn533_std_frame
*frame
= _frame
;
578 frame
->datalen
+= len
;
581 static bool pn533_std_rx_frame_is_valid(void *_frame
)
584 struct pn533_std_frame
*frame
= _frame
;
586 if (frame
->start_frame
!= cpu_to_be16(PN533_STD_FRAME_SOF
))
589 checksum
= pn533_std_checksum(frame
->datalen
);
590 if (checksum
!= frame
->datalen_checksum
)
593 checksum
= pn533_std_data_checksum(frame
->data
, frame
->datalen
);
594 if (checksum
!= PN533_STD_FRAME_CHECKSUM(frame
))
600 static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame
*frame
)
602 if (frame
->start_frame
!= cpu_to_be16(PN533_STD_FRAME_SOF
))
605 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
611 static inline int pn533_std_rx_frame_size(void *frame
)
613 struct pn533_std_frame
*f
= frame
;
615 return sizeof(struct pn533_std_frame
) + f
->datalen
+
616 PN533_STD_FRAME_TAIL_LEN
;
619 static u8
pn533_std_get_cmd_code(void *frame
)
621 struct pn533_std_frame
*f
= frame
;
623 return PN533_STD_FRAME_CMD(f
);
626 static struct pn533_frame_ops pn533_std_frame_ops
= {
627 .tx_frame_init
= pn533_std_tx_frame_init
,
628 .tx_frame_finish
= pn533_std_tx_frame_finish
,
629 .tx_update_payload_len
= pn533_std_tx_update_payload_len
,
630 .tx_header_len
= PN533_STD_FRAME_HEADER_LEN
,
631 .tx_tail_len
= PN533_STD_FRAME_TAIL_LEN
,
633 .rx_is_frame_valid
= pn533_std_rx_frame_is_valid
,
634 .rx_frame_size
= pn533_std_rx_frame_size
,
635 .rx_header_len
= PN533_STD_FRAME_HEADER_LEN
,
636 .rx_tail_len
= PN533_STD_FRAME_TAIL_LEN
,
638 .max_payload_len
= PN533_STD_FRAME_MAX_PAYLOAD_LEN
,
639 .get_cmd_code
= pn533_std_get_cmd_code
,
642 static bool pn533_rx_frame_is_cmd_response(struct pn533
*dev
, void *frame
)
644 return (dev
->ops
->get_cmd_code(frame
) ==
645 PN533_CMD_RESPONSE(dev
->cmd
->code
));
648 static void pn533_recv_response(struct urb
*urb
)
650 struct pn533
*dev
= urb
->context
;
651 struct pn533_cmd
*cmd
= dev
->cmd
;
654 cmd
->status
= urb
->status
;
656 switch (urb
->status
) {
661 nfc_dev_dbg(&dev
->interface
->dev
,
662 "The urb has been canceled (status %d)",
667 nfc_dev_err(&dev
->interface
->dev
,
668 "Urb failure (status %d)", urb
->status
);
672 in_frame
= dev
->in_urb
->transfer_buffer
;
674 nfc_dev_dbg(&dev
->interface
->dev
, "Received a frame.");
675 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE
, 16, 1, in_frame
,
676 dev
->ops
->rx_frame_size(in_frame
), false);
678 if (!dev
->ops
->rx_is_frame_valid(in_frame
)) {
679 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid frame");
684 if (!pn533_rx_frame_is_cmd_response(dev
, in_frame
)) {
685 nfc_dev_err(&dev
->interface
->dev
,
686 "It it not the response to the last command");
692 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
695 static int pn533_submit_urb_for_response(struct pn533
*dev
, gfp_t flags
)
697 dev
->in_urb
->complete
= pn533_recv_response
;
699 return usb_submit_urb(dev
->in_urb
, flags
);
702 static void pn533_recv_ack(struct urb
*urb
)
704 struct pn533
*dev
= urb
->context
;
705 struct pn533_cmd
*cmd
= dev
->cmd
;
706 struct pn533_std_frame
*in_frame
;
709 cmd
->status
= urb
->status
;
711 switch (urb
->status
) {
716 nfc_dev_dbg(&dev
->interface
->dev
,
717 "The urb has been stopped (status %d)",
722 nfc_dev_err(&dev
->interface
->dev
,
723 "Urb failure (status %d)", urb
->status
);
727 in_frame
= dev
->in_urb
->transfer_buffer
;
729 if (!pn533_std_rx_frame_is_ack(in_frame
)) {
730 nfc_dev_err(&dev
->interface
->dev
, "Received an invalid ack");
735 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
737 nfc_dev_err(&dev
->interface
->dev
,
738 "usb_submit_urb failed with result %d", rc
);
746 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
749 static int pn533_submit_urb_for_ack(struct pn533
*dev
, gfp_t flags
)
751 dev
->in_urb
->complete
= pn533_recv_ack
;
753 return usb_submit_urb(dev
->in_urb
, flags
);
756 static int pn533_send_ack(struct pn533
*dev
, gfp_t flags
)
758 u8 ack
[PN533_STD_FRAME_ACK_SIZE
] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
759 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
762 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
764 dev
->out_urb
->transfer_buffer
= ack
;
765 dev
->out_urb
->transfer_buffer_length
= sizeof(ack
);
766 rc
= usb_submit_urb(dev
->out_urb
, flags
);
771 static int __pn533_send_frame_async(struct pn533
*dev
,
778 dev
->out_urb
->transfer_buffer
= out
->data
;
779 dev
->out_urb
->transfer_buffer_length
= out
->len
;
781 dev
->in_urb
->transfer_buffer
= in
->data
;
782 dev
->in_urb
->transfer_buffer_length
= in_len
;
784 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE
, 16, 1,
785 out
->data
, out
->len
, false);
787 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
791 if (dev
->protocol_type
== PN533_PROTO_REQ_RESP
) {
792 /* request for response for sent packet directly */
793 rc
= pn533_submit_urb_for_response(dev
, GFP_ATOMIC
);
796 } else if (dev
->protocol_type
== PN533_PROTO_REQ_ACK_RESP
) {
797 /* request for ACK if that's the case */
798 rc
= pn533_submit_urb_for_ack(dev
, GFP_KERNEL
);
806 usb_unlink_urb(dev
->out_urb
);
810 static void pn533_build_cmd_frame(struct pn533
*dev
, u8 cmd_code
,
813 /* payload is already there, just update datalen */
814 int payload_len
= skb
->len
;
815 struct pn533_frame_ops
*ops
= dev
->ops
;
818 skb_push(skb
, ops
->tx_header_len
);
819 skb_put(skb
, ops
->tx_tail_len
);
821 ops
->tx_frame_init(skb
->data
, cmd_code
);
822 ops
->tx_update_payload_len(skb
->data
, payload_len
);
823 ops
->tx_frame_finish(skb
->data
);
826 static int pn533_send_async_complete(struct pn533
*dev
)
828 struct pn533_cmd
*cmd
= dev
->cmd
;
829 int status
= cmd
->status
;
831 struct sk_buff
*req
= cmd
->req
;
832 struct sk_buff
*resp
= cmd
->resp
;
839 rc
= cmd
->complete_cb(dev
, cmd
->complete_cb_context
,
845 skb_put(resp
, dev
->ops
->rx_frame_size(resp
->data
));
846 skb_pull(resp
, dev
->ops
->rx_header_len
);
847 skb_trim(resp
, resp
->len
- dev
->ops
->rx_tail_len
);
849 rc
= cmd
->complete_cb(dev
, cmd
->complete_cb_context
, resp
);
857 static int __pn533_send_async(struct pn533
*dev
, u8 cmd_code
,
858 struct sk_buff
*req
, struct sk_buff
*resp
,
860 pn533_send_async_complete_t complete_cb
,
861 void *complete_cb_context
)
863 struct pn533_cmd
*cmd
;
866 nfc_dev_dbg(&dev
->interface
->dev
, "Sending command 0x%x", cmd_code
);
868 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
872 cmd
->code
= cmd_code
;
875 cmd
->resp_len
= resp_len
;
876 cmd
->complete_cb
= complete_cb
;
877 cmd
->complete_cb_context
= complete_cb_context
;
879 pn533_build_cmd_frame(dev
, cmd_code
, req
);
881 mutex_lock(&dev
->cmd_lock
);
883 if (!dev
->cmd_pending
) {
884 rc
= __pn533_send_frame_async(dev
, req
, resp
, resp_len
);
888 dev
->cmd_pending
= 1;
893 nfc_dev_dbg(&dev
->interface
->dev
, "%s Queueing command 0x%x", __func__
,
896 INIT_LIST_HEAD(&cmd
->queue
);
897 list_add_tail(&cmd
->queue
, &dev
->cmd_queue
);
904 mutex_unlock(&dev
->cmd_lock
);
908 static int pn533_send_data_async(struct pn533
*dev
, u8 cmd_code
,
910 pn533_send_async_complete_t complete_cb
,
911 void *complete_cb_context
)
913 struct sk_buff
*resp
;
915 int resp_len
= dev
->ops
->rx_header_len
+
916 dev
->ops
->max_payload_len
+
917 dev
->ops
->rx_tail_len
;
919 resp
= nfc_alloc_recv_skb(resp_len
, GFP_KERNEL
);
923 rc
= __pn533_send_async(dev
, cmd_code
, req
, resp
, resp_len
, complete_cb
,
924 complete_cb_context
);
931 static int pn533_send_cmd_async(struct pn533
*dev
, u8 cmd_code
,
933 pn533_send_async_complete_t complete_cb
,
934 void *complete_cb_context
)
936 struct sk_buff
*resp
;
938 int resp_len
= dev
->ops
->rx_header_len
+
939 dev
->ops
->max_payload_len
+
940 dev
->ops
->rx_tail_len
;
942 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
946 rc
= __pn533_send_async(dev
, cmd_code
, req
, resp
, resp_len
, complete_cb
,
947 complete_cb_context
);
955 * pn533_send_cmd_direct_async
957 * The function sends a piority cmd directly to the chip omiting the cmd
958 * queue. It's intended to be used by chaining mechanism of received responses
959 * where the host has to request every single chunk of data before scheduling
960 * next cmd from the queue.
962 static int pn533_send_cmd_direct_async(struct pn533
*dev
, u8 cmd_code
,
964 pn533_send_async_complete_t complete_cb
,
965 void *complete_cb_context
)
967 struct sk_buff
*resp
;
968 struct pn533_cmd
*cmd
;
970 int resp_len
= dev
->ops
->rx_header_len
+
971 dev
->ops
->max_payload_len
+
972 dev
->ops
->rx_tail_len
;
974 resp
= alloc_skb(resp_len
, GFP_KERNEL
);
978 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
984 cmd
->code
= cmd_code
;
987 cmd
->resp_len
= resp_len
;
988 cmd
->complete_cb
= complete_cb
;
989 cmd
->complete_cb_context
= complete_cb_context
;
991 pn533_build_cmd_frame(dev
, cmd_code
, req
);
993 rc
= __pn533_send_frame_async(dev
, req
, resp
, resp_len
);
1004 static void pn533_wq_cmd_complete(struct work_struct
*work
)
1006 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_complete_work
);
1009 rc
= pn533_send_async_complete(dev
);
1010 if (rc
!= -EINPROGRESS
)
1011 queue_work(dev
->wq
, &dev
->cmd_work
);
1014 static void pn533_wq_cmd(struct work_struct
*work
)
1016 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
1017 struct pn533_cmd
*cmd
;
1020 mutex_lock(&dev
->cmd_lock
);
1022 if (list_empty(&dev
->cmd_queue
)) {
1023 dev
->cmd_pending
= 0;
1024 mutex_unlock(&dev
->cmd_lock
);
1028 cmd
= list_first_entry(&dev
->cmd_queue
, struct pn533_cmd
, queue
);
1030 list_del(&cmd
->queue
);
1032 mutex_unlock(&dev
->cmd_lock
);
1034 rc
= __pn533_send_frame_async(dev
, cmd
->req
, cmd
->resp
, cmd
->resp_len
);
1036 dev_kfree_skb(cmd
->req
);
1037 dev_kfree_skb(cmd
->resp
);
1045 struct pn533_sync_cmd_response
{
1046 struct sk_buff
*resp
;
1047 struct completion done
;
1050 static int pn533_send_sync_complete(struct pn533
*dev
, void *_arg
,
1051 struct sk_buff
*resp
)
1053 struct pn533_sync_cmd_response
*arg
= _arg
;
1056 complete(&arg
->done
);
1061 /* pn533_send_cmd_sync
1063 * Please note the req parameter is freed inside the function to
1064 * limit a number of return value interpretations by the caller.
1066 * 1. negative in case of error during TX path -> req should be freed
1068 * 2. negative in case of error during RX path -> req should not be freed
1069 * as it's been already freed at the begining of RX path by
1070 * async_complete_cb.
1072 * 3. valid pointer in case of succesfult RX path
1074 * A caller has to check a return value with IS_ERR macro. If the test pass,
1075 * the returned pointer is valid.
1078 static struct sk_buff
*pn533_send_cmd_sync(struct pn533
*dev
, u8 cmd_code
,
1079 struct sk_buff
*req
)
1082 struct pn533_sync_cmd_response arg
;
1084 init_completion(&arg
.done
);
1086 rc
= pn533_send_cmd_async(dev
, cmd_code
, req
,
1087 pn533_send_sync_complete
, &arg
);
1093 wait_for_completion(&arg
.done
);
1098 static void pn533_send_complete(struct urb
*urb
)
1100 struct pn533
*dev
= urb
->context
;
1102 switch (urb
->status
) {
1104 break; /* success */
1107 nfc_dev_dbg(&dev
->interface
->dev
,
1108 "The urb has been stopped (status %d)",
1113 nfc_dev_err(&dev
->interface
->dev
,
1114 "Urb failure (status %d)", urb
->status
);
1118 static void pn533_abort_cmd(struct pn533
*dev
, gfp_t flags
)
1120 /* ACR122U does not support any command which aborts last
1121 * issued command i.e. as ACK for standard PN533. Additionally,
1122 * it behaves stange, sending broken or incorrect responses,
1123 * when we cancel urb before the chip will send response.
1125 if (dev
->device_type
== PN533_DEVICE_ACR122U
)
1128 /* An ack will cancel the last issued command */
1129 pn533_send_ack(dev
, flags
);
1131 /* cancel the urb request */
1132 usb_kill_urb(dev
->in_urb
);
1135 static struct sk_buff
*pn533_alloc_skb(struct pn533
*dev
, unsigned int size
)
1137 struct sk_buff
*skb
;
1139 skb
= alloc_skb(dev
->ops
->tx_header_len
+
1141 dev
->ops
->tx_tail_len
, GFP_KERNEL
);
1144 skb_reserve(skb
, dev
->ops
->tx_header_len
);
1149 struct pn533_target_type_a
{
1157 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1158 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1159 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1161 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1162 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1164 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1165 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1167 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1168 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1169 #define PN533_TYPE_A_SEL_PROT_DEP 2
1170 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1172 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
1173 int target_data_len
)
1178 if (target_data_len
< sizeof(struct pn533_target_type_a
))
1181 /* The lenght check of nfcid[] and ats[] are not being performed because
1182 the values are not being used */
1184 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1185 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
1186 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
1188 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1189 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
1190 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1191 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
1194 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1195 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
1201 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1204 struct pn533_target_type_a
*tgt_type_a
;
1206 tgt_type_a
= (struct pn533_target_type_a
*)tgt_data
;
1208 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
1211 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
1212 case PN533_TYPE_A_SEL_PROT_MIFARE
:
1213 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
1215 case PN533_TYPE_A_SEL_PROT_ISO14443
:
1216 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
1218 case PN533_TYPE_A_SEL_PROT_DEP
:
1219 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1221 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
1222 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
1223 NFC_PROTO_NFC_DEP_MASK
;
1227 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
1228 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
1229 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
1230 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
1235 struct pn533_target_felica
{
1244 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1245 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1247 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
1248 int target_data_len
)
1250 if (target_data_len
< sizeof(struct pn533_target_felica
))
1253 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
1259 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1262 struct pn533_target_felica
*tgt_felica
;
1264 tgt_felica
= (struct pn533_target_felica
*)tgt_data
;
1266 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
1269 if ((tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
) &&
1270 (tgt_felica
->nfcid2
[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2
))
1271 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1273 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
1275 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
1276 nfc_tgt
->sensf_res_len
= 9;
1281 struct pn533_target_jewel
{
1286 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
1287 int target_data_len
)
1292 if (target_data_len
< sizeof(struct pn533_target_jewel
))
1295 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1296 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
1297 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
1299 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1300 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
1301 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
1302 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
1308 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1311 struct pn533_target_jewel
*tgt_jewel
;
1313 tgt_jewel
= (struct pn533_target_jewel
*)tgt_data
;
1315 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
1318 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
1319 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
1320 nfc_tgt
->nfcid1_len
= 4;
1321 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
1326 struct pn533_type_b_prot_info
{
1332 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1333 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1334 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1336 struct pn533_type_b_sens_res
{
1340 struct pn533_type_b_prot_info prot_info
;
1343 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1345 struct pn533_target_type_b
{
1346 struct pn533_type_b_sens_res sensb_res
;
1351 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
1352 int target_data_len
)
1354 if (target_data_len
< sizeof(struct pn533_target_type_b
))
1357 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
1360 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
1361 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
1367 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
1370 struct pn533_target_type_b
*tgt_type_b
;
1372 tgt_type_b
= (struct pn533_target_type_b
*)tgt_data
;
1374 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
1377 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
1382 static int pn533_target_found(struct pn533
*dev
, u8 tg
, u8
*tgdata
,
1385 struct nfc_target nfc_tgt
;
1388 nfc_dev_dbg(&dev
->interface
->dev
, "%s - modulation=%d", __func__
,
1389 dev
->poll_mod_curr
);
1394 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1396 switch (dev
->poll_mod_curr
) {
1397 case PN533_POLL_MOD_106KBPS_A
:
1398 rc
= pn533_target_found_type_a(&nfc_tgt
, tgdata
, tgdata_len
);
1400 case PN533_POLL_MOD_212KBPS_FELICA
:
1401 case PN533_POLL_MOD_424KBPS_FELICA
:
1402 rc
= pn533_target_found_felica(&nfc_tgt
, tgdata
, tgdata_len
);
1404 case PN533_POLL_MOD_106KBPS_JEWEL
:
1405 rc
= pn533_target_found_jewel(&nfc_tgt
, tgdata
, tgdata_len
);
1407 case PN533_POLL_MOD_847KBPS_B
:
1408 rc
= pn533_target_found_type_b(&nfc_tgt
, tgdata
, tgdata_len
);
1411 nfc_dev_err(&dev
->interface
->dev
,
1412 "Unknown current poll modulation");
1419 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1420 nfc_dev_dbg(&dev
->interface
->dev
,
1421 "The Tg found doesn't have the desired protocol");
1425 nfc_dev_dbg(&dev
->interface
->dev
,
1426 "Target found - supported protocols: 0x%x",
1427 nfc_tgt
.supported_protocols
);
1429 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1431 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1436 static inline void pn533_poll_next_mod(struct pn533
*dev
)
1438 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
1441 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
1443 dev
->poll_mod_count
= 0;
1446 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
1448 dev
->poll_mod_active
[dev
->poll_mod_count
] =
1449 (struct pn533_poll_modulations
*)&poll_mod
[mod_index
];
1450 dev
->poll_mod_count
++;
1453 static void pn533_poll_create_mod_list(struct pn533
*dev
,
1454 u32 im_protocols
, u32 tm_protocols
)
1456 pn533_poll_reset_mod_list(dev
);
1458 if ((im_protocols
& NFC_PROTO_MIFARE_MASK
) ||
1459 (im_protocols
& NFC_PROTO_ISO14443_MASK
) ||
1460 (im_protocols
& NFC_PROTO_NFC_DEP_MASK
))
1461 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
1463 if (im_protocols
& NFC_PROTO_FELICA_MASK
||
1464 im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1465 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
1466 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
1469 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1470 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
1472 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1473 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
1476 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
1479 static int pn533_start_poll_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1481 u8 nbtg
, tg
, *tgdata
;
1484 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1486 nbtg
= resp
->data
[0];
1488 tgdata
= &resp
->data
[2];
1489 tgdata_len
= resp
->len
- 2; /* nbtg + tg */
1492 rc
= pn533_target_found(dev
, tg
, tgdata
, tgdata_len
);
1494 /* We must stop the poll after a valid target found */
1496 pn533_poll_reset_mod_list(dev
);
1504 static struct sk_buff
*pn533_alloc_poll_tg_frame(struct pn533
*dev
)
1506 struct sk_buff
*skb
;
1507 u8
*felica
, *nfcid3
, *gb
;
1509 u8
*gbytes
= dev
->gb
;
1510 size_t gbytes_len
= dev
->gb_len
;
1512 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1513 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1514 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1515 0xff, 0xff}; /* System code */
1517 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1519 0x40}; /* SEL_RES for DEP */
1521 unsigned int skb_len
= 36 + /* mode (1), mifare (6),
1522 felica (18), nfcid3 (10), gb_len (1) */
1526 skb
= pn533_alloc_skb(dev
, skb_len
);
1530 /* DEP support only */
1531 *skb_put(skb
, 1) = PN533_INIT_TARGET_DEP
;
1534 memcpy(skb_put(skb
, 6), mifare_params
, 6);
1537 felica
= skb_put(skb
, 18);
1538 memcpy(felica
, felica_params
, 18);
1539 get_random_bytes(felica
+ 2, 6);
1542 nfcid3
= skb_put(skb
, 10);
1543 memset(nfcid3
, 0, 10);
1544 memcpy(nfcid3
, felica
, 8);
1547 *skb_put(skb
, 1) = gbytes_len
;
1549 gb
= skb_put(skb
, gbytes_len
);
1550 memcpy(gb
, gbytes
, gbytes_len
);
1553 *skb_put(skb
, 1) = 0;
1558 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1559 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1560 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1561 struct sk_buff
*resp
)
1565 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1568 return PTR_ERR(resp
);
1570 status
= resp
->data
[0];
1571 skb_pull(resp
, sizeof(status
));
1574 nfc_tm_deactivated(dev
->nfc_dev
);
1576 dev_kfree_skb(resp
);
1580 return nfc_tm_data_received(dev
->nfc_dev
, resp
);
1583 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1585 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1587 struct sk_buff
*skb
;
1590 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1592 skb
= pn533_alloc_skb(dev
, 0);
1596 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_GET_DATA
, skb
,
1597 pn533_tm_get_data_complete
, NULL
);
1605 #define ATR_REQ_GB_OFFSET 17
1606 static int pn533_init_target_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1608 u8 mode
, *cmd
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1612 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1614 if (resp
->len
< ATR_REQ_GB_OFFSET
+ 1)
1617 mode
= resp
->data
[0];
1618 cmd
= &resp
->data
[1];
1620 nfc_dev_dbg(&dev
->interface
->dev
, "Target mode 0x%x len %d\n",
1623 if ((mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
) ==
1624 PN533_INIT_TARGET_RESP_ACTIVE
)
1625 comm_mode
= NFC_COMM_ACTIVE
;
1627 if ((mode
& PN533_INIT_TARGET_RESP_DEP
) == 0) /* Only DEP supported */
1630 gb
= cmd
+ ATR_REQ_GB_OFFSET
;
1631 gb_len
= resp
->len
- (ATR_REQ_GB_OFFSET
+ 1);
1633 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1634 comm_mode
, gb
, gb_len
);
1636 nfc_dev_err(&dev
->interface
->dev
,
1637 "Error when signaling target activation");
1642 queue_work(dev
->wq
, &dev
->tg_work
);
1647 static void pn533_listen_mode_timer(unsigned long data
)
1649 struct pn533
*dev
= (struct pn533
*)data
;
1651 nfc_dev_dbg(&dev
->interface
->dev
, "Listen mode timeout");
1653 dev
->cancel_listen
= 1;
1655 pn533_poll_next_mod(dev
);
1657 queue_work(dev
->wq
, &dev
->poll_work
);
1660 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1661 struct sk_buff
*resp
)
1663 struct pn533_poll_modulations
*cur_mod
;
1666 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1671 nfc_dev_err(&dev
->interface
->dev
, "%s Poll complete error %d",
1674 if (rc
== -ENOENT
) {
1675 if (dev
->poll_mod_count
!= 0)
1679 } else if (rc
< 0) {
1680 nfc_dev_err(&dev
->interface
->dev
,
1681 "Error %d when running poll", rc
);
1686 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1688 if (cur_mod
->len
== 0) { /* Target mode */
1689 del_timer(&dev
->listen_timer
);
1690 rc
= pn533_init_target_complete(dev
, resp
);
1694 /* Initiator mode */
1695 rc
= pn533_start_poll_complete(dev
, resp
);
1699 if (!dev
->poll_mod_count
) {
1700 nfc_dev_dbg(&dev
->interface
->dev
, "Polling has been stoped.");
1704 pn533_poll_next_mod(dev
);
1705 queue_work(dev
->wq
, &dev
->poll_work
);
1708 dev_kfree_skb(resp
);
1712 nfc_dev_err(&dev
->interface
->dev
, "Polling operation has been stopped");
1714 pn533_poll_reset_mod_list(dev
);
1715 dev
->poll_protocols
= 0;
1719 static struct sk_buff
*pn533_alloc_poll_in_frame(struct pn533
*dev
,
1720 struct pn533_poll_modulations
*mod
)
1722 struct sk_buff
*skb
;
1724 skb
= pn533_alloc_skb(dev
, mod
->len
);
1728 memcpy(skb_put(skb
, mod
->len
), &mod
->data
, mod
->len
);
1733 static int pn533_send_poll_frame(struct pn533
*dev
)
1735 struct pn533_poll_modulations
*mod
;
1736 struct sk_buff
*skb
;
1740 mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1742 nfc_dev_dbg(&dev
->interface
->dev
, "%s mod len %d\n",
1743 __func__
, mod
->len
);
1745 if (mod
->len
== 0) { /* Listen mode */
1746 cmd_code
= PN533_CMD_TG_INIT_AS_TARGET
;
1747 skb
= pn533_alloc_poll_tg_frame(dev
);
1748 } else { /* Polling mode */
1749 cmd_code
= PN533_CMD_IN_LIST_PASSIVE_TARGET
;
1750 skb
= pn533_alloc_poll_in_frame(dev
, mod
);
1754 nfc_dev_err(&dev
->interface
->dev
, "Failed to allocate skb.");
1758 rc
= pn533_send_cmd_async(dev
, cmd_code
, skb
, pn533_poll_complete
,
1762 nfc_dev_err(&dev
->interface
->dev
, "Polling loop error %d", rc
);
1768 static void pn533_wq_poll(struct work_struct
*work
)
1770 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
);
1771 struct pn533_poll_modulations
*cur_mod
;
1774 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1776 nfc_dev_dbg(&dev
->interface
->dev
,
1777 "%s cancel_listen %d modulation len %d",
1778 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1780 if (dev
->cancel_listen
== 1) {
1781 dev
->cancel_listen
= 0;
1782 pn533_abort_cmd(dev
, GFP_ATOMIC
);
1785 rc
= pn533_send_poll_frame(dev
);
1789 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1790 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1795 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1796 u32 im_protocols
, u32 tm_protocols
)
1798 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1800 nfc_dev_dbg(&dev
->interface
->dev
,
1801 "%s: im protocols 0x%x tm protocols 0x%x",
1802 __func__
, im_protocols
, tm_protocols
);
1804 if (dev
->tgt_active_prot
) {
1805 nfc_dev_err(&dev
->interface
->dev
,
1806 "Cannot poll with a target already activated");
1810 if (dev
->tgt_mode
) {
1811 nfc_dev_err(&dev
->interface
->dev
,
1812 "Cannot poll while already being activated");
1817 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1818 if (dev
->gb
== NULL
)
1822 dev
->poll_mod_curr
= 0;
1823 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1824 dev
->poll_protocols
= im_protocols
;
1825 dev
->listen_protocols
= tm_protocols
;
1827 return pn533_send_poll_frame(dev
);
1830 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1832 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1834 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1836 del_timer(&dev
->listen_timer
);
1838 if (!dev
->poll_mod_count
) {
1839 nfc_dev_dbg(&dev
->interface
->dev
,
1840 "Polling operation was not running");
1844 pn533_abort_cmd(dev
, GFP_KERNEL
);
1845 pn533_poll_reset_mod_list(dev
);
1848 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1850 struct pn533_cmd_activate_response
*rsp
;
1854 struct sk_buff
*skb
;
1855 struct sk_buff
*resp
;
1857 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1859 skb
= pn533_alloc_skb(dev
, sizeof(u8
) * 2); /*TG + Next*/
1863 *skb_put(skb
, sizeof(u8
)) = 1; /* TG */
1864 *skb_put(skb
, sizeof(u8
)) = 0; /* Next */
1866 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_ATR
, skb
);
1868 return PTR_ERR(resp
);
1870 rsp
= (struct pn533_cmd_activate_response
*)resp
->data
;
1871 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1872 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1873 nfc_dev_err(&dev
->interface
->dev
,
1874 "Target activation failed (error 0x%x)", rc
);
1875 dev_kfree_skb(resp
);
1879 /* ATR_RES general bytes are located at offset 16 */
1880 gt_len
= resp
->len
- 16;
1881 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, rsp
->gt
, gt_len
);
1883 dev_kfree_skb(resp
);
1887 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1888 struct nfc_target
*target
, u32 protocol
)
1890 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1893 nfc_dev_dbg(&dev
->interface
->dev
, "%s - protocol=%u", __func__
,
1896 if (dev
->poll_mod_count
) {
1897 nfc_dev_err(&dev
->interface
->dev
,
1898 "Cannot activate while polling");
1902 if (dev
->tgt_active_prot
) {
1903 nfc_dev_err(&dev
->interface
->dev
,
1904 "There is already an active target");
1908 if (!dev
->tgt_available_prots
) {
1909 nfc_dev_err(&dev
->interface
->dev
,
1910 "There is no available target to activate");
1914 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1915 nfc_dev_err(&dev
->interface
->dev
,
1916 "Target doesn't support requested proto %u",
1921 if (protocol
== NFC_PROTO_NFC_DEP
) {
1922 rc
= pn533_activate_target_nfcdep(dev
);
1924 nfc_dev_err(&dev
->interface
->dev
,
1925 "Activating target with DEP failed %d", rc
);
1930 dev
->tgt_active_prot
= protocol
;
1931 dev
->tgt_available_prots
= 0;
1936 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1937 struct nfc_target
*target
)
1939 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1941 struct sk_buff
*skb
;
1942 struct sk_buff
*resp
;
1946 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
1948 if (!dev
->tgt_active_prot
) {
1949 nfc_dev_err(&dev
->interface
->dev
, "There is no active target");
1953 dev
->tgt_active_prot
= 0;
1954 skb_queue_purge(&dev
->resp_q
);
1956 skb
= pn533_alloc_skb(dev
, sizeof(u8
));
1960 *skb_put(skb
, 1) = 1; /* TG*/
1962 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_RELEASE
, skb
);
1966 rc
= resp
->data
[0] & PN533_CMD_RET_MASK
;
1967 if (rc
!= PN533_CMD_RET_SUCCESS
)
1968 nfc_dev_err(&dev
->interface
->dev
,
1969 "Error 0x%x when releasing the target", rc
);
1971 dev_kfree_skb(resp
);
1976 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1977 struct sk_buff
*resp
)
1979 struct pn533_cmd_jump_dep_response
*rsp
;
1982 u8 active
= *(u8
*)arg
;
1987 return PTR_ERR(resp
);
1989 if (dev
->tgt_available_prots
&&
1990 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1991 nfc_dev_err(&dev
->interface
->dev
,
1992 "The target does not support DEP");
1997 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1999 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
2000 if (rc
!= PN533_CMD_RET_SUCCESS
) {
2001 nfc_dev_err(&dev
->interface
->dev
,
2002 "Bringing DEP link up failed (error 0x%x)", rc
);
2006 if (!dev
->tgt_available_prots
) {
2007 struct nfc_target nfc_target
;
2009 nfc_dev_dbg(&dev
->interface
->dev
, "Creating new target");
2011 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
2012 nfc_target
.nfcid1_len
= 10;
2013 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
2014 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
2018 dev
->tgt_available_prots
= 0;
2021 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
2023 /* ATR_RES general bytes are located at offset 17 */
2024 target_gt_len
= resp
->len
- 17;
2025 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
2026 rsp
->gt
, target_gt_len
);
2028 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
2029 dev
->nfc_dev
->targets
[0].idx
,
2030 !active
, NFC_RF_INITIATOR
);
2033 dev_kfree_skb(resp
);
2037 static int pn533_mod_to_baud(struct pn533
*dev
)
2039 switch (dev
->poll_mod_curr
) {
2040 case PN533_POLL_MOD_106KBPS_A
:
2042 case PN533_POLL_MOD_212KBPS_FELICA
:
2044 case PN533_POLL_MOD_424KBPS_FELICA
:
2051 #define PASSIVE_DATA_LEN 5
2052 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
2053 u8 comm_mode
, u8
*gb
, size_t gb_len
)
2055 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2056 struct sk_buff
*skb
;
2057 int rc
, baud
, skb_len
;
2060 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
2062 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2064 if (dev
->poll_mod_count
) {
2065 nfc_dev_err(&dev
->interface
->dev
,
2066 "Cannot bring the DEP link up while polling");
2070 if (dev
->tgt_active_prot
) {
2071 nfc_dev_err(&dev
->interface
->dev
,
2072 "There is already an active target");
2076 baud
= pn533_mod_to_baud(dev
);
2078 nfc_dev_err(&dev
->interface
->dev
,
2079 "Invalid curr modulation %d", dev
->poll_mod_curr
);
2083 skb_len
= 3 + gb_len
; /* ActPass + BR + Next */
2084 if (comm_mode
== NFC_COMM_PASSIVE
)
2085 skb_len
+= PASSIVE_DATA_LEN
;
2087 skb
= pn533_alloc_skb(dev
, skb_len
);
2091 *skb_put(skb
, 1) = !comm_mode
; /* ActPass */
2092 *skb_put(skb
, 1) = baud
; /* Baud rate */
2094 next
= skb_put(skb
, 1); /* Next */
2097 if (comm_mode
== NFC_COMM_PASSIVE
&& baud
> 0) {
2098 memcpy(skb_put(skb
, PASSIVE_DATA_LEN
), passive_data
,
2103 if (gb
!= NULL
&& gb_len
> 0) {
2104 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
2105 *next
|= 4; /* We have some Gi */
2110 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2118 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
2119 pn533_in_dep_link_up_complete
, arg
);
2129 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
2131 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2133 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2135 pn533_poll_reset_mod_list(dev
);
2137 if (dev
->tgt_mode
|| dev
->tgt_active_prot
)
2138 pn533_abort_cmd(dev
, GFP_KERNEL
);
2140 dev
->tgt_active_prot
= 0;
2143 skb_queue_purge(&dev
->resp_q
);
2148 struct pn533_data_exchange_arg
{
2149 data_exchange_cb_t cb
;
2153 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
2155 struct sk_buff
*skb
, *tmp
, *t
;
2156 unsigned int skb_len
= 0, tmp_len
= 0;
2158 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2160 if (skb_queue_empty(&dev
->resp_q
))
2163 if (skb_queue_len(&dev
->resp_q
) == 1) {
2164 skb
= skb_dequeue(&dev
->resp_q
);
2168 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
2169 skb_len
+= tmp
->len
;
2171 nfc_dev_dbg(&dev
->interface
->dev
, "%s total length %d\n",
2174 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
2178 skb_put(skb
, skb_len
);
2180 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
2181 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
2182 tmp_len
+= tmp
->len
;
2186 skb_queue_purge(&dev
->resp_q
);
2191 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
2192 struct sk_buff
*resp
)
2194 struct pn533_data_exchange_arg
*arg
= _arg
;
2195 struct sk_buff
*skb
;
2199 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2206 status
= resp
->data
[0];
2207 ret
= status
& PN533_CMD_RET_MASK
;
2208 mi
= status
& PN533_CMD_MI_MASK
;
2210 skb_pull(resp
, sizeof(status
));
2212 if (ret
!= PN533_CMD_RET_SUCCESS
) {
2213 nfc_dev_err(&dev
->interface
->dev
,
2214 "Exchanging data failed (error 0x%x)", ret
);
2219 skb_queue_tail(&dev
->resp_q
, resp
);
2222 dev
->cmd_complete_mi_arg
= arg
;
2223 queue_work(dev
->wq
, &dev
->mi_work
);
2224 return -EINPROGRESS
;
2227 skb
= pn533_build_response(dev
);
2231 arg
->cb(arg
->cb_context
, skb
, 0);
2236 dev_kfree_skb(resp
);
2238 skb_queue_purge(&dev
->resp_q
);
2239 arg
->cb(arg
->cb_context
, NULL
, rc
);
2244 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
2245 struct nfc_target
*target
, struct sk_buff
*skb
,
2246 data_exchange_cb_t cb
, void *cb_context
)
2248 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2249 struct pn533_data_exchange_arg
*arg
= NULL
;
2252 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2254 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2255 /* TODO: Implement support to multi-part data exchange */
2256 nfc_dev_err(&dev
->interface
->dev
,
2257 "Data length greater than the max allowed: %d",
2258 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
2263 if (!dev
->tgt_active_prot
) {
2264 nfc_dev_err(&dev
->interface
->dev
,
2265 "Can't exchange data if there is no active target");
2270 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2277 arg
->cb_context
= cb_context
;
2279 switch (dev
->device_type
) {
2280 case PN533_DEVICE_PASORI
:
2281 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2282 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_COMM_THRU
,
2284 pn533_data_exchange_complete
,
2290 *skb_push(skb
, sizeof(u8
)) = 1; /*TG*/
2292 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_DATA_EXCHANGE
,
2293 skb
, pn533_data_exchange_complete
,
2299 if (rc
< 0) /* rc from send_async */
2310 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2311 struct sk_buff
*resp
)
2315 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2318 return PTR_ERR(resp
);
2320 status
= resp
->data
[0];
2322 dev_kfree_skb(resp
);
2325 nfc_tm_deactivated(dev
->nfc_dev
);
2332 queue_work(dev
->wq
, &dev
->tg_work
);
2337 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2339 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2342 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2344 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2345 nfc_dev_err(&dev
->interface
->dev
,
2346 "Data length greater than the max allowed: %d",
2347 PN533_CMD_DATAEXCH_DATA_MAXLEN
);
2351 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_DATA
, skb
,
2352 pn533_tm_send_complete
, NULL
);
2359 static void pn533_wq_mi_recv(struct work_struct
*work
)
2361 struct pn533
*dev
= container_of(work
, struct pn533
, mi_work
);
2363 struct sk_buff
*skb
;
2366 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2368 skb
= pn533_alloc_skb(dev
, PN533_CMD_DATAEXCH_HEAD_LEN
);
2372 switch (dev
->device_type
) {
2373 case PN533_DEVICE_PASORI
:
2374 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2375 rc
= pn533_send_cmd_direct_async(dev
,
2376 PN533_CMD_IN_COMM_THRU
,
2378 pn533_data_exchange_complete
,
2379 dev
->cmd_complete_mi_arg
);
2384 *skb_put(skb
, sizeof(u8
)) = 1; /*TG*/
2386 rc
= pn533_send_cmd_direct_async(dev
,
2387 PN533_CMD_IN_DATA_EXCHANGE
,
2389 pn533_data_exchange_complete
,
2390 dev
->cmd_complete_mi_arg
);
2395 if (rc
== 0) /* success */
2398 nfc_dev_err(&dev
->interface
->dev
,
2399 "Error %d when trying to perform data_exchange", rc
);
2402 kfree(dev
->cmd_complete_mi_arg
);
2405 pn533_send_ack(dev
, GFP_KERNEL
);
2406 queue_work(dev
->wq
, &dev
->cmd_work
);
2409 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2412 struct sk_buff
*skb
;
2413 struct sk_buff
*resp
;
2417 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2419 skb_len
= sizeof(cfgitem
) + cfgdata_len
; /* cfgitem + cfgdata */
2421 skb
= pn533_alloc_skb(dev
, skb_len
);
2425 *skb_put(skb
, sizeof(cfgitem
)) = cfgitem
;
2426 memcpy(skb_put(skb
, cfgdata_len
), cfgdata
, cfgdata_len
);
2428 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_RF_CONFIGURATION
, skb
);
2430 return PTR_ERR(resp
);
2432 dev_kfree_skb(resp
);
2436 static int pn533_get_firmware_version(struct pn533
*dev
,
2437 struct pn533_fw_version
*fv
)
2439 struct sk_buff
*skb
;
2440 struct sk_buff
*resp
;
2442 skb
= pn533_alloc_skb(dev
, 0);
2446 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_GET_FIRMWARE_VERSION
, skb
);
2448 return PTR_ERR(resp
);
2450 fv
->ic
= resp
->data
[0];
2451 fv
->ver
= resp
->data
[1];
2452 fv
->rev
= resp
->data
[2];
2453 fv
->support
= resp
->data
[3];
2455 dev_kfree_skb(resp
);
2459 static int pn533_pasori_fw_reset(struct pn533
*dev
)
2461 struct sk_buff
*skb
;
2462 struct sk_buff
*resp
;
2464 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2466 skb
= pn533_alloc_skb(dev
, sizeof(u8
));
2470 *skb_put(skb
, sizeof(u8
)) = 0x1;
2472 resp
= pn533_send_cmd_sync(dev
, 0x18, skb
);
2474 return PTR_ERR(resp
);
2476 dev_kfree_skb(resp
);
2481 struct pn533_acr122_poweron_rdr_arg
{
2483 struct completion done
;
2486 static void pn533_acr122_poweron_rdr_resp(struct urb
*urb
)
2488 struct pn533_acr122_poweron_rdr_arg
*arg
= urb
->context
;
2490 nfc_dev_dbg(&urb
->dev
->dev
, "%s", __func__
);
2492 print_hex_dump(KERN_ERR
, "ACR122 RX: ", DUMP_PREFIX_NONE
, 16, 1,
2493 urb
->transfer_buffer
, urb
->transfer_buffer_length
,
2496 arg
->rc
= urb
->status
;
2497 complete(&arg
->done
);
2500 static int pn533_acr122_poweron_rdr(struct pn533
*dev
)
2502 /* Power on th reader (CCID cmd) */
2503 u8 cmd
[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON
,
2504 0, 0, 0, 0, 0, 0, 3, 0, 0};
2508 struct pn533_acr122_poweron_rdr_arg arg
;
2510 nfc_dev_dbg(&dev
->interface
->dev
, "%s", __func__
);
2512 init_completion(&arg
.done
);
2513 cntx
= dev
->in_urb
->context
; /* backup context */
2515 dev
->in_urb
->transfer_buffer
= buf
;
2516 dev
->in_urb
->transfer_buffer_length
= 255;
2517 dev
->in_urb
->complete
= pn533_acr122_poweron_rdr_resp
;
2518 dev
->in_urb
->context
= &arg
;
2520 dev
->out_urb
->transfer_buffer
= cmd
;
2521 dev
->out_urb
->transfer_buffer_length
= sizeof(cmd
);
2523 print_hex_dump(KERN_ERR
, "ACR122 TX: ", DUMP_PREFIX_NONE
, 16, 1,
2524 cmd
, sizeof(cmd
), false);
2526 rc
= usb_submit_urb(dev
->out_urb
, GFP_KERNEL
);
2528 nfc_dev_err(&dev
->interface
->dev
,
2529 "Reader power on cmd error %d", rc
);
2533 rc
= usb_submit_urb(dev
->in_urb
, GFP_KERNEL
);
2535 nfc_dev_err(&dev
->interface
->dev
,
2536 "Can't submit for reader power on cmd response %d",
2541 wait_for_completion(&arg
.done
);
2542 dev
->in_urb
->context
= cntx
; /* restore context */
2547 static int pn533_rf_field(struct nfc_dev
*nfc_dev
, u8 rf
)
2549 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2553 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_RF_FIELD
,
2554 (u8
*)&rf_field
, 1);
2556 nfc_dev_err(&dev
->interface
->dev
,
2557 "Error on setting RF field");
2564 int pn533_dev_up(struct nfc_dev
*nfc_dev
)
2566 return pn533_rf_field(nfc_dev
, 1);
2569 int pn533_dev_down(struct nfc_dev
*nfc_dev
)
2571 return pn533_rf_field(nfc_dev
, 0);
2574 static struct nfc_ops pn533_nfc_ops
= {
2575 .dev_up
= pn533_dev_up
,
2576 .dev_down
= pn533_dev_down
,
2577 .dep_link_up
= pn533_dep_link_up
,
2578 .dep_link_down
= pn533_dep_link_down
,
2579 .start_poll
= pn533_start_poll
,
2580 .stop_poll
= pn533_stop_poll
,
2581 .activate_target
= pn533_activate_target
,
2582 .deactivate_target
= pn533_deactivate_target
,
2583 .im_transceive
= pn533_transceive
,
2584 .tm_send
= pn533_tm_send
,
2587 static int pn533_setup(struct pn533
*dev
)
2589 struct pn533_config_max_retries max_retries
;
2590 struct pn533_config_timing timing
;
2591 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2594 switch (dev
->device_type
) {
2595 case PN533_DEVICE_STD
:
2596 max_retries
.mx_rty_atr
= PN533_CONFIG_MAX_RETRIES_ENDLESS
;
2597 max_retries
.mx_rty_psl
= 2;
2598 max_retries
.mx_rty_passive_act
=
2599 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2601 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2602 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_204
;
2603 timing
.dep_timeout
= PN533_CONFIG_TIMING_409
;
2607 case PN533_DEVICE_PASORI
:
2608 case PN533_DEVICE_ACR122U
:
2609 max_retries
.mx_rty_atr
= 0x2;
2610 max_retries
.mx_rty_psl
= 0x1;
2611 max_retries
.mx_rty_passive_act
=
2612 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2614 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2615 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2616 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2621 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2626 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2627 (u8
*)&max_retries
, sizeof(max_retries
));
2629 nfc_dev_err(&dev
->interface
->dev
,
2630 "Error on setting MAX_RETRIES config");
2635 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2636 (u8
*)&timing
, sizeof(timing
));
2638 nfc_dev_err(&dev
->interface
->dev
,
2639 "Error on setting RF timings");
2643 switch (dev
->device_type
) {
2644 case PN533_DEVICE_STD
:
2647 case PN533_DEVICE_PASORI
:
2648 pn533_pasori_fw_reset(dev
);
2650 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2653 nfc_dev_err(&dev
->interface
->dev
,
2654 "Error while settings PASORI config");
2658 pn533_pasori_fw_reset(dev
);
2666 static int pn533_probe(struct usb_interface
*interface
,
2667 const struct usb_device_id
*id
)
2669 struct pn533_fw_version fw_ver
;
2671 struct usb_host_interface
*iface_desc
;
2672 struct usb_endpoint_descriptor
*endpoint
;
2673 int in_endpoint
= 0;
2674 int out_endpoint
= 0;
2679 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2683 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
2684 dev
->interface
= interface
;
2685 mutex_init(&dev
->cmd_lock
);
2687 iface_desc
= interface
->cur_altsetting
;
2688 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
2689 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2691 if (!in_endpoint
&& usb_endpoint_is_bulk_in(endpoint
))
2692 in_endpoint
= endpoint
->bEndpointAddress
;
2694 if (!out_endpoint
&& usb_endpoint_is_bulk_out(endpoint
))
2695 out_endpoint
= endpoint
->bEndpointAddress
;
2698 if (!in_endpoint
|| !out_endpoint
) {
2699 nfc_dev_err(&interface
->dev
,
2700 "Could not find bulk-in or bulk-out endpoint");
2705 dev
->in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2706 dev
->out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2708 if (!dev
->in_urb
|| !dev
->out_urb
)
2711 usb_fill_bulk_urb(dev
->in_urb
, dev
->udev
,
2712 usb_rcvbulkpipe(dev
->udev
, in_endpoint
),
2713 NULL
, 0, NULL
, dev
);
2714 usb_fill_bulk_urb(dev
->out_urb
, dev
->udev
,
2715 usb_sndbulkpipe(dev
->udev
, out_endpoint
),
2716 NULL
, 0, pn533_send_complete
, dev
);
2718 INIT_WORK(&dev
->cmd_work
, pn533_wq_cmd
);
2719 INIT_WORK(&dev
->cmd_complete_work
, pn533_wq_cmd_complete
);
2720 INIT_WORK(&dev
->mi_work
, pn533_wq_mi_recv
);
2721 INIT_WORK(&dev
->tg_work
, pn533_wq_tg_get_data
);
2722 INIT_WORK(&dev
->poll_work
, pn533_wq_poll
);
2723 dev
->wq
= alloc_ordered_workqueue("pn533", 0);
2724 if (dev
->wq
== NULL
)
2727 init_timer(&dev
->listen_timer
);
2728 dev
->listen_timer
.data
= (unsigned long) dev
;
2729 dev
->listen_timer
.function
= pn533_listen_mode_timer
;
2731 skb_queue_head_init(&dev
->resp_q
);
2733 INIT_LIST_HEAD(&dev
->cmd_queue
);
2735 usb_set_intfdata(interface
, dev
);
2737 dev
->ops
= &pn533_std_frame_ops
;
2739 dev
->protocol_type
= PN533_PROTO_REQ_ACK_RESP
;
2740 dev
->device_type
= id
->driver_info
;
2741 switch (dev
->device_type
) {
2742 case PN533_DEVICE_STD
:
2743 protocols
= PN533_ALL_PROTOCOLS
;
2746 case PN533_DEVICE_PASORI
:
2747 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
2750 case PN533_DEVICE_ACR122U
:
2751 protocols
= PN533_NO_TYPE_B_PROTOCOLS
;
2752 dev
->ops
= &pn533_acr122_frame_ops
;
2753 dev
->protocol_type
= PN533_PROTO_REQ_RESP
,
2755 rc
= pn533_acr122_poweron_rdr(dev
);
2757 nfc_dev_err(&dev
->interface
->dev
,
2758 "Couldn't poweron the reader (error %d)",
2765 nfc_dev_err(&dev
->interface
->dev
, "Unknown device type %d\n",
2771 memset(&fw_ver
, 0, sizeof(fw_ver
));
2772 rc
= pn533_get_firmware_version(dev
, &fw_ver
);
2776 nfc_dev_info(&dev
->interface
->dev
,
2777 "NXP PN533 firmware ver %d.%d now attached",
2778 fw_ver
.ver
, fw_ver
.rev
);
2781 dev
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2783 dev
->ops
->tx_header_len
+
2784 PN533_CMD_DATAEXCH_HEAD_LEN
,
2785 dev
->ops
->tx_tail_len
);
2789 nfc_set_parent_dev(dev
->nfc_dev
, &interface
->dev
);
2790 nfc_set_drvdata(dev
->nfc_dev
, dev
);
2792 rc
= nfc_register_device(dev
->nfc_dev
);
2796 rc
= pn533_setup(dev
);
2798 goto unregister_nfc_dev
;
2803 nfc_unregister_device(dev
->nfc_dev
);
2806 nfc_free_device(dev
->nfc_dev
);
2809 destroy_workqueue(dev
->wq
);
2811 usb_free_urb(dev
->in_urb
);
2812 usb_free_urb(dev
->out_urb
);
2813 usb_put_dev(dev
->udev
);
2818 static void pn533_disconnect(struct usb_interface
*interface
)
2821 struct pn533_cmd
*cmd
, *n
;
2823 dev
= usb_get_intfdata(interface
);
2824 usb_set_intfdata(interface
, NULL
);
2826 nfc_unregister_device(dev
->nfc_dev
);
2827 nfc_free_device(dev
->nfc_dev
);
2829 usb_kill_urb(dev
->in_urb
);
2830 usb_kill_urb(dev
->out_urb
);
2832 destroy_workqueue(dev
->wq
);
2834 skb_queue_purge(&dev
->resp_q
);
2836 del_timer(&dev
->listen_timer
);
2838 list_for_each_entry_safe(cmd
, n
, &dev
->cmd_queue
, queue
) {
2839 list_del(&cmd
->queue
);
2843 usb_free_urb(dev
->in_urb
);
2844 usb_free_urb(dev
->out_urb
);
2847 nfc_dev_info(&interface
->dev
, "NXP PN533 NFC device disconnected");
2850 static struct usb_driver pn533_driver
= {
2852 .probe
= pn533_probe
,
2853 .disconnect
= pn533_disconnect
,
2854 .id_table
= pn533_table
,
2857 module_usb_driver(pn533_driver
);
2859 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2860 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2861 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2862 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION
);
2863 MODULE_VERSION(VERSION
);
2864 MODULE_LICENSE("GPL");