1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for NXP PN533 NFC Chip - core functions
5 * Copyright (C) 2011 Instituto Nokia de Tecnologia
6 * Copyright (C) 2012-2013 Tieto Poland
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/nfc.h>
14 #include <linux/netdevice.h>
15 #include <net/nfc/nfc.h>
20 /* How much time we spend listening for initiators */
21 #define PN533_LISTEN_TIME 2
22 /* Delay between each poll frame (ms) */
23 #define PN533_POLL_INTERVAL 10
25 /* structs for pn533 commands */
27 /* PN533_CMD_GET_FIRMWARE_VERSION */
28 struct pn533_fw_version
{
35 /* PN533_CMD_RF_CONFIGURATION */
36 #define PN533_CFGITEM_RF_FIELD 0x01
37 #define PN533_CFGITEM_TIMING 0x02
38 #define PN533_CFGITEM_MAX_RETRIES 0x05
39 #define PN533_CFGITEM_PASORI 0x82
41 #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
42 #define PN533_CFGITEM_RF_FIELD_ON 0x1
43 #define PN533_CFGITEM_RF_FIELD_OFF 0x0
45 #define PN533_CONFIG_TIMING_102 0xb
46 #define PN533_CONFIG_TIMING_204 0xc
47 #define PN533_CONFIG_TIMING_409 0xd
48 #define PN533_CONFIG_TIMING_819 0xe
50 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
51 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
53 struct pn533_config_max_retries
{
56 u8 mx_rty_passive_act
;
59 struct pn533_config_timing
{
65 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
67 /* felica commands opcode */
68 #define PN533_FELICA_OPC_SENSF_REQ 0
69 #define PN533_FELICA_OPC_SENSF_RES 1
70 /* felica SENSF_REQ parameters */
71 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
72 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
73 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
74 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
76 /* type B initiator_data values */
77 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
78 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
79 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
81 union pn533_cmd_poll_initdata
{
94 struct pn533_poll_modulations
{
98 union pn533_cmd_poll_initdata initiator_data
;
103 static const struct pn533_poll_modulations poll_mod
[] = {
104 [PN533_POLL_MOD_106KBPS_A
] = {
111 [PN533_POLL_MOD_212KBPS_FELICA
] = {
115 .initiator_data
.felica
= {
116 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
117 .sc
= PN533_FELICA_SENSF_SC_ALL
,
118 .rc
= PN533_FELICA_SENSF_RC_SYSTEM_CODE
,
124 [PN533_POLL_MOD_424KBPS_FELICA
] = {
128 .initiator_data
.felica
= {
129 .opcode
= PN533_FELICA_OPC_SENSF_REQ
,
130 .sc
= PN533_FELICA_SENSF_SC_ALL
,
131 .rc
= PN533_FELICA_SENSF_RC_SYSTEM_CODE
,
137 [PN533_POLL_MOD_106KBPS_JEWEL
] = {
144 [PN533_POLL_MOD_847KBPS_B
] = {
148 .initiator_data
.type_b
= {
149 .afi
= PN533_TYPE_B_AFI_ALL_FAMILIES
,
151 PN533_TYPE_B_POLL_METHOD_TIMESLOT
,
156 [PN533_LISTEN_MOD
] = {
161 /* PN533_CMD_IN_ATR */
163 struct pn533_cmd_activate_response
{
175 struct pn533_cmd_jump_dep_response
{
188 struct pn532_autopoll_resp
{
195 /* PN532_CMD_IN_AUTOPOLL */
196 #define PN532_AUTOPOLL_POLLNR_INFINITE 0xff
197 #define PN532_AUTOPOLL_PERIOD 0x03 /* in units of 150 ms */
199 #define PN532_AUTOPOLL_TYPE_GENERIC_106 0x00
200 #define PN532_AUTOPOLL_TYPE_GENERIC_212 0x01
201 #define PN532_AUTOPOLL_TYPE_GENERIC_424 0x02
202 #define PN532_AUTOPOLL_TYPE_JEWEL 0x04
203 #define PN532_AUTOPOLL_TYPE_MIFARE 0x10
204 #define PN532_AUTOPOLL_TYPE_FELICA212 0x11
205 #define PN532_AUTOPOLL_TYPE_FELICA424 0x12
206 #define PN532_AUTOPOLL_TYPE_ISOA 0x20
207 #define PN532_AUTOPOLL_TYPE_ISOB 0x23
208 #define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106 0x40
209 #define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212 0x41
210 #define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424 0x42
211 #define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106 0x80
212 #define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_212 0x81
213 #define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_424 0x82
215 /* PN533_TG_INIT_AS_TARGET */
216 #define PN533_INIT_TARGET_PASSIVE 0x1
217 #define PN533_INIT_TARGET_DEP 0x2
219 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
220 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
221 #define PN533_INIT_TARGET_RESP_DEP 0x4
223 /* The rule: value(high byte) + value(low byte) + checksum = 0 */
224 static inline u8
pn533_ext_checksum(u16 value
)
226 return ~(u8
)(((value
& 0xFF00) >> 8) + (u8
)(value
& 0xFF)) + 1;
229 /* The rule: value + checksum = 0 */
230 static inline u8
pn533_std_checksum(u8 value
)
235 /* The rule: sum(data elements) + checksum = 0 */
236 static u8
pn533_std_data_checksum(u8
*data
, int datalen
)
241 for (i
= 0; i
< datalen
; i
++)
244 return pn533_std_checksum(sum
);
247 static void pn533_std_tx_frame_init(void *_frame
, u8 cmd_code
)
249 struct pn533_std_frame
*frame
= _frame
;
252 frame
->start_frame
= cpu_to_be16(PN533_STD_FRAME_SOF
);
253 PN533_STD_FRAME_IDENTIFIER(frame
) = PN533_STD_FRAME_DIR_OUT
;
254 PN533_FRAME_CMD(frame
) = cmd_code
;
258 static void pn533_std_tx_frame_finish(void *_frame
)
260 struct pn533_std_frame
*frame
= _frame
;
262 frame
->datalen_checksum
= pn533_std_checksum(frame
->datalen
);
264 PN533_STD_FRAME_CHECKSUM(frame
) =
265 pn533_std_data_checksum(frame
->data
, frame
->datalen
);
267 PN533_STD_FRAME_POSTAMBLE(frame
) = 0;
270 static void pn533_std_tx_update_payload_len(void *_frame
, int len
)
272 struct pn533_std_frame
*frame
= _frame
;
274 frame
->datalen
+= len
;
277 static bool pn533_std_rx_frame_is_valid(void *_frame
, struct pn533
*dev
)
280 struct pn533_std_frame
*stdf
= _frame
;
282 if (stdf
->start_frame
!= cpu_to_be16(PN533_STD_FRAME_SOF
))
285 if (likely(!PN533_STD_IS_EXTENDED(stdf
))) {
286 /* Standard frame code */
287 dev
->ops
->rx_header_len
= PN533_STD_FRAME_HEADER_LEN
;
289 checksum
= pn533_std_checksum(stdf
->datalen
);
290 if (checksum
!= stdf
->datalen_checksum
)
293 checksum
= pn533_std_data_checksum(stdf
->data
, stdf
->datalen
);
294 if (checksum
!= PN533_STD_FRAME_CHECKSUM(stdf
))
298 struct pn533_ext_frame
*eif
= _frame
;
300 dev
->ops
->rx_header_len
= PN533_EXT_FRAME_HEADER_LEN
;
302 checksum
= pn533_ext_checksum(be16_to_cpu(eif
->datalen
));
303 if (checksum
!= eif
->datalen_checksum
)
306 /* check data checksum */
307 checksum
= pn533_std_data_checksum(eif
->data
,
308 be16_to_cpu(eif
->datalen
));
309 if (checksum
!= PN533_EXT_FRAME_CHECKSUM(eif
))
316 bool pn533_rx_frame_is_ack(void *_frame
)
318 struct pn533_std_frame
*frame
= _frame
;
320 if (frame
->start_frame
!= cpu_to_be16(PN533_STD_FRAME_SOF
))
323 if (frame
->datalen
!= 0 || frame
->datalen_checksum
!= 0xFF)
328 EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack
);
330 static inline int pn533_std_rx_frame_size(void *frame
)
332 struct pn533_std_frame
*f
= frame
;
334 /* check for Extended Information frame */
335 if (PN533_STD_IS_EXTENDED(f
)) {
336 struct pn533_ext_frame
*eif
= frame
;
338 return sizeof(struct pn533_ext_frame
)
339 + be16_to_cpu(eif
->datalen
) + PN533_STD_FRAME_TAIL_LEN
;
342 return sizeof(struct pn533_std_frame
) + f
->datalen
+
343 PN533_STD_FRAME_TAIL_LEN
;
346 static u8
pn533_std_get_cmd_code(void *frame
)
348 struct pn533_std_frame
*f
= frame
;
349 struct pn533_ext_frame
*eif
= frame
;
351 if (PN533_STD_IS_EXTENDED(f
))
352 return PN533_FRAME_CMD(eif
);
354 return PN533_FRAME_CMD(f
);
357 bool pn533_rx_frame_is_cmd_response(struct pn533
*dev
, void *frame
)
359 return (dev
->ops
->get_cmd_code(frame
) ==
360 PN533_CMD_RESPONSE(dev
->cmd
->code
));
362 EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response
);
365 static struct pn533_frame_ops pn533_std_frame_ops
= {
366 .tx_frame_init
= pn533_std_tx_frame_init
,
367 .tx_frame_finish
= pn533_std_tx_frame_finish
,
368 .tx_update_payload_len
= pn533_std_tx_update_payload_len
,
369 .tx_header_len
= PN533_STD_FRAME_HEADER_LEN
,
370 .tx_tail_len
= PN533_STD_FRAME_TAIL_LEN
,
372 .rx_is_frame_valid
= pn533_std_rx_frame_is_valid
,
373 .rx_frame_size
= pn533_std_rx_frame_size
,
374 .rx_header_len
= PN533_STD_FRAME_HEADER_LEN
,
375 .rx_tail_len
= PN533_STD_FRAME_TAIL_LEN
,
377 .max_payload_len
= PN533_STD_FRAME_MAX_PAYLOAD_LEN
,
378 .get_cmd_code
= pn533_std_get_cmd_code
,
381 static void pn533_build_cmd_frame(struct pn533
*dev
, u8 cmd_code
,
384 /* payload is already there, just update datalen */
385 int payload_len
= skb
->len
;
386 struct pn533_frame_ops
*ops
= dev
->ops
;
389 skb_push(skb
, ops
->tx_header_len
);
390 skb_put(skb
, ops
->tx_tail_len
);
392 ops
->tx_frame_init(skb
->data
, cmd_code
);
393 ops
->tx_update_payload_len(skb
->data
, payload_len
);
394 ops
->tx_frame_finish(skb
->data
);
397 static int pn533_send_async_complete(struct pn533
*dev
)
399 struct pn533_cmd
*cmd
= dev
->cmd
;
400 struct sk_buff
*resp
;
404 dev_dbg(dev
->dev
, "%s: cmd not set\n", __func__
);
408 dev_kfree_skb(cmd
->req
);
410 status
= cmd
->status
;
414 rc
= cmd
->complete_cb(dev
, cmd
->complete_cb_context
,
420 /* when no response is set we got interrupted */
422 resp
= ERR_PTR(-EINTR
);
425 skb_pull(resp
, dev
->ops
->rx_header_len
);
426 skb_trim(resp
, resp
->len
- dev
->ops
->rx_tail_len
);
429 rc
= cmd
->complete_cb(dev
, cmd
->complete_cb_context
, resp
);
437 static int __pn533_send_async(struct pn533
*dev
, u8 cmd_code
,
439 pn533_send_async_complete_t complete_cb
,
440 void *complete_cb_context
)
442 struct pn533_cmd
*cmd
;
445 dev_dbg(dev
->dev
, "Sending command 0x%x\n", cmd_code
);
447 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
451 cmd
->code
= cmd_code
;
453 cmd
->complete_cb
= complete_cb
;
454 cmd
->complete_cb_context
= complete_cb_context
;
456 pn533_build_cmd_frame(dev
, cmd_code
, req
);
458 mutex_lock(&dev
->cmd_lock
);
460 if (!dev
->cmd_pending
) {
462 rc
= dev
->phy_ops
->send_frame(dev
, req
);
468 dev
->cmd_pending
= 1;
472 dev_dbg(dev
->dev
, "%s Queueing command 0x%x\n",
475 INIT_LIST_HEAD(&cmd
->queue
);
476 list_add_tail(&cmd
->queue
, &dev
->cmd_queue
);
483 mutex_unlock(&dev
->cmd_lock
);
487 static int pn533_send_data_async(struct pn533
*dev
, u8 cmd_code
,
489 pn533_send_async_complete_t complete_cb
,
490 void *complete_cb_context
)
494 rc
= __pn533_send_async(dev
, cmd_code
, req
, complete_cb
,
495 complete_cb_context
);
500 static int pn533_send_cmd_async(struct pn533
*dev
, u8 cmd_code
,
502 pn533_send_async_complete_t complete_cb
,
503 void *complete_cb_context
)
507 rc
= __pn533_send_async(dev
, cmd_code
, req
, complete_cb
,
508 complete_cb_context
);
514 * pn533_send_cmd_direct_async
516 * The function sends a piority cmd directly to the chip omitting the cmd
517 * queue. It's intended to be used by chaining mechanism of received responses
518 * where the host has to request every single chunk of data before scheduling
519 * next cmd from the queue.
521 static int pn533_send_cmd_direct_async(struct pn533
*dev
, u8 cmd_code
,
523 pn533_send_async_complete_t complete_cb
,
524 void *complete_cb_context
)
526 struct pn533_cmd
*cmd
;
529 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
533 cmd
->code
= cmd_code
;
535 cmd
->complete_cb
= complete_cb
;
536 cmd
->complete_cb_context
= complete_cb_context
;
538 pn533_build_cmd_frame(dev
, cmd_code
, req
);
541 rc
= dev
->phy_ops
->send_frame(dev
, req
);
550 static void pn533_wq_cmd_complete(struct work_struct
*work
)
552 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_complete_work
);
555 rc
= pn533_send_async_complete(dev
);
556 if (rc
!= -EINPROGRESS
)
557 queue_work(dev
->wq
, &dev
->cmd_work
);
560 static void pn533_wq_cmd(struct work_struct
*work
)
562 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
563 struct pn533_cmd
*cmd
;
566 mutex_lock(&dev
->cmd_lock
);
568 if (list_empty(&dev
->cmd_queue
)) {
569 dev
->cmd_pending
= 0;
570 mutex_unlock(&dev
->cmd_lock
);
574 cmd
= list_first_entry(&dev
->cmd_queue
, struct pn533_cmd
, queue
);
576 list_del(&cmd
->queue
);
578 mutex_unlock(&dev
->cmd_lock
);
581 rc
= dev
->phy_ops
->send_frame(dev
, cmd
->req
);
584 dev_kfree_skb(cmd
->req
);
591 struct pn533_sync_cmd_response
{
592 struct sk_buff
*resp
;
593 struct completion done
;
596 static int pn533_send_sync_complete(struct pn533
*dev
, void *_arg
,
597 struct sk_buff
*resp
)
599 struct pn533_sync_cmd_response
*arg
= _arg
;
602 complete(&arg
->done
);
607 /* pn533_send_cmd_sync
609 * Please note the req parameter is freed inside the function to
610 * limit a number of return value interpretations by the caller.
612 * 1. negative in case of error during TX path -> req should be freed
614 * 2. negative in case of error during RX path -> req should not be freed
615 * as it's been already freed at the beginning of RX path by
618 * 3. valid pointer in case of succesfult RX path
620 * A caller has to check a return value with IS_ERR macro. If the test pass,
621 * the returned pointer is valid.
624 static struct sk_buff
*pn533_send_cmd_sync(struct pn533
*dev
, u8 cmd_code
,
628 struct pn533_sync_cmd_response arg
;
630 init_completion(&arg
.done
);
632 rc
= pn533_send_cmd_async(dev
, cmd_code
, req
,
633 pn533_send_sync_complete
, &arg
);
639 wait_for_completion(&arg
.done
);
644 static struct sk_buff
*pn533_alloc_skb(struct pn533
*dev
, unsigned int size
)
648 skb
= alloc_skb(dev
->ops
->tx_header_len
+
650 dev
->ops
->tx_tail_len
, GFP_KERNEL
);
653 skb_reserve(skb
, dev
->ops
->tx_header_len
);
658 struct pn533_target_type_a
{
666 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
667 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
668 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
670 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
671 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
673 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
674 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
676 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
677 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
678 #define PN533_TYPE_A_SEL_PROT_DEP 2
679 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
681 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
687 if (target_data_len
< sizeof(struct pn533_target_type_a
))
691 * The length check of nfcid[] and ats[] are not being performed because
692 * the values are not being used
695 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
696 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
697 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
699 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
700 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
701 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
702 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
705 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
706 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
712 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
715 struct pn533_target_type_a
*tgt_type_a
;
717 tgt_type_a
= (struct pn533_target_type_a
*)tgt_data
;
719 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
722 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
723 case PN533_TYPE_A_SEL_PROT_MIFARE
:
724 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
726 case PN533_TYPE_A_SEL_PROT_ISO14443
:
727 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
729 case PN533_TYPE_A_SEL_PROT_DEP
:
730 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
732 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
733 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
734 NFC_PROTO_NFC_DEP_MASK
;
738 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
739 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
740 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
741 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
746 struct pn533_target_felica
{
749 u8 nfcid2
[NFC_NFCID2_MAXSIZE
];
755 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
756 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
758 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
761 if (target_data_len
< sizeof(struct pn533_target_felica
))
764 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
770 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
773 struct pn533_target_felica
*tgt_felica
;
775 tgt_felica
= (struct pn533_target_felica
*)tgt_data
;
777 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
780 if ((tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
) &&
781 (tgt_felica
->nfcid2
[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2
))
782 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
784 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
786 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
787 nfc_tgt
->sensf_res_len
= 9;
789 memcpy(nfc_tgt
->nfcid2
, tgt_felica
->nfcid2
, NFC_NFCID2_MAXSIZE
);
790 nfc_tgt
->nfcid2_len
= NFC_NFCID2_MAXSIZE
;
795 struct pn533_target_jewel
{
800 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
806 if (target_data_len
< sizeof(struct pn533_target_jewel
))
809 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
810 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
811 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
813 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
814 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
815 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
816 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
822 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
825 struct pn533_target_jewel
*tgt_jewel
;
827 tgt_jewel
= (struct pn533_target_jewel
*)tgt_data
;
829 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
832 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
833 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
834 nfc_tgt
->nfcid1_len
= 4;
835 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
840 struct pn533_type_b_prot_info
{
846 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
847 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
848 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
850 struct pn533_type_b_sens_res
{
854 struct pn533_type_b_prot_info prot_info
;
857 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
859 struct pn533_target_type_b
{
860 struct pn533_type_b_sens_res sensb_res
;
865 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
868 if (target_data_len
< sizeof(struct pn533_target_type_b
))
871 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
874 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
875 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
881 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
884 struct pn533_target_type_b
*tgt_type_b
;
886 tgt_type_b
= (struct pn533_target_type_b
*)tgt_data
;
888 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
891 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
896 static void pn533_poll_reset_mod_list(struct pn533
*dev
);
897 static int pn533_target_found(struct pn533
*dev
, u8 tg
, u8
*tgdata
,
900 struct nfc_target nfc_tgt
;
903 dev_dbg(dev
->dev
, "%s: modulation=%d\n",
904 __func__
, dev
->poll_mod_curr
);
909 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
911 switch (dev
->poll_mod_curr
) {
912 case PN533_POLL_MOD_106KBPS_A
:
913 rc
= pn533_target_found_type_a(&nfc_tgt
, tgdata
, tgdata_len
);
915 case PN533_POLL_MOD_212KBPS_FELICA
:
916 case PN533_POLL_MOD_424KBPS_FELICA
:
917 rc
= pn533_target_found_felica(&nfc_tgt
, tgdata
, tgdata_len
);
919 case PN533_POLL_MOD_106KBPS_JEWEL
:
920 rc
= pn533_target_found_jewel(&nfc_tgt
, tgdata
, tgdata_len
);
922 case PN533_POLL_MOD_847KBPS_B
:
923 rc
= pn533_target_found_type_b(&nfc_tgt
, tgdata
, tgdata_len
);
927 "Unknown current poll modulation\n");
934 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
936 "The Tg found doesn't have the desired protocol\n");
941 "Target found - supported protocols: 0x%x\n",
942 nfc_tgt
.supported_protocols
);
944 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
946 pn533_poll_reset_mod_list(dev
);
947 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
952 static inline void pn533_poll_next_mod(struct pn533
*dev
)
954 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
957 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
959 dev
->poll_mod_count
= 0;
962 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
964 dev
->poll_mod_active
[dev
->poll_mod_count
] =
965 (struct pn533_poll_modulations
*)&poll_mod
[mod_index
];
966 dev
->poll_mod_count
++;
969 static void pn533_poll_create_mod_list(struct pn533
*dev
,
970 u32 im_protocols
, u32 tm_protocols
)
972 pn533_poll_reset_mod_list(dev
);
974 if ((im_protocols
& NFC_PROTO_MIFARE_MASK
) ||
975 (im_protocols
& NFC_PROTO_ISO14443_MASK
) ||
976 (im_protocols
& NFC_PROTO_NFC_DEP_MASK
))
977 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
979 if (im_protocols
& NFC_PROTO_FELICA_MASK
||
980 im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
981 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
982 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
985 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
986 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
988 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
989 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
992 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
995 static int pn533_start_poll_complete(struct pn533
*dev
, struct sk_buff
*resp
)
997 u8 nbtg
, tg
, *tgdata
;
1000 /* Toggle the DEP polling */
1001 if (dev
->poll_protocols
& NFC_PROTO_NFC_DEP_MASK
)
1004 nbtg
= resp
->data
[0];
1006 tgdata
= &resp
->data
[2];
1007 tgdata_len
= resp
->len
- 2; /* nbtg + tg */
1010 rc
= pn533_target_found(dev
, tg
, tgdata
, tgdata_len
);
1012 /* We must stop the poll after a valid target found */
1020 static struct sk_buff
*pn533_alloc_poll_tg_frame(struct pn533
*dev
)
1022 struct sk_buff
*skb
;
1023 u8
*felica
, *nfcid3
;
1025 u8
*gbytes
= dev
->gb
;
1026 size_t gbytes_len
= dev
->gb_len
;
1028 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1029 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1030 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1031 0xff, 0xff}; /* System code */
1033 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1035 0x40}; /* SEL_RES for DEP */
1037 unsigned int skb_len
= 36 + /*
1038 * mode (1), mifare (6),
1039 * felica (18), nfcid3 (10), gb_len (1)
1044 skb
= pn533_alloc_skb(dev
, skb_len
);
1048 /* DEP support only */
1049 skb_put_u8(skb
, PN533_INIT_TARGET_DEP
);
1052 skb_put_data(skb
, mifare_params
, 6);
1055 felica
= skb_put_data(skb
, felica_params
, 18);
1056 get_random_bytes(felica
+ 2, 6);
1059 nfcid3
= skb_put_zero(skb
, 10);
1060 memcpy(nfcid3
, felica
, 8);
1063 skb_put_u8(skb
, gbytes_len
);
1065 skb_put_data(skb
, gbytes
, gbytes_len
);
1073 static void pn533_wq_tm_mi_recv(struct work_struct
*work
);
1074 static struct sk_buff
*pn533_build_response(struct pn533
*dev
);
1076 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1077 struct sk_buff
*resp
)
1079 struct sk_buff
*skb
;
1083 dev_dbg(dev
->dev
, "%s\n", __func__
);
1086 skb_queue_purge(&dev
->resp_q
);
1087 return PTR_ERR(resp
);
1090 status
= resp
->data
[0];
1092 ret
= status
& PN533_CMD_RET_MASK
;
1093 mi
= status
& PN533_CMD_MI_MASK
;
1095 skb_pull(resp
, sizeof(status
));
1097 if (ret
!= PN533_CMD_RET_SUCCESS
) {
1102 skb_queue_tail(&dev
->resp_q
, resp
);
1105 queue_work(dev
->wq
, &dev
->mi_tm_rx_work
);
1106 return -EINPROGRESS
;
1109 skb
= pn533_build_response(dev
);
1115 return nfc_tm_data_received(dev
->nfc_dev
, skb
);
1118 nfc_tm_deactivated(dev
->nfc_dev
);
1120 skb_queue_purge(&dev
->resp_q
);
1121 dev_kfree_skb(resp
);
1126 static void pn533_wq_tm_mi_recv(struct work_struct
*work
)
1128 struct pn533
*dev
= container_of(work
, struct pn533
, mi_tm_rx_work
);
1129 struct sk_buff
*skb
;
1132 dev_dbg(dev
->dev
, "%s\n", __func__
);
1134 skb
= pn533_alloc_skb(dev
, 0);
1138 rc
= pn533_send_cmd_direct_async(dev
,
1139 PN533_CMD_TG_GET_DATA
,
1141 pn533_tm_get_data_complete
,
1148 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
1149 struct sk_buff
*resp
);
1150 static void pn533_wq_tm_mi_send(struct work_struct
*work
)
1152 struct pn533
*dev
= container_of(work
, struct pn533
, mi_tm_tx_work
);
1153 struct sk_buff
*skb
;
1156 dev_dbg(dev
->dev
, "%s\n", __func__
);
1158 /* Grab the first skb in the queue */
1159 skb
= skb_dequeue(&dev
->fragment_skb
);
1160 if (skb
== NULL
) { /* No more data */
1161 /* Reset the queue for future use */
1162 skb_queue_head_init(&dev
->fragment_skb
);
1166 /* last entry - remove MI bit */
1167 if (skb_queue_len(&dev
->fragment_skb
) == 0) {
1168 rc
= pn533_send_cmd_direct_async(dev
, PN533_CMD_TG_SET_DATA
,
1169 skb
, pn533_tm_send_complete
, NULL
);
1171 rc
= pn533_send_cmd_direct_async(dev
,
1172 PN533_CMD_TG_SET_META_DATA
,
1173 skb
, pn533_tm_send_complete
, NULL
);
1175 if (rc
== 0) /* success */
1179 "Error %d when trying to perform set meta data_exchange", rc
);
1184 dev
->phy_ops
->send_ack(dev
, GFP_KERNEL
);
1185 queue_work(dev
->wq
, &dev
->cmd_work
);
1188 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1190 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1191 struct sk_buff
*skb
;
1194 dev_dbg(dev
->dev
, "%s\n", __func__
);
1196 skb
= pn533_alloc_skb(dev
, 0);
1200 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_GET_DATA
, skb
,
1201 pn533_tm_get_data_complete
, NULL
);
1207 #define ATR_REQ_GB_OFFSET 17
1208 static int pn533_init_target_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1210 u8 mode
, *cmd
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1214 dev_dbg(dev
->dev
, "%s\n", __func__
);
1216 if (resp
->len
< ATR_REQ_GB_OFFSET
+ 1)
1219 mode
= resp
->data
[0];
1220 cmd
= &resp
->data
[1];
1222 dev_dbg(dev
->dev
, "Target mode 0x%x len %d\n",
1225 if ((mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
) ==
1226 PN533_INIT_TARGET_RESP_ACTIVE
)
1227 comm_mode
= NFC_COMM_ACTIVE
;
1229 if ((mode
& PN533_INIT_TARGET_RESP_DEP
) == 0) /* Only DEP supported */
1232 gb
= cmd
+ ATR_REQ_GB_OFFSET
;
1233 gb_len
= resp
->len
- (ATR_REQ_GB_OFFSET
+ 1);
1235 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1236 comm_mode
, gb
, gb_len
);
1239 "Error when signaling target activation\n");
1244 queue_work(dev
->wq
, &dev
->tg_work
);
1249 static void pn533_listen_mode_timer(struct timer_list
*t
)
1251 struct pn533
*dev
= from_timer(dev
, t
, listen_timer
);
1253 dev_dbg(dev
->dev
, "Listen mode timeout\n");
1255 dev
->cancel_listen
= 1;
1257 pn533_poll_next_mod(dev
);
1259 queue_delayed_work(dev
->wq
, &dev
->poll_work
,
1260 msecs_to_jiffies(PN533_POLL_INTERVAL
));
1263 static int pn533_rf_complete(struct pn533
*dev
, void *arg
,
1264 struct sk_buff
*resp
)
1268 dev_dbg(dev
->dev
, "%s\n", __func__
);
1273 nfc_err(dev
->dev
, "RF setting error %d\n", rc
);
1278 queue_delayed_work(dev
->wq
, &dev
->poll_work
,
1279 msecs_to_jiffies(PN533_POLL_INTERVAL
));
1281 dev_kfree_skb(resp
);
1285 static void pn533_wq_rf(struct work_struct
*work
)
1287 struct pn533
*dev
= container_of(work
, struct pn533
, rf_work
);
1288 struct sk_buff
*skb
;
1291 dev_dbg(dev
->dev
, "%s\n", __func__
);
1293 skb
= pn533_alloc_skb(dev
, 2);
1297 skb_put_u8(skb
, PN533_CFGITEM_RF_FIELD
);
1298 skb_put_u8(skb
, PN533_CFGITEM_RF_FIELD_AUTO_RFCA
);
1300 rc
= pn533_send_cmd_async(dev
, PN533_CMD_RF_CONFIGURATION
, skb
,
1301 pn533_rf_complete
, NULL
);
1304 nfc_err(dev
->dev
, "RF setting error %d\n", rc
);
1308 static int pn533_poll_dep_complete(struct pn533
*dev
, void *arg
,
1309 struct sk_buff
*resp
)
1311 struct pn533_cmd_jump_dep_response
*rsp
;
1312 struct nfc_target nfc_target
;
1317 return PTR_ERR(resp
);
1319 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1321 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1322 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1323 /* Not target found, turn radio off */
1324 queue_work(dev
->wq
, &dev
->rf_work
);
1326 dev_kfree_skb(resp
);
1330 dev_dbg(dev
->dev
, "Creating new target");
1332 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1333 nfc_target
.nfcid1_len
= 10;
1334 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
1335 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1339 dev
->tgt_available_prots
= 0;
1340 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1342 /* ATR_RES general bytes are located at offset 17 */
1343 target_gt_len
= resp
->len
- 17;
1344 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1345 rsp
->gt
, target_gt_len
);
1347 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1348 dev
->nfc_dev
->targets
[0].idx
,
1349 0, NFC_RF_INITIATOR
);
1352 pn533_poll_reset_mod_list(dev
);
1355 dev_kfree_skb(resp
);
1359 #define PASSIVE_DATA_LEN 5
1360 static int pn533_poll_dep(struct nfc_dev
*nfc_dev
)
1362 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1363 struct sk_buff
*skb
;
1365 u8
*next
, nfcid3
[NFC_NFCID3_MAXSIZE
];
1366 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1368 dev_dbg(dev
->dev
, "%s", __func__
);
1371 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1373 if (!dev
->gb
|| !dev
->gb_len
) {
1375 queue_work(dev
->wq
, &dev
->rf_work
);
1379 skb_len
= 3 + dev
->gb_len
; /* ActPass + BR + Next */
1380 skb_len
+= PASSIVE_DATA_LEN
;
1383 skb_len
+= NFC_NFCID3_MAXSIZE
;
1386 get_random_bytes(nfcid3
+ 2, 6);
1388 skb
= pn533_alloc_skb(dev
, skb_len
);
1392 skb_put_u8(skb
, 0x01); /* Active */
1393 skb_put_u8(skb
, 0x02); /* 424 kbps */
1395 next
= skb_put(skb
, 1); /* Next */
1398 /* Copy passive data */
1399 skb_put_data(skb
, passive_data
, PASSIVE_DATA_LEN
);
1402 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1403 skb_put_data(skb
, nfcid3
, NFC_NFCID3_MAXSIZE
);
1406 skb_put_data(skb
, dev
->gb
, dev
->gb_len
);
1407 *next
|= 4; /* We have some Gi */
1409 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
1410 pn533_poll_dep_complete
, NULL
);
1418 static int pn533_autopoll_complete(struct pn533
*dev
, void *arg
,
1419 struct sk_buff
*resp
)
1421 struct pn532_autopoll_resp
*apr
;
1422 struct nfc_target nfc_tgt
;
1429 nfc_err(dev
->dev
, "%s autopoll complete error %d\n",
1432 if (rc
== -ENOENT
) {
1433 if (dev
->poll_mod_count
!= 0)
1436 } else if (rc
< 0) {
1438 "Error %d when running autopoll\n", rc
);
1443 nbtg
= resp
->data
[0];
1444 if ((nbtg
> 2) || (nbtg
<= 0))
1447 apr
= (struct pn532_autopoll_resp
*)&resp
->data
[1];
1449 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1450 switch (apr
->type
) {
1451 case PN532_AUTOPOLL_TYPE_ISOA
:
1452 dev_dbg(dev
->dev
, "ISOA\n");
1453 rc
= pn533_target_found_type_a(&nfc_tgt
, apr
->tgdata
,
1456 case PN532_AUTOPOLL_TYPE_FELICA212
:
1457 case PN532_AUTOPOLL_TYPE_FELICA424
:
1458 dev_dbg(dev
->dev
, "FELICA\n");
1459 rc
= pn533_target_found_felica(&nfc_tgt
, apr
->tgdata
,
1462 case PN532_AUTOPOLL_TYPE_JEWEL
:
1463 dev_dbg(dev
->dev
, "JEWEL\n");
1464 rc
= pn533_target_found_jewel(&nfc_tgt
, apr
->tgdata
,
1467 case PN532_AUTOPOLL_TYPE_ISOB
:
1468 dev_dbg(dev
->dev
, "ISOB\n");
1469 rc
= pn533_target_found_type_b(&nfc_tgt
, apr
->tgdata
,
1472 case PN532_AUTOPOLL_TYPE_MIFARE
:
1473 dev_dbg(dev
->dev
, "Mifare\n");
1474 rc
= pn533_target_found_type_a(&nfc_tgt
, apr
->tgdata
,
1479 "Unknown current poll modulation\n");
1486 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1488 "The Tg found doesn't have the desired protocol\n");
1493 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1494 apr
= (struct pn532_autopoll_resp
*)
1495 (apr
->tgdata
+ (apr
->ln
- 1));
1498 pn533_poll_reset_mod_list(dev
);
1499 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1502 dev_kfree_skb(resp
);
1506 nfc_err(dev
->dev
, "autopoll operation has been stopped\n");
1508 pn533_poll_reset_mod_list(dev
);
1509 dev
->poll_protocols
= 0;
1513 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1514 struct sk_buff
*resp
)
1516 struct pn533_poll_modulations
*cur_mod
;
1519 dev_dbg(dev
->dev
, "%s\n", __func__
);
1524 nfc_err(dev
->dev
, "%s Poll complete error %d\n",
1527 if (rc
== -ENOENT
) {
1528 if (dev
->poll_mod_count
!= 0)
1531 } else if (rc
< 0) {
1533 "Error %d when running poll\n", rc
);
1538 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1540 if (cur_mod
->len
== 0) { /* Target mode */
1541 del_timer(&dev
->listen_timer
);
1542 rc
= pn533_init_target_complete(dev
, resp
);
1546 /* Initiator mode */
1547 rc
= pn533_start_poll_complete(dev
, resp
);
1551 if (!dev
->poll_mod_count
) {
1552 dev_dbg(dev
->dev
, "Polling has been stopped\n");
1556 pn533_poll_next_mod(dev
);
1557 /* Not target found, turn radio off */
1558 queue_work(dev
->wq
, &dev
->rf_work
);
1561 dev_kfree_skb(resp
);
1565 nfc_err(dev
->dev
, "Polling operation has been stopped\n");
1567 pn533_poll_reset_mod_list(dev
);
1568 dev
->poll_protocols
= 0;
1572 static struct sk_buff
*pn533_alloc_poll_in_frame(struct pn533
*dev
,
1573 struct pn533_poll_modulations
*mod
)
1575 struct sk_buff
*skb
;
1577 skb
= pn533_alloc_skb(dev
, mod
->len
);
1581 skb_put_data(skb
, &mod
->data
, mod
->len
);
1586 static int pn533_send_poll_frame(struct pn533
*dev
)
1588 struct pn533_poll_modulations
*mod
;
1589 struct sk_buff
*skb
;
1593 mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1595 dev_dbg(dev
->dev
, "%s mod len %d\n",
1596 __func__
, mod
->len
);
1598 if ((dev
->poll_protocols
& NFC_PROTO_NFC_DEP_MASK
) && dev
->poll_dep
) {
1600 return pn533_poll_dep(dev
->nfc_dev
);
1603 if (mod
->len
== 0) { /* Listen mode */
1604 cmd_code
= PN533_CMD_TG_INIT_AS_TARGET
;
1605 skb
= pn533_alloc_poll_tg_frame(dev
);
1606 } else { /* Polling mode */
1607 cmd_code
= PN533_CMD_IN_LIST_PASSIVE_TARGET
;
1608 skb
= pn533_alloc_poll_in_frame(dev
, mod
);
1612 nfc_err(dev
->dev
, "Failed to allocate skb\n");
1616 rc
= pn533_send_cmd_async(dev
, cmd_code
, skb
, pn533_poll_complete
,
1620 nfc_err(dev
->dev
, "Polling loop error %d\n", rc
);
1626 static void pn533_wq_poll(struct work_struct
*work
)
1628 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
.work
);
1629 struct pn533_poll_modulations
*cur_mod
;
1632 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1635 "%s cancel_listen %d modulation len %d\n",
1636 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1638 if (dev
->cancel_listen
== 1) {
1639 dev
->cancel_listen
= 0;
1640 dev
->phy_ops
->abort_cmd(dev
, GFP_ATOMIC
);
1643 rc
= pn533_send_poll_frame(dev
);
1647 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1648 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1651 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1652 u32 im_protocols
, u32 tm_protocols
)
1654 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1655 struct pn533_poll_modulations
*cur_mod
;
1656 struct sk_buff
*skb
;
1661 "%s: im protocols 0x%x tm protocols 0x%x\n",
1662 __func__
, im_protocols
, tm_protocols
);
1664 if (dev
->tgt_active_prot
) {
1666 "Cannot poll with a target already activated\n");
1670 if (dev
->tgt_mode
) {
1672 "Cannot poll while already being activated\n");
1677 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1678 if (dev
->gb
== NULL
)
1682 dev
->poll_protocols
= im_protocols
;
1683 dev
->listen_protocols
= tm_protocols
;
1684 if (dev
->device_type
== PN533_DEVICE_PN532_AUTOPOLL
) {
1685 skb
= pn533_alloc_skb(dev
, 4 + 6);
1689 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1690 PN532_AUTOPOLL_POLLNR_INFINITE
;
1691 *((u8
*)skb_put(skb
, sizeof(u8
))) = PN532_AUTOPOLL_PERIOD
;
1693 if ((im_protocols
& NFC_PROTO_MIFARE_MASK
) &&
1694 (im_protocols
& NFC_PROTO_ISO14443_MASK
) &&
1695 (im_protocols
& NFC_PROTO_NFC_DEP_MASK
))
1696 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1697 PN532_AUTOPOLL_TYPE_GENERIC_106
;
1699 if (im_protocols
& NFC_PROTO_MIFARE_MASK
)
1700 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1701 PN532_AUTOPOLL_TYPE_MIFARE
;
1703 if (im_protocols
& NFC_PROTO_ISO14443_MASK
)
1704 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1705 PN532_AUTOPOLL_TYPE_ISOA
;
1707 if (im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1708 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1709 PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106
;
1710 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1711 PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212
;
1712 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1713 PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424
;
1717 if (im_protocols
& NFC_PROTO_FELICA_MASK
||
1718 im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1719 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1720 PN532_AUTOPOLL_TYPE_FELICA212
;
1721 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1722 PN532_AUTOPOLL_TYPE_FELICA424
;
1725 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1726 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1727 PN532_AUTOPOLL_TYPE_JEWEL
;
1729 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1730 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1731 PN532_AUTOPOLL_TYPE_ISOB
;
1734 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1735 PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106
;
1737 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_AUTOPOLL
, skb
,
1738 pn533_autopoll_complete
, NULL
);
1743 dev
->poll_mod_count
++;
1748 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1750 /* Do not always start polling from the same modulation */
1751 get_random_bytes(&rand_mod
, sizeof(rand_mod
));
1752 rand_mod
%= dev
->poll_mod_count
;
1753 dev
->poll_mod_curr
= rand_mod
;
1755 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1757 rc
= pn533_send_poll_frame(dev
);
1759 /* Start listen timer */
1760 if (!rc
&& cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1761 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1766 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1768 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1770 del_timer(&dev
->listen_timer
);
1772 if (!dev
->poll_mod_count
) {
1774 "Polling operation was not running\n");
1778 dev
->phy_ops
->abort_cmd(dev
, GFP_KERNEL
);
1779 flush_delayed_work(&dev
->poll_work
);
1780 pn533_poll_reset_mod_list(dev
);
1783 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1785 struct pn533_cmd_activate_response
*rsp
;
1788 struct sk_buff
*skb
;
1789 struct sk_buff
*resp
;
1791 dev_dbg(dev
->dev
, "%s\n", __func__
);
1793 skb
= pn533_alloc_skb(dev
, sizeof(u8
) * 2); /*TG + Next*/
1797 skb_put_u8(skb
, 1); /* TG */
1798 skb_put_u8(skb
, 0); /* Next */
1800 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_ATR
, skb
);
1802 return PTR_ERR(resp
);
1804 rsp
= (struct pn533_cmd_activate_response
*)resp
->data
;
1805 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1806 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1808 "Target activation failed (error 0x%x)\n", rc
);
1809 dev_kfree_skb(resp
);
1813 /* ATR_RES general bytes are located at offset 16 */
1814 gt_len
= resp
->len
- 16;
1815 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, rsp
->gt
, gt_len
);
1817 dev_kfree_skb(resp
);
1821 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1822 struct nfc_target
*target
, u32 protocol
)
1824 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1827 dev_dbg(dev
->dev
, "%s: protocol=%u\n", __func__
, protocol
);
1829 if (dev
->poll_mod_count
) {
1831 "Cannot activate while polling\n");
1835 if (dev
->tgt_active_prot
) {
1837 "There is already an active target\n");
1841 if (!dev
->tgt_available_prots
) {
1843 "There is no available target to activate\n");
1847 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1849 "Target doesn't support requested proto %u\n",
1854 if (protocol
== NFC_PROTO_NFC_DEP
) {
1855 rc
= pn533_activate_target_nfcdep(dev
);
1858 "Activating target with DEP failed %d\n", rc
);
1863 dev
->tgt_active_prot
= protocol
;
1864 dev
->tgt_available_prots
= 0;
1869 static int pn533_deactivate_target_complete(struct pn533
*dev
, void *arg
,
1870 struct sk_buff
*resp
)
1874 dev_dbg(dev
->dev
, "%s\n", __func__
);
1879 nfc_err(dev
->dev
, "Target release error %d\n", rc
);
1884 rc
= resp
->data
[0] & PN533_CMD_RET_MASK
;
1885 if (rc
!= PN533_CMD_RET_SUCCESS
)
1887 "Error 0x%x when releasing the target\n", rc
);
1889 dev_kfree_skb(resp
);
1893 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1894 struct nfc_target
*target
, u8 mode
)
1896 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1897 struct sk_buff
*skb
;
1900 dev_dbg(dev
->dev
, "%s\n", __func__
);
1902 if (!dev
->tgt_active_prot
) {
1903 nfc_err(dev
->dev
, "There is no active target\n");
1907 dev
->tgt_active_prot
= 0;
1908 skb_queue_purge(&dev
->resp_q
);
1910 skb
= pn533_alloc_skb(dev
, sizeof(u8
));
1914 skb_put_u8(skb
, 1); /* TG*/
1916 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_RELEASE
, skb
,
1917 pn533_deactivate_target_complete
, NULL
);
1920 nfc_err(dev
->dev
, "Target release error %d\n", rc
);
1925 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1926 struct sk_buff
*resp
)
1928 struct pn533_cmd_jump_dep_response
*rsp
;
1931 u8 active
= *(u8
*)arg
;
1936 return PTR_ERR(resp
);
1938 if (dev
->tgt_available_prots
&&
1939 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1941 "The target does not support DEP\n");
1946 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1948 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1949 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1951 "Bringing DEP link up failed (error 0x%x)\n", rc
);
1955 if (!dev
->tgt_available_prots
) {
1956 struct nfc_target nfc_target
;
1958 dev_dbg(dev
->dev
, "Creating new target\n");
1960 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1961 nfc_target
.nfcid1_len
= 10;
1962 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
1963 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1967 dev
->tgt_available_prots
= 0;
1970 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1972 /* ATR_RES general bytes are located at offset 17 */
1973 target_gt_len
= resp
->len
- 17;
1974 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1975 rsp
->gt
, target_gt_len
);
1977 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1978 dev
->nfc_dev
->targets
[0].idx
,
1979 !active
, NFC_RF_INITIATOR
);
1982 dev_kfree_skb(resp
);
1986 static int pn533_rf_field(struct nfc_dev
*nfc_dev
, u8 rf
);
1987 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1988 u8 comm_mode
, u8
*gb
, size_t gb_len
)
1990 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1991 struct sk_buff
*skb
;
1993 u8
*next
, *arg
, nfcid3
[NFC_NFCID3_MAXSIZE
];
1994 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1996 dev_dbg(dev
->dev
, "%s\n", __func__
);
1998 if (dev
->poll_mod_count
) {
2000 "Cannot bring the DEP link up while polling\n");
2004 if (dev
->tgt_active_prot
) {
2006 "There is already an active target\n");
2010 skb_len
= 3 + gb_len
; /* ActPass + BR + Next */
2011 skb_len
+= PASSIVE_DATA_LEN
;
2014 skb_len
+= NFC_NFCID3_MAXSIZE
;
2015 if (target
&& !target
->nfcid2_len
) {
2018 get_random_bytes(nfcid3
+ 2, 6);
2021 skb
= pn533_alloc_skb(dev
, skb_len
);
2025 skb_put_u8(skb
, !comm_mode
); /* ActPass */
2026 skb_put_u8(skb
, 0x02); /* 424 kbps */
2028 next
= skb_put(skb
, 1); /* Next */
2031 /* Copy passive data */
2032 skb_put_data(skb
, passive_data
, PASSIVE_DATA_LEN
);
2035 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2036 if (target
&& target
->nfcid2_len
)
2037 memcpy(skb_put(skb
, NFC_NFCID3_MAXSIZE
), target
->nfcid2
,
2038 target
->nfcid2_len
);
2040 skb_put_data(skb
, nfcid3
, NFC_NFCID3_MAXSIZE
);
2043 if (gb
!= NULL
&& gb_len
> 0) {
2044 skb_put_data(skb
, gb
, gb_len
);
2045 *next
|= 4; /* We have some Gi */
2050 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2058 pn533_rf_field(dev
->nfc_dev
, 0);
2060 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
2061 pn533_in_dep_link_up_complete
, arg
);
2071 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
2073 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2075 dev_dbg(dev
->dev
, "%s\n", __func__
);
2077 pn533_poll_reset_mod_list(dev
);
2079 if (dev
->tgt_mode
|| dev
->tgt_active_prot
)
2080 dev
->phy_ops
->abort_cmd(dev
, GFP_KERNEL
);
2082 dev
->tgt_active_prot
= 0;
2085 skb_queue_purge(&dev
->resp_q
);
2090 struct pn533_data_exchange_arg
{
2091 data_exchange_cb_t cb
;
2095 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
2097 struct sk_buff
*skb
, *tmp
, *t
;
2098 unsigned int skb_len
= 0, tmp_len
= 0;
2100 dev_dbg(dev
->dev
, "%s\n", __func__
);
2102 if (skb_queue_empty(&dev
->resp_q
))
2105 if (skb_queue_len(&dev
->resp_q
) == 1) {
2106 skb
= skb_dequeue(&dev
->resp_q
);
2110 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
2111 skb_len
+= tmp
->len
;
2113 dev_dbg(dev
->dev
, "%s total length %d\n",
2116 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
2120 skb_put(skb
, skb_len
);
2122 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
2123 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
2124 tmp_len
+= tmp
->len
;
2128 skb_queue_purge(&dev
->resp_q
);
2133 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
2134 struct sk_buff
*resp
)
2136 struct pn533_data_exchange_arg
*arg
= _arg
;
2137 struct sk_buff
*skb
;
2141 dev_dbg(dev
->dev
, "%s\n", __func__
);
2148 status
= resp
->data
[0];
2149 ret
= status
& PN533_CMD_RET_MASK
;
2150 mi
= status
& PN533_CMD_MI_MASK
;
2152 skb_pull(resp
, sizeof(status
));
2154 if (ret
!= PN533_CMD_RET_SUCCESS
) {
2156 "Exchanging data failed (error 0x%x)\n", ret
);
2161 skb_queue_tail(&dev
->resp_q
, resp
);
2164 dev
->cmd_complete_mi_arg
= arg
;
2165 queue_work(dev
->wq
, &dev
->mi_rx_work
);
2166 return -EINPROGRESS
;
2169 /* Prepare for the next round */
2170 if (skb_queue_len(&dev
->fragment_skb
) > 0) {
2171 dev
->cmd_complete_dep_arg
= arg
;
2172 queue_work(dev
->wq
, &dev
->mi_tx_work
);
2174 return -EINPROGRESS
;
2177 skb
= pn533_build_response(dev
);
2183 arg
->cb(arg
->cb_context
, skb
, 0);
2188 dev_kfree_skb(resp
);
2190 skb_queue_purge(&dev
->resp_q
);
2191 arg
->cb(arg
->cb_context
, NULL
, rc
);
2197 * Receive an incoming pn533 frame. skb contains only header and payload.
2198 * If skb == NULL, it is a notification that the link below is dead.
2200 void pn533_recv_frame(struct pn533
*dev
, struct sk_buff
*skb
, int status
)
2205 dev
->cmd
->status
= status
;
2208 dev_dbg(dev
->dev
, "%s: Error received: %d\n", __func__
, status
);
2213 pr_err("NULL Frame -> link is dead\n");
2217 if (pn533_rx_frame_is_ack(skb
->data
)) {
2218 dev_dbg(dev
->dev
, "%s: Received ACK frame\n", __func__
);
2223 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE
, 16, 1, skb
->data
,
2224 dev
->ops
->rx_frame_size(skb
->data
), false);
2226 if (!dev
->ops
->rx_is_frame_valid(skb
->data
, dev
)) {
2227 nfc_err(dev
->dev
, "Received an invalid frame\n");
2228 dev
->cmd
->status
= -EIO
;
2229 } else if (!pn533_rx_frame_is_cmd_response(dev
, skb
->data
)) {
2230 nfc_err(dev
->dev
, "It it not the response to the last command\n");
2231 dev
->cmd
->status
= -EIO
;
2234 dev
->cmd
->resp
= skb
;
2237 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
2239 EXPORT_SYMBOL(pn533_recv_frame
);
2241 /* Split the Tx skb into small chunks */
2242 static int pn533_fill_fragment_skbs(struct pn533
*dev
, struct sk_buff
*skb
)
2244 struct sk_buff
*frag
;
2248 /* Remaining size */
2249 if (skb
->len
> PN533_CMD_DATAFRAME_MAXLEN
)
2250 frag_size
= PN533_CMD_DATAFRAME_MAXLEN
;
2252 frag_size
= skb
->len
;
2254 /* Allocate and reserve */
2255 frag
= pn533_alloc_skb(dev
, frag_size
);
2257 skb_queue_purge(&dev
->fragment_skb
);
2261 if (!dev
->tgt_mode
) {
2262 /* Reserve the TG/MI byte */
2263 skb_reserve(frag
, 1);
2266 if (frag_size
== PN533_CMD_DATAFRAME_MAXLEN
)
2267 *(u8
*)skb_push(frag
, sizeof(u8
)) =
2268 (PN533_CMD_MI_MASK
| 1);
2270 *(u8
*)skb_push(frag
, sizeof(u8
)) = 1; /* TG */
2273 skb_put_data(frag
, skb
->data
, frag_size
);
2275 /* Reduce the size of incoming buffer */
2276 skb_pull(skb
, frag_size
);
2278 /* Add this to skb_queue */
2279 skb_queue_tail(&dev
->fragment_skb
, frag
);
2281 } while (skb
->len
> 0);
2285 return skb_queue_len(&dev
->fragment_skb
);
2288 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
2289 struct nfc_target
*target
, struct sk_buff
*skb
,
2290 data_exchange_cb_t cb
, void *cb_context
)
2292 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2293 struct pn533_data_exchange_arg
*arg
= NULL
;
2296 dev_dbg(dev
->dev
, "%s\n", __func__
);
2298 if (!dev
->tgt_active_prot
) {
2300 "Can't exchange data if there is no active target\n");
2305 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2312 arg
->cb_context
= cb_context
;
2314 switch (dev
->device_type
) {
2315 case PN533_DEVICE_PASORI
:
2316 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2317 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_COMM_THRU
,
2319 pn533_data_exchange_complete
,
2327 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2328 rc
= pn533_fill_fragment_skbs(dev
, skb
);
2332 skb
= skb_dequeue(&dev
->fragment_skb
);
2338 *(u8
*)skb_push(skb
, sizeof(u8
)) = 1; /* TG */
2341 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_DATA_EXCHANGE
,
2342 skb
, pn533_data_exchange_complete
,
2348 if (rc
< 0) /* rc from send_async */
2359 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2360 struct sk_buff
*resp
)
2364 dev_dbg(dev
->dev
, "%s\n", __func__
);
2367 return PTR_ERR(resp
);
2369 status
= resp
->data
[0];
2371 /* Prepare for the next round */
2372 if (skb_queue_len(&dev
->fragment_skb
) > 0) {
2373 queue_work(dev
->wq
, &dev
->mi_tm_tx_work
);
2374 return -EINPROGRESS
;
2376 dev_kfree_skb(resp
);
2379 nfc_tm_deactivated(dev
->nfc_dev
);
2386 queue_work(dev
->wq
, &dev
->tg_work
);
2391 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2393 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2396 dev_dbg(dev
->dev
, "%s\n", __func__
);
2398 /* let's split in multiple chunks if size's too big */
2399 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2400 rc
= pn533_fill_fragment_skbs(dev
, skb
);
2404 /* get the first skb */
2405 skb
= skb_dequeue(&dev
->fragment_skb
);
2411 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_META_DATA
, skb
,
2412 pn533_tm_send_complete
, NULL
);
2415 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_DATA
, skb
,
2416 pn533_tm_send_complete
, NULL
);
2422 skb_queue_purge(&dev
->fragment_skb
);
2428 static void pn533_wq_mi_recv(struct work_struct
*work
)
2430 struct pn533
*dev
= container_of(work
, struct pn533
, mi_rx_work
);
2431 struct sk_buff
*skb
;
2434 dev_dbg(dev
->dev
, "%s\n", __func__
);
2436 skb
= pn533_alloc_skb(dev
, PN533_CMD_DATAEXCH_HEAD_LEN
);
2440 switch (dev
->device_type
) {
2441 case PN533_DEVICE_PASORI
:
2442 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2443 rc
= pn533_send_cmd_direct_async(dev
,
2444 PN533_CMD_IN_COMM_THRU
,
2446 pn533_data_exchange_complete
,
2447 dev
->cmd_complete_mi_arg
);
2453 skb_put_u8(skb
, 1); /*TG*/
2455 rc
= pn533_send_cmd_direct_async(dev
,
2456 PN533_CMD_IN_DATA_EXCHANGE
,
2458 pn533_data_exchange_complete
,
2459 dev
->cmd_complete_mi_arg
);
2464 if (rc
== 0) /* success */
2468 "Error %d when trying to perform data_exchange\n", rc
);
2471 kfree(dev
->cmd_complete_mi_arg
);
2474 dev
->phy_ops
->send_ack(dev
, GFP_KERNEL
);
2475 queue_work(dev
->wq
, &dev
->cmd_work
);
2478 static void pn533_wq_mi_send(struct work_struct
*work
)
2480 struct pn533
*dev
= container_of(work
, struct pn533
, mi_tx_work
);
2481 struct sk_buff
*skb
;
2484 dev_dbg(dev
->dev
, "%s\n", __func__
);
2486 /* Grab the first skb in the queue */
2487 skb
= skb_dequeue(&dev
->fragment_skb
);
2489 if (skb
== NULL
) { /* No more data */
2490 /* Reset the queue for future use */
2491 skb_queue_head_init(&dev
->fragment_skb
);
2495 switch (dev
->device_type
) {
2496 case PN533_DEVICE_PASORI
:
2497 if (dev
->tgt_active_prot
!= NFC_PROTO_FELICA
) {
2502 rc
= pn533_send_cmd_direct_async(dev
, PN533_CMD_IN_COMM_THRU
,
2504 pn533_data_exchange_complete
,
2505 dev
->cmd_complete_dep_arg
);
2510 /* Still some fragments? */
2511 rc
= pn533_send_cmd_direct_async(dev
,
2512 PN533_CMD_IN_DATA_EXCHANGE
,
2514 pn533_data_exchange_complete
,
2515 dev
->cmd_complete_dep_arg
);
2520 if (rc
== 0) /* success */
2524 "Error %d when trying to perform data_exchange\n", rc
);
2527 kfree(dev
->cmd_complete_dep_arg
);
2530 dev
->phy_ops
->send_ack(dev
, GFP_KERNEL
);
2531 queue_work(dev
->wq
, &dev
->cmd_work
);
2534 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2537 struct sk_buff
*skb
;
2538 struct sk_buff
*resp
;
2541 dev_dbg(dev
->dev
, "%s\n", __func__
);
2543 skb_len
= sizeof(cfgitem
) + cfgdata_len
; /* cfgitem + cfgdata */
2545 skb
= pn533_alloc_skb(dev
, skb_len
);
2549 skb_put_u8(skb
, cfgitem
);
2550 skb_put_data(skb
, cfgdata
, cfgdata_len
);
2552 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_RF_CONFIGURATION
, skb
);
2554 return PTR_ERR(resp
);
2556 dev_kfree_skb(resp
);
2560 static int pn533_get_firmware_version(struct pn533
*dev
,
2561 struct pn533_fw_version
*fv
)
2563 struct sk_buff
*skb
;
2564 struct sk_buff
*resp
;
2566 skb
= pn533_alloc_skb(dev
, 0);
2570 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_GET_FIRMWARE_VERSION
, skb
);
2572 return PTR_ERR(resp
);
2574 fv
->ic
= resp
->data
[0];
2575 fv
->ver
= resp
->data
[1];
2576 fv
->rev
= resp
->data
[2];
2577 fv
->support
= resp
->data
[3];
2579 dev_kfree_skb(resp
);
2583 static int pn533_pasori_fw_reset(struct pn533
*dev
)
2585 struct sk_buff
*skb
;
2586 struct sk_buff
*resp
;
2588 dev_dbg(dev
->dev
, "%s\n", __func__
);
2590 skb
= pn533_alloc_skb(dev
, sizeof(u8
));
2594 skb_put_u8(skb
, 0x1);
2596 resp
= pn533_send_cmd_sync(dev
, 0x18, skb
);
2598 return PTR_ERR(resp
);
2600 dev_kfree_skb(resp
);
2605 static int pn533_rf_field(struct nfc_dev
*nfc_dev
, u8 rf
)
2607 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2611 rf_field
|= PN533_CFGITEM_RF_FIELD_AUTO_RFCA
;
2613 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_RF_FIELD
,
2614 (u8
*)&rf_field
, 1);
2616 nfc_err(dev
->dev
, "Error on setting RF field\n");
2623 static int pn532_sam_configuration(struct nfc_dev
*nfc_dev
)
2625 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2626 struct sk_buff
*skb
;
2627 struct sk_buff
*resp
;
2629 skb
= pn533_alloc_skb(dev
, 1);
2633 skb_put_u8(skb
, 0x01);
2635 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_SAM_CONFIGURATION
, skb
);
2637 return PTR_ERR(resp
);
2639 dev_kfree_skb(resp
);
2643 static int pn533_dev_up(struct nfc_dev
*nfc_dev
)
2645 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2648 if (dev
->phy_ops
->dev_up
) {
2649 rc
= dev
->phy_ops
->dev_up(dev
);
2654 if ((dev
->device_type
== PN533_DEVICE_PN532
) ||
2655 (dev
->device_type
== PN533_DEVICE_PN532_AUTOPOLL
)) {
2656 rc
= pn532_sam_configuration(nfc_dev
);
2662 return pn533_rf_field(nfc_dev
, 1);
2665 static int pn533_dev_down(struct nfc_dev
*nfc_dev
)
2667 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2670 ret
= pn533_rf_field(nfc_dev
, 0);
2671 if (dev
->phy_ops
->dev_down
&& !ret
)
2672 ret
= dev
->phy_ops
->dev_down(dev
);
2677 static struct nfc_ops pn533_nfc_ops
= {
2678 .dev_up
= pn533_dev_up
,
2679 .dev_down
= pn533_dev_down
,
2680 .dep_link_up
= pn533_dep_link_up
,
2681 .dep_link_down
= pn533_dep_link_down
,
2682 .start_poll
= pn533_start_poll
,
2683 .stop_poll
= pn533_stop_poll
,
2684 .activate_target
= pn533_activate_target
,
2685 .deactivate_target
= pn533_deactivate_target
,
2686 .im_transceive
= pn533_transceive
,
2687 .tm_send
= pn533_tm_send
,
2690 static int pn533_setup(struct pn533
*dev
)
2692 struct pn533_config_max_retries max_retries
;
2693 struct pn533_config_timing timing
;
2694 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2697 switch (dev
->device_type
) {
2698 case PN533_DEVICE_STD
:
2699 case PN533_DEVICE_PASORI
:
2700 case PN533_DEVICE_ACR122U
:
2701 case PN533_DEVICE_PN532
:
2702 case PN533_DEVICE_PN532_AUTOPOLL
:
2703 max_retries
.mx_rty_atr
= 0x2;
2704 max_retries
.mx_rty_psl
= 0x1;
2705 max_retries
.mx_rty_passive_act
=
2706 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2708 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2709 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2710 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2715 nfc_err(dev
->dev
, "Unknown device type %d\n",
2720 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2721 (u8
*)&max_retries
, sizeof(max_retries
));
2724 "Error on setting MAX_RETRIES config\n");
2729 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2730 (u8
*)&timing
, sizeof(timing
));
2732 nfc_err(dev
->dev
, "Error on setting RF timings\n");
2736 switch (dev
->device_type
) {
2737 case PN533_DEVICE_STD
:
2738 case PN533_DEVICE_PN532
:
2739 case PN533_DEVICE_PN532_AUTOPOLL
:
2742 case PN533_DEVICE_PASORI
:
2743 pn533_pasori_fw_reset(dev
);
2745 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2749 "Error while settings PASORI config\n");
2753 pn533_pasori_fw_reset(dev
);
2761 int pn533_finalize_setup(struct pn533
*dev
)
2764 struct pn533_fw_version fw_ver
;
2767 memset(&fw_ver
, 0, sizeof(fw_ver
));
2769 rc
= pn533_get_firmware_version(dev
, &fw_ver
);
2771 nfc_err(dev
->dev
, "Unable to get FW version\n");
2775 nfc_info(dev
->dev
, "NXP PN5%02X firmware ver %d.%d now attached\n",
2776 fw_ver
.ic
, fw_ver
.ver
, fw_ver
.rev
);
2778 rc
= pn533_setup(dev
);
2784 EXPORT_SYMBOL_GPL(pn533_finalize_setup
);
2786 struct pn533
*pn53x_common_init(u32 device_type
,
2787 enum pn533_protocol_type protocol_type
,
2789 struct pn533_phy_ops
*phy_ops
,
2790 struct pn533_frame_ops
*fops
,
2796 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
2798 return ERR_PTR(-ENOMEM
);
2801 priv
->phy_ops
= phy_ops
;
2806 priv
->ops
= &pn533_std_frame_ops
;
2808 priv
->protocol_type
= protocol_type
;
2809 priv
->device_type
= device_type
;
2811 mutex_init(&priv
->cmd_lock
);
2813 INIT_WORK(&priv
->cmd_work
, pn533_wq_cmd
);
2814 INIT_WORK(&priv
->cmd_complete_work
, pn533_wq_cmd_complete
);
2815 INIT_WORK(&priv
->mi_rx_work
, pn533_wq_mi_recv
);
2816 INIT_WORK(&priv
->mi_tx_work
, pn533_wq_mi_send
);
2817 INIT_WORK(&priv
->tg_work
, pn533_wq_tg_get_data
);
2818 INIT_WORK(&priv
->mi_tm_rx_work
, pn533_wq_tm_mi_recv
);
2819 INIT_WORK(&priv
->mi_tm_tx_work
, pn533_wq_tm_mi_send
);
2820 INIT_DELAYED_WORK(&priv
->poll_work
, pn533_wq_poll
);
2821 INIT_WORK(&priv
->rf_work
, pn533_wq_rf
);
2822 priv
->wq
= alloc_ordered_workqueue("pn533", 0);
2823 if (priv
->wq
== NULL
)
2826 timer_setup(&priv
->listen_timer
, pn533_listen_mode_timer
, 0);
2828 skb_queue_head_init(&priv
->resp_q
);
2829 skb_queue_head_init(&priv
->fragment_skb
);
2831 INIT_LIST_HEAD(&priv
->cmd_queue
);
2838 EXPORT_SYMBOL_GPL(pn53x_common_init
);
2840 void pn53x_common_clean(struct pn533
*priv
)
2842 struct pn533_cmd
*cmd
, *n
;
2844 flush_delayed_work(&priv
->poll_work
);
2845 destroy_workqueue(priv
->wq
);
2847 skb_queue_purge(&priv
->resp_q
);
2849 del_timer(&priv
->listen_timer
);
2851 list_for_each_entry_safe(cmd
, n
, &priv
->cmd_queue
, queue
) {
2852 list_del(&cmd
->queue
);
2858 EXPORT_SYMBOL_GPL(pn53x_common_clean
);
2860 int pn532_i2c_nfc_alloc(struct pn533
*priv
, u32 protocols
,
2861 struct device
*parent
)
2863 priv
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2864 priv
->ops
->tx_header_len
+
2865 PN533_CMD_DATAEXCH_HEAD_LEN
,
2866 priv
->ops
->tx_tail_len
);
2870 nfc_set_parent_dev(priv
->nfc_dev
, parent
);
2871 nfc_set_drvdata(priv
->nfc_dev
, priv
);
2874 EXPORT_SYMBOL_GPL(pn532_i2c_nfc_alloc
);
2876 int pn53x_register_nfc(struct pn533
*priv
, u32 protocols
,
2877 struct device
*parent
)
2881 rc
= pn532_i2c_nfc_alloc(priv
, protocols
, parent
);
2885 rc
= nfc_register_device(priv
->nfc_dev
);
2887 nfc_free_device(priv
->nfc_dev
);
2891 EXPORT_SYMBOL_GPL(pn53x_register_nfc
);
2893 void pn53x_unregister_nfc(struct pn533
*priv
)
2895 nfc_unregister_device(priv
->nfc_dev
);
2896 nfc_free_device(priv
->nfc_dev
);
2898 EXPORT_SYMBOL_GPL(pn53x_unregister_nfc
);
2900 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2901 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2902 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2903 MODULE_DESCRIPTION("PN533 driver ver " VERSION
);
2904 MODULE_VERSION(VERSION
);
2905 MODULE_LICENSE("GPL");