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