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
)
492 return __pn533_send_async(dev
, cmd_code
, req
, complete_cb
,
493 complete_cb_context
);
496 static int pn533_send_cmd_async(struct pn533
*dev
, u8 cmd_code
,
498 pn533_send_async_complete_t complete_cb
,
499 void *complete_cb_context
)
501 return __pn533_send_async(dev
, cmd_code
, req
, complete_cb
,
502 complete_cb_context
);
506 * pn533_send_cmd_direct_async
508 * The function sends a priority cmd directly to the chip omitting the cmd
509 * queue. It's intended to be used by chaining mechanism of received responses
510 * where the host has to request every single chunk of data before scheduling
511 * next cmd from the queue.
513 static int pn533_send_cmd_direct_async(struct pn533
*dev
, u8 cmd_code
,
515 pn533_send_async_complete_t complete_cb
,
516 void *complete_cb_context
)
518 struct pn533_cmd
*cmd
;
521 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
525 cmd
->code
= cmd_code
;
527 cmd
->complete_cb
= complete_cb
;
528 cmd
->complete_cb_context
= complete_cb_context
;
530 pn533_build_cmd_frame(dev
, cmd_code
, req
);
533 rc
= dev
->phy_ops
->send_frame(dev
, req
);
542 static void pn533_wq_cmd_complete(struct work_struct
*work
)
544 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_complete_work
);
547 rc
= pn533_send_async_complete(dev
);
548 if (rc
!= -EINPROGRESS
)
549 queue_work(dev
->wq
, &dev
->cmd_work
);
552 static void pn533_wq_cmd(struct work_struct
*work
)
554 struct pn533
*dev
= container_of(work
, struct pn533
, cmd_work
);
555 struct pn533_cmd
*cmd
;
558 mutex_lock(&dev
->cmd_lock
);
560 if (list_empty(&dev
->cmd_queue
)) {
561 dev
->cmd_pending
= 0;
562 mutex_unlock(&dev
->cmd_lock
);
566 cmd
= list_first_entry(&dev
->cmd_queue
, struct pn533_cmd
, queue
);
568 list_del(&cmd
->queue
);
570 mutex_unlock(&dev
->cmd_lock
);
573 rc
= dev
->phy_ops
->send_frame(dev
, cmd
->req
);
576 dev_kfree_skb(cmd
->req
);
583 struct pn533_sync_cmd_response
{
584 struct sk_buff
*resp
;
585 struct completion done
;
588 static int pn533_send_sync_complete(struct pn533
*dev
, void *_arg
,
589 struct sk_buff
*resp
)
591 struct pn533_sync_cmd_response
*arg
= _arg
;
594 complete(&arg
->done
);
599 /* pn533_send_cmd_sync
601 * Please note the req parameter is freed inside the function to
602 * limit a number of return value interpretations by the caller.
604 * 1. negative in case of error during TX path -> req should be freed
606 * 2. negative in case of error during RX path -> req should not be freed
607 * as it's been already freed at the beginning of RX path by
610 * 3. valid pointer in case of successful RX path
612 * A caller has to check a return value with IS_ERR macro. If the test pass,
613 * the returned pointer is valid.
616 static struct sk_buff
*pn533_send_cmd_sync(struct pn533
*dev
, u8 cmd_code
,
620 struct pn533_sync_cmd_response arg
;
622 init_completion(&arg
.done
);
624 rc
= pn533_send_cmd_async(dev
, cmd_code
, req
,
625 pn533_send_sync_complete
, &arg
);
631 wait_for_completion(&arg
.done
);
636 static struct sk_buff
*pn533_alloc_skb(struct pn533
*dev
, unsigned int size
)
640 skb
= alloc_skb(dev
->ops
->tx_header_len
+
642 dev
->ops
->tx_tail_len
, GFP_KERNEL
);
645 skb_reserve(skb
, dev
->ops
->tx_header_len
);
650 struct pn533_target_type_a
{
658 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
659 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
660 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
662 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
663 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
665 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
666 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
668 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
669 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
670 #define PN533_TYPE_A_SEL_PROT_DEP 2
671 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
673 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a
*type_a
,
679 if (target_data_len
< sizeof(struct pn533_target_type_a
))
683 * The length check of nfcid[] and ats[] are not being performed because
684 * the values are not being used
687 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
688 ssd
= PN533_TYPE_A_SENS_RES_SSD(type_a
->sens_res
);
689 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(type_a
->sens_res
);
691 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
692 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
693 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
694 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
697 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
698 if (PN533_TYPE_A_SEL_CASCADE(type_a
->sel_res
) != 0)
701 if (type_a
->nfcid_len
> NFC_NFCID1_MAXSIZE
)
707 static int pn533_target_found_type_a(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
710 struct pn533_target_type_a
*tgt_type_a
;
712 tgt_type_a
= (struct pn533_target_type_a
*)tgt_data
;
714 if (!pn533_target_type_a_is_valid(tgt_type_a
, tgt_data_len
))
717 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a
->sel_res
)) {
718 case PN533_TYPE_A_SEL_PROT_MIFARE
:
719 nfc_tgt
->supported_protocols
= NFC_PROTO_MIFARE_MASK
;
721 case PN533_TYPE_A_SEL_PROT_ISO14443
:
722 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
;
724 case PN533_TYPE_A_SEL_PROT_DEP
:
725 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
727 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP
:
728 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_MASK
|
729 NFC_PROTO_NFC_DEP_MASK
;
733 nfc_tgt
->sens_res
= be16_to_cpu(tgt_type_a
->sens_res
);
734 nfc_tgt
->sel_res
= tgt_type_a
->sel_res
;
735 nfc_tgt
->nfcid1_len
= tgt_type_a
->nfcid_len
;
736 memcpy(nfc_tgt
->nfcid1
, tgt_type_a
->nfcid_data
, nfc_tgt
->nfcid1_len
);
741 struct pn533_target_felica
{
744 u8 nfcid2
[NFC_NFCID2_MAXSIZE
];
750 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
751 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
753 static bool pn533_target_felica_is_valid(struct pn533_target_felica
*felica
,
756 if (target_data_len
< sizeof(struct pn533_target_felica
))
759 if (felica
->opcode
!= PN533_FELICA_OPC_SENSF_RES
)
765 static int pn533_target_found_felica(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
768 struct pn533_target_felica
*tgt_felica
;
770 tgt_felica
= (struct pn533_target_felica
*)tgt_data
;
772 if (!pn533_target_felica_is_valid(tgt_felica
, tgt_data_len
))
775 if ((tgt_felica
->nfcid2
[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1
) &&
776 (tgt_felica
->nfcid2
[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2
))
777 nfc_tgt
->supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
779 nfc_tgt
->supported_protocols
= NFC_PROTO_FELICA_MASK
;
781 memcpy(nfc_tgt
->sensf_res
, &tgt_felica
->opcode
, 9);
782 nfc_tgt
->sensf_res_len
= 9;
784 memcpy(nfc_tgt
->nfcid2
, tgt_felica
->nfcid2
, NFC_NFCID2_MAXSIZE
);
785 nfc_tgt
->nfcid2_len
= NFC_NFCID2_MAXSIZE
;
790 struct pn533_target_jewel
{
795 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel
*jewel
,
801 if (target_data_len
< sizeof(struct pn533_target_jewel
))
804 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
805 ssd
= PN533_TYPE_A_SENS_RES_SSD(jewel
->sens_res
);
806 platconf
= PN533_TYPE_A_SENS_RES_PLATCONF(jewel
->sens_res
);
808 if ((ssd
== PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
809 platconf
!= PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
) ||
810 (ssd
!= PN533_TYPE_A_SENS_RES_SSD_JEWEL
&&
811 platconf
== PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL
))
817 static int pn533_target_found_jewel(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
820 struct pn533_target_jewel
*tgt_jewel
;
822 tgt_jewel
= (struct pn533_target_jewel
*)tgt_data
;
824 if (!pn533_target_jewel_is_valid(tgt_jewel
, tgt_data_len
))
827 nfc_tgt
->supported_protocols
= NFC_PROTO_JEWEL_MASK
;
828 nfc_tgt
->sens_res
= be16_to_cpu(tgt_jewel
->sens_res
);
829 nfc_tgt
->nfcid1_len
= 4;
830 memcpy(nfc_tgt
->nfcid1
, tgt_jewel
->jewelid
, nfc_tgt
->nfcid1_len
);
835 struct pn533_type_b_prot_info
{
841 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
842 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
843 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
845 struct pn533_type_b_sens_res
{
849 struct pn533_type_b_prot_info prot_info
;
852 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
854 struct pn533_target_type_b
{
855 struct pn533_type_b_sens_res sensb_res
;
860 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b
*type_b
,
863 if (target_data_len
< sizeof(struct pn533_target_type_b
))
866 if (type_b
->sensb_res
.opcode
!= PN533_TYPE_B_OPC_SENSB_RES
)
869 if (PN533_TYPE_B_PROT_TYPE(type_b
->sensb_res
.prot_info
.fsci_type
) &
870 PN533_TYPE_B_PROT_TYPE_RFU_MASK
)
876 static int pn533_target_found_type_b(struct nfc_target
*nfc_tgt
, u8
*tgt_data
,
879 struct pn533_target_type_b
*tgt_type_b
;
881 tgt_type_b
= (struct pn533_target_type_b
*)tgt_data
;
883 if (!pn533_target_type_b_is_valid(tgt_type_b
, tgt_data_len
))
886 nfc_tgt
->supported_protocols
= NFC_PROTO_ISO14443_B_MASK
;
891 static void pn533_poll_reset_mod_list(struct pn533
*dev
);
892 static int pn533_target_found(struct pn533
*dev
, u8 tg
, u8
*tgdata
,
895 struct nfc_target nfc_tgt
;
898 dev_dbg(dev
->dev
, "%s: modulation=%d\n",
899 __func__
, dev
->poll_mod_curr
);
904 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
906 switch (dev
->poll_mod_curr
) {
907 case PN533_POLL_MOD_106KBPS_A
:
908 rc
= pn533_target_found_type_a(&nfc_tgt
, tgdata
, tgdata_len
);
910 case PN533_POLL_MOD_212KBPS_FELICA
:
911 case PN533_POLL_MOD_424KBPS_FELICA
:
912 rc
= pn533_target_found_felica(&nfc_tgt
, tgdata
, tgdata_len
);
914 case PN533_POLL_MOD_106KBPS_JEWEL
:
915 rc
= pn533_target_found_jewel(&nfc_tgt
, tgdata
, tgdata_len
);
917 case PN533_POLL_MOD_847KBPS_B
:
918 rc
= pn533_target_found_type_b(&nfc_tgt
, tgdata
, tgdata_len
);
922 "Unknown current poll modulation\n");
929 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
931 "The Tg found doesn't have the desired protocol\n");
936 "Target found - supported protocols: 0x%x\n",
937 nfc_tgt
.supported_protocols
);
939 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
941 pn533_poll_reset_mod_list(dev
);
942 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
947 static inline void pn533_poll_next_mod(struct pn533
*dev
)
949 dev
->poll_mod_curr
= (dev
->poll_mod_curr
+ 1) % dev
->poll_mod_count
;
952 static void pn533_poll_reset_mod_list(struct pn533
*dev
)
954 dev
->poll_mod_count
= 0;
957 static void pn533_poll_add_mod(struct pn533
*dev
, u8 mod_index
)
959 dev
->poll_mod_active
[dev
->poll_mod_count
] =
960 (struct pn533_poll_modulations
*)&poll_mod
[mod_index
];
961 dev
->poll_mod_count
++;
964 static void pn533_poll_create_mod_list(struct pn533
*dev
,
965 u32 im_protocols
, u32 tm_protocols
)
967 pn533_poll_reset_mod_list(dev
);
969 if ((im_protocols
& NFC_PROTO_MIFARE_MASK
) ||
970 (im_protocols
& NFC_PROTO_ISO14443_MASK
) ||
971 (im_protocols
& NFC_PROTO_NFC_DEP_MASK
))
972 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_A
);
974 if (im_protocols
& NFC_PROTO_FELICA_MASK
||
975 im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
976 pn533_poll_add_mod(dev
, PN533_POLL_MOD_212KBPS_FELICA
);
977 pn533_poll_add_mod(dev
, PN533_POLL_MOD_424KBPS_FELICA
);
980 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
981 pn533_poll_add_mod(dev
, PN533_POLL_MOD_106KBPS_JEWEL
);
983 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
984 pn533_poll_add_mod(dev
, PN533_POLL_MOD_847KBPS_B
);
987 pn533_poll_add_mod(dev
, PN533_LISTEN_MOD
);
990 static int pn533_start_poll_complete(struct pn533
*dev
, struct sk_buff
*resp
)
992 u8 nbtg
, tg
, *tgdata
;
995 /* Toggle the DEP polling */
996 if (dev
->poll_protocols
& NFC_PROTO_NFC_DEP_MASK
)
999 nbtg
= resp
->data
[0];
1001 tgdata
= &resp
->data
[2];
1002 tgdata_len
= resp
->len
- 2; /* nbtg + tg */
1005 rc
= pn533_target_found(dev
, tg
, tgdata
, tgdata_len
);
1007 /* We must stop the poll after a valid target found */
1015 static struct sk_buff
*pn533_alloc_poll_tg_frame(struct pn533
*dev
)
1017 struct sk_buff
*skb
;
1018 u8
*felica
, *nfcid3
;
1020 u8
*gbytes
= dev
->gb
;
1021 size_t gbytes_len
= dev
->gb_len
;
1023 u8 felica_params
[18] = {0x1, 0xfe, /* DEP */
1024 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1025 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1026 0xff, 0xff}; /* System code */
1028 u8 mifare_params
[6] = {0x1, 0x1, /* SENS_RES */
1030 0x40}; /* SEL_RES for DEP */
1032 unsigned int skb_len
= 36 + /*
1033 * mode (1), mifare (6),
1034 * felica (18), nfcid3 (10), gb_len (1)
1039 skb
= pn533_alloc_skb(dev
, skb_len
);
1043 /* DEP support only */
1044 skb_put_u8(skb
, PN533_INIT_TARGET_DEP
);
1047 skb_put_data(skb
, mifare_params
, 6);
1050 felica
= skb_put_data(skb
, felica_params
, 18);
1051 get_random_bytes(felica
+ 2, 6);
1054 nfcid3
= skb_put_zero(skb
, 10);
1055 memcpy(nfcid3
, felica
, 8);
1058 skb_put_u8(skb
, gbytes_len
);
1060 skb_put_data(skb
, gbytes
, gbytes_len
);
1068 static void pn533_wq_tm_mi_recv(struct work_struct
*work
);
1069 static struct sk_buff
*pn533_build_response(struct pn533
*dev
);
1071 static int pn533_tm_get_data_complete(struct pn533
*dev
, void *arg
,
1072 struct sk_buff
*resp
)
1074 struct sk_buff
*skb
;
1079 skb_queue_purge(&dev
->resp_q
);
1080 return PTR_ERR(resp
);
1083 status
= resp
->data
[0];
1085 ret
= status
& PN533_CMD_RET_MASK
;
1086 mi
= status
& PN533_CMD_MI_MASK
;
1088 skb_pull(resp
, sizeof(status
));
1090 if (ret
!= PN533_CMD_RET_SUCCESS
) {
1095 skb_queue_tail(&dev
->resp_q
, resp
);
1098 queue_work(dev
->wq
, &dev
->mi_tm_rx_work
);
1099 return -EINPROGRESS
;
1102 skb
= pn533_build_response(dev
);
1108 return nfc_tm_data_received(dev
->nfc_dev
, skb
);
1111 nfc_tm_deactivated(dev
->nfc_dev
);
1113 skb_queue_purge(&dev
->resp_q
);
1114 dev_kfree_skb(resp
);
1119 static void pn533_wq_tm_mi_recv(struct work_struct
*work
)
1121 struct pn533
*dev
= container_of(work
, struct pn533
, mi_tm_rx_work
);
1122 struct sk_buff
*skb
;
1125 skb
= pn533_alloc_skb(dev
, 0);
1129 rc
= pn533_send_cmd_direct_async(dev
,
1130 PN533_CMD_TG_GET_DATA
,
1132 pn533_tm_get_data_complete
,
1139 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
1140 struct sk_buff
*resp
);
1141 static void pn533_wq_tm_mi_send(struct work_struct
*work
)
1143 struct pn533
*dev
= container_of(work
, struct pn533
, mi_tm_tx_work
);
1144 struct sk_buff
*skb
;
1147 /* Grab the first skb in the queue */
1148 skb
= skb_dequeue(&dev
->fragment_skb
);
1149 if (skb
== NULL
) { /* No more data */
1150 /* Reset the queue for future use */
1151 skb_queue_head_init(&dev
->fragment_skb
);
1155 /* last entry - remove MI bit */
1156 if (skb_queue_len(&dev
->fragment_skb
) == 0) {
1157 rc
= pn533_send_cmd_direct_async(dev
, PN533_CMD_TG_SET_DATA
,
1158 skb
, pn533_tm_send_complete
, NULL
);
1160 rc
= pn533_send_cmd_direct_async(dev
,
1161 PN533_CMD_TG_SET_META_DATA
,
1162 skb
, pn533_tm_send_complete
, NULL
);
1164 if (rc
== 0) /* success */
1168 "Error %d when trying to perform set meta data_exchange", rc
);
1173 dev
->phy_ops
->send_ack(dev
, GFP_KERNEL
);
1174 queue_work(dev
->wq
, &dev
->cmd_work
);
1177 static void pn533_wq_tg_get_data(struct work_struct
*work
)
1179 struct pn533
*dev
= container_of(work
, struct pn533
, tg_work
);
1180 struct sk_buff
*skb
;
1183 skb
= pn533_alloc_skb(dev
, 0);
1187 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_GET_DATA
, skb
,
1188 pn533_tm_get_data_complete
, NULL
);
1194 #define ATR_REQ_GB_OFFSET 17
1195 static int pn533_init_target_complete(struct pn533
*dev
, struct sk_buff
*resp
)
1197 u8 mode
, *cmd
, comm_mode
= NFC_COMM_PASSIVE
, *gb
;
1201 if (resp
->len
< ATR_REQ_GB_OFFSET
+ 1)
1204 mode
= resp
->data
[0];
1205 cmd
= &resp
->data
[1];
1207 dev_dbg(dev
->dev
, "Target mode 0x%x len %d\n",
1210 if ((mode
& PN533_INIT_TARGET_RESP_FRAME_MASK
) ==
1211 PN533_INIT_TARGET_RESP_ACTIVE
)
1212 comm_mode
= NFC_COMM_ACTIVE
;
1214 if ((mode
& PN533_INIT_TARGET_RESP_DEP
) == 0) /* Only DEP supported */
1217 gb
= cmd
+ ATR_REQ_GB_OFFSET
;
1218 gb_len
= resp
->len
- (ATR_REQ_GB_OFFSET
+ 1);
1220 rc
= nfc_tm_activated(dev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1221 comm_mode
, gb
, gb_len
);
1224 "Error when signaling target activation\n");
1229 queue_work(dev
->wq
, &dev
->tg_work
);
1234 static void pn533_listen_mode_timer(struct timer_list
*t
)
1236 struct pn533
*dev
= from_timer(dev
, t
, listen_timer
);
1238 dev
->cancel_listen
= 1;
1240 pn533_poll_next_mod(dev
);
1242 queue_delayed_work(dev
->wq
, &dev
->poll_work
,
1243 msecs_to_jiffies(PN533_POLL_INTERVAL
));
1246 static int pn533_rf_complete(struct pn533
*dev
, void *arg
,
1247 struct sk_buff
*resp
)
1254 nfc_err(dev
->dev
, "RF setting error %d\n", rc
);
1259 queue_delayed_work(dev
->wq
, &dev
->poll_work
,
1260 msecs_to_jiffies(PN533_POLL_INTERVAL
));
1262 dev_kfree_skb(resp
);
1266 static void pn533_wq_rf(struct work_struct
*work
)
1268 struct pn533
*dev
= container_of(work
, struct pn533
, rf_work
);
1269 struct sk_buff
*skb
;
1272 skb
= pn533_alloc_skb(dev
, 2);
1276 skb_put_u8(skb
, PN533_CFGITEM_RF_FIELD
);
1277 skb_put_u8(skb
, PN533_CFGITEM_RF_FIELD_AUTO_RFCA
);
1279 rc
= pn533_send_cmd_async(dev
, PN533_CMD_RF_CONFIGURATION
, skb
,
1280 pn533_rf_complete
, NULL
);
1283 nfc_err(dev
->dev
, "RF setting error %d\n", rc
);
1287 static int pn533_poll_dep_complete(struct pn533
*dev
, void *arg
,
1288 struct sk_buff
*resp
)
1290 struct pn533_cmd_jump_dep_response
*rsp
;
1291 struct nfc_target nfc_target
;
1296 return PTR_ERR(resp
);
1298 memset(&nfc_target
, 0, sizeof(struct nfc_target
));
1300 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1302 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1303 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1304 /* Not target found, turn radio off */
1305 queue_work(dev
->wq
, &dev
->rf_work
);
1307 dev_kfree_skb(resp
);
1311 dev_dbg(dev
->dev
, "Creating new target");
1313 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1314 nfc_target
.nfcid1_len
= 10;
1315 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
1316 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1320 dev
->tgt_available_prots
= 0;
1321 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1323 /* ATR_RES general bytes are located at offset 17 */
1324 target_gt_len
= resp
->len
- 17;
1325 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1326 rsp
->gt
, target_gt_len
);
1328 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1329 dev
->nfc_dev
->targets
[0].idx
,
1330 0, NFC_RF_INITIATOR
);
1333 pn533_poll_reset_mod_list(dev
);
1336 dev_kfree_skb(resp
);
1340 #define PASSIVE_DATA_LEN 5
1341 static int pn533_poll_dep(struct nfc_dev
*nfc_dev
)
1343 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1344 struct sk_buff
*skb
;
1346 u8
*next
, nfcid3
[NFC_NFCID3_MAXSIZE
];
1347 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1350 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1352 if (!dev
->gb
|| !dev
->gb_len
) {
1354 queue_work(dev
->wq
, &dev
->rf_work
);
1358 skb_len
= 3 + dev
->gb_len
; /* ActPass + BR + Next */
1359 skb_len
+= PASSIVE_DATA_LEN
;
1362 skb_len
+= NFC_NFCID3_MAXSIZE
;
1365 get_random_bytes(nfcid3
+ 2, 6);
1367 skb
= pn533_alloc_skb(dev
, skb_len
);
1371 skb_put_u8(skb
, 0x01); /* Active */
1372 skb_put_u8(skb
, 0x02); /* 424 kbps */
1374 next
= skb_put(skb
, 1); /* Next */
1377 /* Copy passive data */
1378 skb_put_data(skb
, passive_data
, PASSIVE_DATA_LEN
);
1381 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1382 skb_put_data(skb
, nfcid3
, NFC_NFCID3_MAXSIZE
);
1385 skb_put_data(skb
, dev
->gb
, dev
->gb_len
);
1386 *next
|= 4; /* We have some Gi */
1388 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
1389 pn533_poll_dep_complete
, NULL
);
1397 static int pn533_autopoll_complete(struct pn533
*dev
, void *arg
,
1398 struct sk_buff
*resp
)
1400 struct pn532_autopoll_resp
*apr
;
1401 struct nfc_target nfc_tgt
;
1408 nfc_err(dev
->dev
, "%s autopoll complete error %d\n",
1411 if (rc
== -ENOENT
) {
1412 if (dev
->poll_mod_count
!= 0)
1415 } else if (rc
< 0) {
1417 "Error %d when running autopoll\n", rc
);
1422 nbtg
= resp
->data
[0];
1423 if ((nbtg
> 2) || (nbtg
<= 0))
1426 apr
= (struct pn532_autopoll_resp
*)&resp
->data
[1];
1428 memset(&nfc_tgt
, 0, sizeof(struct nfc_target
));
1429 switch (apr
->type
) {
1430 case PN532_AUTOPOLL_TYPE_ISOA
:
1431 dev_dbg(dev
->dev
, "ISOA\n");
1432 rc
= pn533_target_found_type_a(&nfc_tgt
, apr
->tgdata
,
1435 case PN532_AUTOPOLL_TYPE_FELICA212
:
1436 case PN532_AUTOPOLL_TYPE_FELICA424
:
1437 dev_dbg(dev
->dev
, "FELICA\n");
1438 rc
= pn533_target_found_felica(&nfc_tgt
, apr
->tgdata
,
1441 case PN532_AUTOPOLL_TYPE_JEWEL
:
1442 dev_dbg(dev
->dev
, "JEWEL\n");
1443 rc
= pn533_target_found_jewel(&nfc_tgt
, apr
->tgdata
,
1446 case PN532_AUTOPOLL_TYPE_ISOB
:
1447 dev_dbg(dev
->dev
, "ISOB\n");
1448 rc
= pn533_target_found_type_b(&nfc_tgt
, apr
->tgdata
,
1451 case PN532_AUTOPOLL_TYPE_MIFARE
:
1452 dev_dbg(dev
->dev
, "Mifare\n");
1453 rc
= pn533_target_found_type_a(&nfc_tgt
, apr
->tgdata
,
1458 "Unknown current poll modulation\n");
1465 if (!(nfc_tgt
.supported_protocols
& dev
->poll_protocols
)) {
1467 "The Tg found doesn't have the desired protocol\n");
1472 dev
->tgt_available_prots
= nfc_tgt
.supported_protocols
;
1473 apr
= (struct pn532_autopoll_resp
*)
1474 (apr
->tgdata
+ (apr
->ln
- 1));
1477 pn533_poll_reset_mod_list(dev
);
1478 nfc_targets_found(dev
->nfc_dev
, &nfc_tgt
, 1);
1481 dev_kfree_skb(resp
);
1485 nfc_err(dev
->dev
, "autopoll operation has been stopped\n");
1487 pn533_poll_reset_mod_list(dev
);
1488 dev
->poll_protocols
= 0;
1492 static int pn533_poll_complete(struct pn533
*dev
, void *arg
,
1493 struct sk_buff
*resp
)
1495 struct pn533_poll_modulations
*cur_mod
;
1501 nfc_err(dev
->dev
, "%s Poll complete error %d\n",
1504 if (rc
== -ENOENT
) {
1505 if (dev
->poll_mod_count
!= 0)
1508 } else if (rc
< 0) {
1510 "Error %d when running poll\n", rc
);
1515 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1517 if (cur_mod
->len
== 0) { /* Target mode */
1518 del_timer(&dev
->listen_timer
);
1519 rc
= pn533_init_target_complete(dev
, resp
);
1523 /* Initiator mode */
1524 rc
= pn533_start_poll_complete(dev
, resp
);
1528 if (!dev
->poll_mod_count
) {
1529 dev_dbg(dev
->dev
, "Polling has been stopped\n");
1533 pn533_poll_next_mod(dev
);
1534 /* Not target found, turn radio off */
1535 queue_work(dev
->wq
, &dev
->rf_work
);
1538 dev_kfree_skb(resp
);
1542 nfc_err(dev
->dev
, "Polling operation has been stopped\n");
1544 pn533_poll_reset_mod_list(dev
);
1545 dev
->poll_protocols
= 0;
1549 static struct sk_buff
*pn533_alloc_poll_in_frame(struct pn533
*dev
,
1550 struct pn533_poll_modulations
*mod
)
1552 struct sk_buff
*skb
;
1554 skb
= pn533_alloc_skb(dev
, mod
->len
);
1558 skb_put_data(skb
, &mod
->data
, mod
->len
);
1563 static int pn533_send_poll_frame(struct pn533
*dev
)
1565 struct pn533_poll_modulations
*mod
;
1566 struct sk_buff
*skb
;
1570 mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1572 dev_dbg(dev
->dev
, "%s mod len %d\n",
1573 __func__
, mod
->len
);
1575 if ((dev
->poll_protocols
& NFC_PROTO_NFC_DEP_MASK
) && dev
->poll_dep
) {
1577 return pn533_poll_dep(dev
->nfc_dev
);
1580 if (mod
->len
== 0) { /* Listen mode */
1581 cmd_code
= PN533_CMD_TG_INIT_AS_TARGET
;
1582 skb
= pn533_alloc_poll_tg_frame(dev
);
1583 } else { /* Polling mode */
1584 cmd_code
= PN533_CMD_IN_LIST_PASSIVE_TARGET
;
1585 skb
= pn533_alloc_poll_in_frame(dev
, mod
);
1589 nfc_err(dev
->dev
, "Failed to allocate skb\n");
1593 rc
= pn533_send_cmd_async(dev
, cmd_code
, skb
, pn533_poll_complete
,
1597 nfc_err(dev
->dev
, "Polling loop error %d\n", rc
);
1603 static void pn533_wq_poll(struct work_struct
*work
)
1605 struct pn533
*dev
= container_of(work
, struct pn533
, poll_work
.work
);
1606 struct pn533_poll_modulations
*cur_mod
;
1609 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1612 "%s cancel_listen %d modulation len %d\n",
1613 __func__
, dev
->cancel_listen
, cur_mod
->len
);
1615 if (dev
->cancel_listen
== 1) {
1616 dev
->cancel_listen
= 0;
1617 dev
->phy_ops
->abort_cmd(dev
, GFP_ATOMIC
);
1620 rc
= pn533_send_poll_frame(dev
);
1624 if (cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1625 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1628 static int pn533_start_poll(struct nfc_dev
*nfc_dev
,
1629 u32 im_protocols
, u32 tm_protocols
)
1631 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1632 struct pn533_poll_modulations
*cur_mod
;
1633 struct sk_buff
*skb
;
1638 "%s: im protocols 0x%x tm protocols 0x%x\n",
1639 __func__
, im_protocols
, tm_protocols
);
1641 if (dev
->tgt_active_prot
) {
1643 "Cannot poll with a target already activated\n");
1647 if (dev
->tgt_mode
) {
1649 "Cannot poll while already being activated\n");
1654 dev
->gb
= nfc_get_local_general_bytes(nfc_dev
, &dev
->gb_len
);
1655 if (dev
->gb
== NULL
)
1659 dev
->poll_protocols
= im_protocols
;
1660 dev
->listen_protocols
= tm_protocols
;
1661 if (dev
->device_type
== PN533_DEVICE_PN532_AUTOPOLL
) {
1662 skb
= pn533_alloc_skb(dev
, 4 + 6);
1666 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1667 PN532_AUTOPOLL_POLLNR_INFINITE
;
1668 *((u8
*)skb_put(skb
, sizeof(u8
))) = PN532_AUTOPOLL_PERIOD
;
1670 if ((im_protocols
& NFC_PROTO_MIFARE_MASK
) &&
1671 (im_protocols
& NFC_PROTO_ISO14443_MASK
) &&
1672 (im_protocols
& NFC_PROTO_NFC_DEP_MASK
))
1673 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1674 PN532_AUTOPOLL_TYPE_GENERIC_106
;
1676 if (im_protocols
& NFC_PROTO_MIFARE_MASK
)
1677 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1678 PN532_AUTOPOLL_TYPE_MIFARE
;
1680 if (im_protocols
& NFC_PROTO_ISO14443_MASK
)
1681 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1682 PN532_AUTOPOLL_TYPE_ISOA
;
1684 if (im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1685 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1686 PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106
;
1687 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1688 PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212
;
1689 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1690 PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424
;
1694 if (im_protocols
& NFC_PROTO_FELICA_MASK
||
1695 im_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
1696 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1697 PN532_AUTOPOLL_TYPE_FELICA212
;
1698 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1699 PN532_AUTOPOLL_TYPE_FELICA424
;
1702 if (im_protocols
& NFC_PROTO_JEWEL_MASK
)
1703 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1704 PN532_AUTOPOLL_TYPE_JEWEL
;
1706 if (im_protocols
& NFC_PROTO_ISO14443_B_MASK
)
1707 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1708 PN532_AUTOPOLL_TYPE_ISOB
;
1711 *((u8
*)skb_put(skb
, sizeof(u8
))) =
1712 PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106
;
1714 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_AUTOPOLL
, skb
,
1715 pn533_autopoll_complete
, NULL
);
1720 dev
->poll_mod_count
++;
1725 pn533_poll_create_mod_list(dev
, im_protocols
, tm_protocols
);
1726 if (!dev
->poll_mod_count
) {
1728 "Poll mod list is empty\n");
1732 /* Do not always start polling from the same modulation */
1733 get_random_bytes(&rand_mod
, sizeof(rand_mod
));
1734 rand_mod
%= dev
->poll_mod_count
;
1735 dev
->poll_mod_curr
= rand_mod
;
1737 cur_mod
= dev
->poll_mod_active
[dev
->poll_mod_curr
];
1739 rc
= pn533_send_poll_frame(dev
);
1741 /* Start listen timer */
1742 if (!rc
&& cur_mod
->len
== 0 && dev
->poll_mod_count
> 1)
1743 mod_timer(&dev
->listen_timer
, jiffies
+ PN533_LISTEN_TIME
* HZ
);
1748 static void pn533_stop_poll(struct nfc_dev
*nfc_dev
)
1750 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1752 del_timer(&dev
->listen_timer
);
1754 if (!dev
->poll_mod_count
) {
1756 "Polling operation was not running\n");
1760 dev
->phy_ops
->abort_cmd(dev
, GFP_KERNEL
);
1761 flush_delayed_work(&dev
->poll_work
);
1762 pn533_poll_reset_mod_list(dev
);
1765 static int pn533_activate_target_nfcdep(struct pn533
*dev
)
1767 struct pn533_cmd_activate_response
*rsp
;
1770 struct sk_buff
*skb
;
1771 struct sk_buff
*resp
;
1773 skb
= pn533_alloc_skb(dev
, sizeof(u8
) * 2); /*TG + Next*/
1777 skb_put_u8(skb
, 1); /* TG */
1778 skb_put_u8(skb
, 0); /* Next */
1780 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_IN_ATR
, skb
);
1782 return PTR_ERR(resp
);
1784 rsp
= (struct pn533_cmd_activate_response
*)resp
->data
;
1785 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1786 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1788 "Target activation failed (error 0x%x)\n", rc
);
1789 dev_kfree_skb(resp
);
1793 /* ATR_RES general bytes are located at offset 16 */
1794 gt_len
= resp
->len
- 16;
1795 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
, rsp
->gt
, gt_len
);
1797 dev_kfree_skb(resp
);
1801 static int pn533_activate_target(struct nfc_dev
*nfc_dev
,
1802 struct nfc_target
*target
, u32 protocol
)
1804 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1807 dev_dbg(dev
->dev
, "%s: protocol=%u\n", __func__
, protocol
);
1809 if (dev
->poll_mod_count
) {
1811 "Cannot activate while polling\n");
1815 if (dev
->tgt_active_prot
) {
1817 "There is already an active target\n");
1821 if (!dev
->tgt_available_prots
) {
1823 "There is no available target to activate\n");
1827 if (!(dev
->tgt_available_prots
& (1 << protocol
))) {
1829 "Target doesn't support requested proto %u\n",
1834 if (protocol
== NFC_PROTO_NFC_DEP
) {
1835 rc
= pn533_activate_target_nfcdep(dev
);
1838 "Activating target with DEP failed %d\n", rc
);
1843 dev
->tgt_active_prot
= protocol
;
1844 dev
->tgt_available_prots
= 0;
1849 static int pn533_deactivate_target_complete(struct pn533
*dev
, void *arg
,
1850 struct sk_buff
*resp
)
1857 nfc_err(dev
->dev
, "Target release error %d\n", rc
);
1862 rc
= resp
->data
[0] & PN533_CMD_RET_MASK
;
1863 if (rc
!= PN533_CMD_RET_SUCCESS
)
1865 "Error 0x%x when releasing the target\n", rc
);
1867 dev_kfree_skb(resp
);
1871 static void pn533_deactivate_target(struct nfc_dev
*nfc_dev
,
1872 struct nfc_target
*target
, u8 mode
)
1874 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1875 struct sk_buff
*skb
;
1878 if (!dev
->tgt_active_prot
) {
1879 nfc_err(dev
->dev
, "There is no active target\n");
1883 dev
->tgt_active_prot
= 0;
1884 skb_queue_purge(&dev
->resp_q
);
1886 skb
= pn533_alloc_skb(dev
, sizeof(u8
));
1890 skb_put_u8(skb
, 1); /* TG*/
1892 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_RELEASE
, skb
,
1893 pn533_deactivate_target_complete
, NULL
);
1896 nfc_err(dev
->dev
, "Target release error %d\n", rc
);
1901 static int pn533_in_dep_link_up_complete(struct pn533
*dev
, void *arg
,
1902 struct sk_buff
*resp
)
1904 struct pn533_cmd_jump_dep_response
*rsp
;
1907 u8 active
= *(u8
*)arg
;
1912 return PTR_ERR(resp
);
1914 if (dev
->tgt_available_prots
&&
1915 !(dev
->tgt_available_prots
& (1 << NFC_PROTO_NFC_DEP
))) {
1917 "The target does not support DEP\n");
1922 rsp
= (struct pn533_cmd_jump_dep_response
*)resp
->data
;
1924 rc
= rsp
->status
& PN533_CMD_RET_MASK
;
1925 if (rc
!= PN533_CMD_RET_SUCCESS
) {
1927 "Bringing DEP link up failed (error 0x%x)\n", rc
);
1931 if (!dev
->tgt_available_prots
) {
1932 struct nfc_target nfc_target
;
1934 dev_dbg(dev
->dev
, "Creating new target\n");
1936 memset(&nfc_target
, 0, sizeof(struct nfc_target
));
1938 nfc_target
.supported_protocols
= NFC_PROTO_NFC_DEP_MASK
;
1939 nfc_target
.nfcid1_len
= 10;
1940 memcpy(nfc_target
.nfcid1
, rsp
->nfcid3t
, nfc_target
.nfcid1_len
);
1941 rc
= nfc_targets_found(dev
->nfc_dev
, &nfc_target
, 1);
1945 dev
->tgt_available_prots
= 0;
1948 dev
->tgt_active_prot
= NFC_PROTO_NFC_DEP
;
1950 /* ATR_RES general bytes are located at offset 17 */
1951 target_gt_len
= resp
->len
- 17;
1952 rc
= nfc_set_remote_general_bytes(dev
->nfc_dev
,
1953 rsp
->gt
, target_gt_len
);
1955 rc
= nfc_dep_link_is_up(dev
->nfc_dev
,
1956 dev
->nfc_dev
->targets
[0].idx
,
1957 !active
, NFC_RF_INITIATOR
);
1960 dev_kfree_skb(resp
);
1964 static int pn533_rf_field(struct nfc_dev
*nfc_dev
, u8 rf
);
1965 static int pn533_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1966 u8 comm_mode
, u8
*gb
, size_t gb_len
)
1968 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
1969 struct sk_buff
*skb
;
1971 u8
*next
, *arg
, nfcid3
[NFC_NFCID3_MAXSIZE
];
1972 u8 passive_data
[PASSIVE_DATA_LEN
] = {0x00, 0xff, 0xff, 0x00, 0x3};
1974 if (dev
->poll_mod_count
) {
1976 "Cannot bring the DEP link up while polling\n");
1980 if (dev
->tgt_active_prot
) {
1982 "There is already an active target\n");
1986 skb_len
= 3 + gb_len
; /* ActPass + BR + Next */
1987 skb_len
+= PASSIVE_DATA_LEN
;
1990 skb_len
+= NFC_NFCID3_MAXSIZE
;
1991 if (target
&& !target
->nfcid2_len
) {
1994 get_random_bytes(nfcid3
+ 2, 6);
1997 skb
= pn533_alloc_skb(dev
, skb_len
);
2001 skb_put_u8(skb
, !comm_mode
); /* ActPass */
2002 skb_put_u8(skb
, 0x02); /* 424 kbps */
2004 next
= skb_put(skb
, 1); /* Next */
2007 /* Copy passive data */
2008 skb_put_data(skb
, passive_data
, PASSIVE_DATA_LEN
);
2011 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2012 if (target
&& target
->nfcid2_len
)
2013 memcpy(skb_put(skb
, NFC_NFCID3_MAXSIZE
), target
->nfcid2
,
2014 target
->nfcid2_len
);
2016 skb_put_data(skb
, nfcid3
, NFC_NFCID3_MAXSIZE
);
2019 if (gb
!= NULL
&& gb_len
> 0) {
2020 skb_put_data(skb
, gb
, gb_len
);
2021 *next
|= 4; /* We have some Gi */
2026 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2034 pn533_rf_field(dev
->nfc_dev
, 0);
2036 rc
= pn533_send_cmd_async(dev
, PN533_CMD_IN_JUMP_FOR_DEP
, skb
,
2037 pn533_in_dep_link_up_complete
, arg
);
2047 static int pn533_dep_link_down(struct nfc_dev
*nfc_dev
)
2049 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2051 pn533_poll_reset_mod_list(dev
);
2053 if (dev
->tgt_mode
|| dev
->tgt_active_prot
)
2054 dev
->phy_ops
->abort_cmd(dev
, GFP_KERNEL
);
2056 dev
->tgt_active_prot
= 0;
2059 skb_queue_purge(&dev
->resp_q
);
2064 struct pn533_data_exchange_arg
{
2065 data_exchange_cb_t cb
;
2069 static struct sk_buff
*pn533_build_response(struct pn533
*dev
)
2071 struct sk_buff
*skb
, *tmp
, *t
;
2072 unsigned int skb_len
= 0, tmp_len
= 0;
2074 if (skb_queue_empty(&dev
->resp_q
))
2077 if (skb_queue_len(&dev
->resp_q
) == 1) {
2078 skb
= skb_dequeue(&dev
->resp_q
);
2082 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
)
2083 skb_len
+= tmp
->len
;
2085 dev_dbg(dev
->dev
, "%s total length %d\n",
2088 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
2092 skb_put(skb
, skb_len
);
2094 skb_queue_walk_safe(&dev
->resp_q
, tmp
, t
) {
2095 memcpy(skb
->data
+ tmp_len
, tmp
->data
, tmp
->len
);
2096 tmp_len
+= tmp
->len
;
2100 skb_queue_purge(&dev
->resp_q
);
2105 static int pn533_data_exchange_complete(struct pn533
*dev
, void *_arg
,
2106 struct sk_buff
*resp
)
2108 struct pn533_data_exchange_arg
*arg
= _arg
;
2109 struct sk_buff
*skb
;
2118 status
= resp
->data
[0];
2119 ret
= status
& PN533_CMD_RET_MASK
;
2120 mi
= status
& PN533_CMD_MI_MASK
;
2122 skb_pull(resp
, sizeof(status
));
2124 if (ret
!= PN533_CMD_RET_SUCCESS
) {
2126 "Exchanging data failed (error 0x%x)\n", ret
);
2131 skb_queue_tail(&dev
->resp_q
, resp
);
2134 dev
->cmd_complete_mi_arg
= arg
;
2135 queue_work(dev
->wq
, &dev
->mi_rx_work
);
2136 return -EINPROGRESS
;
2139 /* Prepare for the next round */
2140 if (skb_queue_len(&dev
->fragment_skb
) > 0) {
2141 dev
->cmd_complete_dep_arg
= arg
;
2142 queue_work(dev
->wq
, &dev
->mi_tx_work
);
2144 return -EINPROGRESS
;
2147 skb
= pn533_build_response(dev
);
2153 arg
->cb(arg
->cb_context
, skb
, 0);
2158 dev_kfree_skb(resp
);
2160 skb_queue_purge(&dev
->resp_q
);
2161 arg
->cb(arg
->cb_context
, NULL
, rc
);
2167 * Receive an incoming pn533 frame. skb contains only header and payload.
2168 * If skb == NULL, it is a notification that the link below is dead.
2170 void pn533_recv_frame(struct pn533
*dev
, struct sk_buff
*skb
, int status
)
2175 dev
->cmd
->status
= status
;
2178 dev_dbg(dev
->dev
, "%s: Error received: %d\n", __func__
, status
);
2183 dev_err(dev
->dev
, "NULL Frame -> link is dead\n");
2187 if (pn533_rx_frame_is_ack(skb
->data
)) {
2188 dev_dbg(dev
->dev
, "%s: Received ACK frame\n", __func__
);
2193 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE
, 16, 1, skb
->data
,
2194 dev
->ops
->rx_frame_size(skb
->data
), false);
2196 if (!dev
->ops
->rx_is_frame_valid(skb
->data
, dev
)) {
2197 nfc_err(dev
->dev
, "Received an invalid frame\n");
2198 dev
->cmd
->status
= -EIO
;
2199 } else if (!pn533_rx_frame_is_cmd_response(dev
, skb
->data
)) {
2200 nfc_err(dev
->dev
, "It it not the response to the last command\n");
2201 dev
->cmd
->status
= -EIO
;
2204 dev
->cmd
->resp
= skb
;
2207 queue_work(dev
->wq
, &dev
->cmd_complete_work
);
2209 EXPORT_SYMBOL(pn533_recv_frame
);
2211 /* Split the Tx skb into small chunks */
2212 static int pn533_fill_fragment_skbs(struct pn533
*dev
, struct sk_buff
*skb
)
2214 struct sk_buff
*frag
;
2218 /* Remaining size */
2219 if (skb
->len
> PN533_CMD_DATAFRAME_MAXLEN
)
2220 frag_size
= PN533_CMD_DATAFRAME_MAXLEN
;
2222 frag_size
= skb
->len
;
2224 /* Allocate and reserve */
2225 frag
= pn533_alloc_skb(dev
, frag_size
);
2227 skb_queue_purge(&dev
->fragment_skb
);
2231 if (!dev
->tgt_mode
) {
2232 /* Reserve the TG/MI byte */
2233 skb_reserve(frag
, 1);
2236 if (frag_size
== PN533_CMD_DATAFRAME_MAXLEN
)
2237 *(u8
*)skb_push(frag
, sizeof(u8
)) =
2238 (PN533_CMD_MI_MASK
| 1);
2240 *(u8
*)skb_push(frag
, sizeof(u8
)) = 1; /* TG */
2243 skb_put_data(frag
, skb
->data
, frag_size
);
2245 /* Reduce the size of incoming buffer */
2246 skb_pull(skb
, frag_size
);
2248 /* Add this to skb_queue */
2249 skb_queue_tail(&dev
->fragment_skb
, frag
);
2251 } while (skb
->len
> 0);
2255 return skb_queue_len(&dev
->fragment_skb
);
2258 static int pn533_transceive(struct nfc_dev
*nfc_dev
,
2259 struct nfc_target
*target
, struct sk_buff
*skb
,
2260 data_exchange_cb_t cb
, void *cb_context
)
2262 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2263 struct pn533_data_exchange_arg
*arg
= NULL
;
2266 if (!dev
->tgt_active_prot
) {
2268 "Can't exchange data if there is no active target\n");
2273 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
2280 arg
->cb_context
= cb_context
;
2282 switch (dev
->device_type
) {
2283 case PN533_DEVICE_PASORI
:
2284 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2285 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_COMM_THRU
,
2287 pn533_data_exchange_complete
,
2295 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2296 rc
= pn533_fill_fragment_skbs(dev
, skb
);
2300 skb
= skb_dequeue(&dev
->fragment_skb
);
2306 *(u8
*)skb_push(skb
, sizeof(u8
)) = 1; /* TG */
2309 rc
= pn533_send_data_async(dev
, PN533_CMD_IN_DATA_EXCHANGE
,
2310 skb
, pn533_data_exchange_complete
,
2316 if (rc
< 0) /* rc from send_async */
2327 static int pn533_tm_send_complete(struct pn533
*dev
, void *arg
,
2328 struct sk_buff
*resp
)
2333 return PTR_ERR(resp
);
2335 status
= resp
->data
[0];
2337 /* Prepare for the next round */
2338 if (skb_queue_len(&dev
->fragment_skb
) > 0) {
2339 queue_work(dev
->wq
, &dev
->mi_tm_tx_work
);
2340 return -EINPROGRESS
;
2342 dev_kfree_skb(resp
);
2345 nfc_tm_deactivated(dev
->nfc_dev
);
2352 queue_work(dev
->wq
, &dev
->tg_work
);
2357 static int pn533_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
2359 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2362 /* let's split in multiple chunks if size's too big */
2363 if (skb
->len
> PN533_CMD_DATAEXCH_DATA_MAXLEN
) {
2364 rc
= pn533_fill_fragment_skbs(dev
, skb
);
2368 /* get the first skb */
2369 skb
= skb_dequeue(&dev
->fragment_skb
);
2375 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_META_DATA
, skb
,
2376 pn533_tm_send_complete
, NULL
);
2379 rc
= pn533_send_data_async(dev
, PN533_CMD_TG_SET_DATA
, skb
,
2380 pn533_tm_send_complete
, NULL
);
2386 skb_queue_purge(&dev
->fragment_skb
);
2392 static void pn533_wq_mi_recv(struct work_struct
*work
)
2394 struct pn533
*dev
= container_of(work
, struct pn533
, mi_rx_work
);
2395 struct sk_buff
*skb
;
2398 skb
= pn533_alloc_skb(dev
, PN533_CMD_DATAEXCH_HEAD_LEN
);
2402 switch (dev
->device_type
) {
2403 case PN533_DEVICE_PASORI
:
2404 if (dev
->tgt_active_prot
== NFC_PROTO_FELICA
) {
2405 rc
= pn533_send_cmd_direct_async(dev
,
2406 PN533_CMD_IN_COMM_THRU
,
2408 pn533_data_exchange_complete
,
2409 dev
->cmd_complete_mi_arg
);
2415 skb_put_u8(skb
, 1); /*TG*/
2417 rc
= pn533_send_cmd_direct_async(dev
,
2418 PN533_CMD_IN_DATA_EXCHANGE
,
2420 pn533_data_exchange_complete
,
2421 dev
->cmd_complete_mi_arg
);
2426 if (rc
== 0) /* success */
2430 "Error %d when trying to perform data_exchange\n", rc
);
2433 kfree(dev
->cmd_complete_mi_arg
);
2436 dev
->phy_ops
->send_ack(dev
, GFP_KERNEL
);
2437 queue_work(dev
->wq
, &dev
->cmd_work
);
2440 static void pn533_wq_mi_send(struct work_struct
*work
)
2442 struct pn533
*dev
= container_of(work
, struct pn533
, mi_tx_work
);
2443 struct sk_buff
*skb
;
2446 /* Grab the first skb in the queue */
2447 skb
= skb_dequeue(&dev
->fragment_skb
);
2449 if (skb
== NULL
) { /* No more data */
2450 /* Reset the queue for future use */
2451 skb_queue_head_init(&dev
->fragment_skb
);
2455 switch (dev
->device_type
) {
2456 case PN533_DEVICE_PASORI
:
2457 if (dev
->tgt_active_prot
!= NFC_PROTO_FELICA
) {
2462 rc
= pn533_send_cmd_direct_async(dev
, PN533_CMD_IN_COMM_THRU
,
2464 pn533_data_exchange_complete
,
2465 dev
->cmd_complete_dep_arg
);
2470 /* Still some fragments? */
2471 rc
= pn533_send_cmd_direct_async(dev
,
2472 PN533_CMD_IN_DATA_EXCHANGE
,
2474 pn533_data_exchange_complete
,
2475 dev
->cmd_complete_dep_arg
);
2480 if (rc
== 0) /* success */
2484 "Error %d when trying to perform data_exchange\n", rc
);
2487 kfree(dev
->cmd_complete_dep_arg
);
2490 dev
->phy_ops
->send_ack(dev
, GFP_KERNEL
);
2491 queue_work(dev
->wq
, &dev
->cmd_work
);
2494 static int pn533_set_configuration(struct pn533
*dev
, u8 cfgitem
, u8
*cfgdata
,
2497 struct sk_buff
*skb
;
2498 struct sk_buff
*resp
;
2501 skb_len
= sizeof(cfgitem
) + cfgdata_len
; /* cfgitem + cfgdata */
2503 skb
= pn533_alloc_skb(dev
, skb_len
);
2507 skb_put_u8(skb
, cfgitem
);
2508 skb_put_data(skb
, cfgdata
, cfgdata_len
);
2510 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_RF_CONFIGURATION
, skb
);
2512 return PTR_ERR(resp
);
2514 dev_kfree_skb(resp
);
2518 static int pn533_get_firmware_version(struct pn533
*dev
,
2519 struct pn533_fw_version
*fv
)
2521 struct sk_buff
*skb
;
2522 struct sk_buff
*resp
;
2524 skb
= pn533_alloc_skb(dev
, 0);
2528 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_GET_FIRMWARE_VERSION
, skb
);
2530 return PTR_ERR(resp
);
2532 fv
->ic
= resp
->data
[0];
2533 fv
->ver
= resp
->data
[1];
2534 fv
->rev
= resp
->data
[2];
2535 fv
->support
= resp
->data
[3];
2537 dev_kfree_skb(resp
);
2541 static int pn533_pasori_fw_reset(struct pn533
*dev
)
2543 struct sk_buff
*skb
;
2544 struct sk_buff
*resp
;
2546 skb
= pn533_alloc_skb(dev
, sizeof(u8
));
2550 skb_put_u8(skb
, 0x1);
2552 resp
= pn533_send_cmd_sync(dev
, 0x18, skb
);
2554 return PTR_ERR(resp
);
2556 dev_kfree_skb(resp
);
2561 static int pn533_rf_field(struct nfc_dev
*nfc_dev
, u8 rf
)
2563 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2567 rf_field
|= PN533_CFGITEM_RF_FIELD_AUTO_RFCA
;
2569 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_RF_FIELD
,
2570 (u8
*)&rf_field
, 1);
2572 nfc_err(dev
->dev
, "Error on setting RF field\n");
2579 static int pn532_sam_configuration(struct nfc_dev
*nfc_dev
)
2581 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2582 struct sk_buff
*skb
;
2583 struct sk_buff
*resp
;
2585 skb
= pn533_alloc_skb(dev
, 1);
2589 skb_put_u8(skb
, 0x01);
2591 resp
= pn533_send_cmd_sync(dev
, PN533_CMD_SAM_CONFIGURATION
, skb
);
2593 return PTR_ERR(resp
);
2595 dev_kfree_skb(resp
);
2599 static int pn533_dev_up(struct nfc_dev
*nfc_dev
)
2601 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2604 if (dev
->phy_ops
->dev_up
) {
2605 rc
= dev
->phy_ops
->dev_up(dev
);
2610 if ((dev
->device_type
== PN533_DEVICE_PN532
) ||
2611 (dev
->device_type
== PN533_DEVICE_PN532_AUTOPOLL
)) {
2612 rc
= pn532_sam_configuration(nfc_dev
);
2618 return pn533_rf_field(nfc_dev
, 1);
2621 static int pn533_dev_down(struct nfc_dev
*nfc_dev
)
2623 struct pn533
*dev
= nfc_get_drvdata(nfc_dev
);
2626 ret
= pn533_rf_field(nfc_dev
, 0);
2627 if (dev
->phy_ops
->dev_down
&& !ret
)
2628 ret
= dev
->phy_ops
->dev_down(dev
);
2633 static const struct nfc_ops pn533_nfc_ops
= {
2634 .dev_up
= pn533_dev_up
,
2635 .dev_down
= pn533_dev_down
,
2636 .dep_link_up
= pn533_dep_link_up
,
2637 .dep_link_down
= pn533_dep_link_down
,
2638 .start_poll
= pn533_start_poll
,
2639 .stop_poll
= pn533_stop_poll
,
2640 .activate_target
= pn533_activate_target
,
2641 .deactivate_target
= pn533_deactivate_target
,
2642 .im_transceive
= pn533_transceive
,
2643 .tm_send
= pn533_tm_send
,
2646 static int pn533_setup(struct pn533
*dev
)
2648 struct pn533_config_max_retries max_retries
;
2649 struct pn533_config_timing timing
;
2650 u8 pasori_cfg
[3] = {0x08, 0x01, 0x08};
2653 switch (dev
->device_type
) {
2654 case PN533_DEVICE_STD
:
2655 case PN533_DEVICE_PASORI
:
2656 case PN533_DEVICE_ACR122U
:
2657 case PN533_DEVICE_PN532
:
2658 case PN533_DEVICE_PN532_AUTOPOLL
:
2659 max_retries
.mx_rty_atr
= 0x2;
2660 max_retries
.mx_rty_psl
= 0x1;
2661 max_retries
.mx_rty_passive_act
=
2662 PN533_CONFIG_MAX_RETRIES_NO_RETRY
;
2664 timing
.rfu
= PN533_CONFIG_TIMING_102
;
2665 timing
.atr_res_timeout
= PN533_CONFIG_TIMING_102
;
2666 timing
.dep_timeout
= PN533_CONFIG_TIMING_204
;
2671 nfc_err(dev
->dev
, "Unknown device type %d\n",
2676 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_MAX_RETRIES
,
2677 (u8
*)&max_retries
, sizeof(max_retries
));
2680 "Error on setting MAX_RETRIES config\n");
2685 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_TIMING
,
2686 (u8
*)&timing
, sizeof(timing
));
2688 nfc_err(dev
->dev
, "Error on setting RF timings\n");
2692 switch (dev
->device_type
) {
2693 case PN533_DEVICE_STD
:
2694 case PN533_DEVICE_PN532
:
2695 case PN533_DEVICE_PN532_AUTOPOLL
:
2698 case PN533_DEVICE_PASORI
:
2699 pn533_pasori_fw_reset(dev
);
2701 rc
= pn533_set_configuration(dev
, PN533_CFGITEM_PASORI
,
2705 "Error while settings PASORI config\n");
2709 pn533_pasori_fw_reset(dev
);
2717 int pn533_finalize_setup(struct pn533
*dev
)
2720 struct pn533_fw_version fw_ver
;
2723 memset(&fw_ver
, 0, sizeof(fw_ver
));
2725 rc
= pn533_get_firmware_version(dev
, &fw_ver
);
2727 nfc_err(dev
->dev
, "Unable to get FW version\n");
2731 nfc_info(dev
->dev
, "NXP PN5%02X firmware ver %d.%d now attached\n",
2732 fw_ver
.ic
, fw_ver
.ver
, fw_ver
.rev
);
2734 rc
= pn533_setup(dev
);
2740 EXPORT_SYMBOL_GPL(pn533_finalize_setup
);
2742 struct pn533
*pn53x_common_init(u32 device_type
,
2743 enum pn533_protocol_type protocol_type
,
2745 const struct pn533_phy_ops
*phy_ops
,
2746 struct pn533_frame_ops
*fops
,
2751 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
2753 return ERR_PTR(-ENOMEM
);
2756 priv
->phy_ops
= phy_ops
;
2761 priv
->ops
= &pn533_std_frame_ops
;
2763 priv
->protocol_type
= protocol_type
;
2764 priv
->device_type
= device_type
;
2766 mutex_init(&priv
->cmd_lock
);
2768 INIT_WORK(&priv
->cmd_work
, pn533_wq_cmd
);
2769 INIT_WORK(&priv
->cmd_complete_work
, pn533_wq_cmd_complete
);
2770 INIT_WORK(&priv
->mi_rx_work
, pn533_wq_mi_recv
);
2771 INIT_WORK(&priv
->mi_tx_work
, pn533_wq_mi_send
);
2772 INIT_WORK(&priv
->tg_work
, pn533_wq_tg_get_data
);
2773 INIT_WORK(&priv
->mi_tm_rx_work
, pn533_wq_tm_mi_recv
);
2774 INIT_WORK(&priv
->mi_tm_tx_work
, pn533_wq_tm_mi_send
);
2775 INIT_DELAYED_WORK(&priv
->poll_work
, pn533_wq_poll
);
2776 INIT_WORK(&priv
->rf_work
, pn533_wq_rf
);
2777 priv
->wq
= alloc_ordered_workqueue("pn533", 0);
2778 if (priv
->wq
== NULL
)
2781 timer_setup(&priv
->listen_timer
, pn533_listen_mode_timer
, 0);
2783 skb_queue_head_init(&priv
->resp_q
);
2784 skb_queue_head_init(&priv
->fragment_skb
);
2786 INIT_LIST_HEAD(&priv
->cmd_queue
);
2791 return ERR_PTR(-ENOMEM
);
2793 EXPORT_SYMBOL_GPL(pn53x_common_init
);
2795 void pn53x_common_clean(struct pn533
*priv
)
2797 struct pn533_cmd
*cmd
, *n
;
2799 /* delete the timer before cleanup the worker */
2800 timer_shutdown_sync(&priv
->listen_timer
);
2802 flush_delayed_work(&priv
->poll_work
);
2803 destroy_workqueue(priv
->wq
);
2805 skb_queue_purge(&priv
->resp_q
);
2807 list_for_each_entry_safe(cmd
, n
, &priv
->cmd_queue
, queue
) {
2808 list_del(&cmd
->queue
);
2814 EXPORT_SYMBOL_GPL(pn53x_common_clean
);
2816 int pn532_i2c_nfc_alloc(struct pn533
*priv
, u32 protocols
,
2817 struct device
*parent
)
2819 priv
->nfc_dev
= nfc_allocate_device(&pn533_nfc_ops
, protocols
,
2820 priv
->ops
->tx_header_len
+
2821 PN533_CMD_DATAEXCH_HEAD_LEN
,
2822 priv
->ops
->tx_tail_len
);
2826 nfc_set_parent_dev(priv
->nfc_dev
, parent
);
2827 nfc_set_drvdata(priv
->nfc_dev
, priv
);
2830 EXPORT_SYMBOL_GPL(pn532_i2c_nfc_alloc
);
2832 int pn53x_register_nfc(struct pn533
*priv
, u32 protocols
,
2833 struct device
*parent
)
2837 rc
= pn532_i2c_nfc_alloc(priv
, protocols
, parent
);
2841 rc
= nfc_register_device(priv
->nfc_dev
);
2843 nfc_free_device(priv
->nfc_dev
);
2847 EXPORT_SYMBOL_GPL(pn53x_register_nfc
);
2849 void pn53x_unregister_nfc(struct pn533
*priv
)
2851 nfc_unregister_device(priv
->nfc_dev
);
2852 nfc_free_device(priv
->nfc_dev
);
2854 EXPORT_SYMBOL_GPL(pn53x_unregister_nfc
);
2856 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2857 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2858 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2859 MODULE_DESCRIPTION("PN533 driver ver " VERSION
);
2860 MODULE_VERSION(VERSION
);
2861 MODULE_LICENSE("GPL");