2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
20 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
21 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
23 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
25 #define DIGITAL_CMD_ATR_REQ 0x00
26 #define DIGITAL_CMD_ATR_RES 0x01
27 #define DIGITAL_CMD_PSL_REQ 0x04
28 #define DIGITAL_CMD_PSL_RES 0x05
29 #define DIGITAL_CMD_DEP_REQ 0x06
30 #define DIGITAL_CMD_DEP_RES 0x07
32 #define DIGITAL_ATR_REQ_MIN_SIZE 16
33 #define DIGITAL_ATR_REQ_MAX_SIZE 64
35 #define DIGITAL_DID_MAX 14
37 #define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
38 #define DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B \
39 (DIGITAL_LR_BITS_PAYLOAD_SIZE_254B >> 4)
40 #define DIGITAL_GB_BIT 0x02
42 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
44 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
45 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
47 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
48 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
49 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
50 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
51 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
52 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
54 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
55 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
56 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
58 struct digital_atr_req
{
69 struct digital_atr_res
{
81 struct digital_psl_req
{
89 struct digital_psl_res
{
95 struct digital_dep_req_res
{
101 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
102 struct sk_buff
*resp
);
104 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
107 skb_push(skb
, sizeof(u8
));
109 skb
->data
[0] = skb
->len
;
111 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
112 *skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
115 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
123 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
124 skb_pull(skb
, sizeof(u8
));
127 if (size
!= skb
->len
)
130 skb_pull(skb
, sizeof(u8
));
135 static void digital_in_recv_psl_res(struct nfc_digital_dev
*ddev
, void *arg
,
136 struct sk_buff
*resp
)
138 struct nfc_target
*target
= arg
;
139 struct digital_psl_res
*psl_res
;
148 rc
= ddev
->skb_check_crc(resp
);
150 PROTOCOL_ERR("14.4.1.6");
154 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
156 PROTOCOL_ERR("14.4.1.2");
160 psl_res
= (struct digital_psl_res
*)resp
->data
;
162 if ((resp
->len
!= sizeof(*psl_res
)) ||
163 (psl_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
) ||
164 (psl_res
->cmd
!= DIGITAL_CMD_PSL_RES
)) {
169 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
170 NFC_DIGITAL_RF_TECH_424F
);
174 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
175 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
);
179 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
) &&
180 (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)) {
181 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
182 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
185 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
187 nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
190 ddev
->curr_nfc_dep_pni
= 0;
196 ddev
->curr_protocol
= 0;
199 static int digital_in_send_psl_req(struct nfc_digital_dev
*ddev
,
200 struct nfc_target
*target
)
203 struct digital_psl_req
*psl_req
;
206 skb
= digital_skb_alloc(ddev
, sizeof(*psl_req
));
210 skb_put(skb
, sizeof(*psl_req
));
212 psl_req
= (struct digital_psl_req
*)skb
->data
;
214 psl_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
215 psl_req
->cmd
= DIGITAL_CMD_PSL_REQ
;
217 psl_req
->brs
= (0x2 << 3) | 0x2; /* 424F both directions */
218 psl_req
->fsl
= DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B
;
220 digital_skb_push_dep_sod(ddev
, skb
);
222 ddev
->skb_add_crc(skb
);
224 rc
= digital_in_send_cmd(ddev
, skb
, 500, digital_in_recv_psl_res
,
232 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
233 struct sk_buff
*resp
)
235 struct nfc_target
*target
= arg
;
236 struct digital_atr_res
*atr_res
;
246 rc
= ddev
->skb_check_crc(resp
);
248 PROTOCOL_ERR("14.4.1.6");
252 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
254 PROTOCOL_ERR("14.4.1.2");
258 if (resp
->len
< sizeof(struct digital_atr_res
)) {
263 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
265 atr_res
= (struct digital_atr_res
*)resp
->data
;
267 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
271 if ((ddev
->protocols
& NFC_PROTO_FELICA_MASK
) &&
272 (ddev
->curr_rf_tech
!= NFC_DIGITAL_RF_TECH_424F
)) {
273 rc
= digital_in_send_psl_req(ddev
, target
);
278 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
281 ddev
->curr_nfc_dep_pni
= 0;
287 ddev
->curr_protocol
= 0;
290 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
291 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
295 struct digital_atr_req
*atr_req
;
299 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
301 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
302 PROTOCOL_ERR("14.6.1.1");
306 skb
= digital_skb_alloc(ddev
, size
);
310 skb_put(skb
, sizeof(struct digital_atr_req
));
312 atr_req
= (struct digital_atr_req
*)skb
->data
;
313 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
315 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
316 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
317 if (target
->nfcid2_len
)
318 memcpy(atr_req
->nfcid3
, target
->nfcid2
, NFC_NFCID2_MAXSIZE
);
320 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
326 atr_req
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
329 atr_req
->pp
|= DIGITAL_GB_BIT
;
330 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
333 digital_skb_push_dep_sod(ddev
, skb
);
335 ddev
->skb_add_crc(skb
);
337 rc
= digital_in_send_cmd(ddev
, skb
, 500, digital_in_recv_atr_res
,
345 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
346 struct digital_data_exch
*data_exch
, u8 rtox
)
348 struct digital_dep_req_res
*dep_req
;
352 skb
= digital_skb_alloc(ddev
, 1);
356 *skb_put(skb
, 1) = rtox
;
358 skb_push(skb
, sizeof(struct digital_dep_req_res
));
360 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
362 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
363 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
364 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
365 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
367 digital_skb_push_dep_sod(ddev
, skb
);
369 ddev
->skb_add_crc(skb
);
371 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
379 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
380 struct sk_buff
*resp
)
382 struct digital_data_exch
*data_exch
= arg
;
383 struct digital_dep_req_res
*dep_res
;
394 rc
= ddev
->skb_check_crc(resp
);
396 PROTOCOL_ERR("14.4.1.6");
400 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
402 PROTOCOL_ERR("14.4.1.2");
406 size
= sizeof(struct digital_dep_req_res
);
407 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
409 if (resp
->len
< size
|| dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
410 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
417 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
418 PROTOCOL_ERR("14.8.2.1");
423 if (size
> resp
->len
) {
428 skb_pull(resp
, size
);
430 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
431 case DIGITAL_NFC_DEP_PFB_I_PDU
:
432 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
433 PROTOCOL_ERR("14.12.3.3");
438 ddev
->curr_nfc_dep_pni
=
439 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
443 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
444 pr_err("Received a ACK/NACK PDU\n");
448 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
449 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
454 rc
= digital_in_send_rtox(ddev
, data_exch
, resp
->data
[0]);
462 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
463 pr_err("MI bit set. Chained PDU not supported\n");
469 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
478 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
479 struct nfc_target
*target
, struct sk_buff
*skb
,
480 struct digital_data_exch
*data_exch
)
482 struct digital_dep_req_res
*dep_req
;
484 skb_push(skb
, sizeof(struct digital_dep_req_res
));
486 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
487 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
488 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
489 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
491 digital_skb_push_dep_sod(ddev
, skb
);
493 ddev
->skb_add_crc(skb
);
495 return digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
499 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
501 ddev
->curr_rf_tech
= rf_tech
;
503 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
504 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
506 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
509 switch (ddev
->curr_rf_tech
) {
510 case NFC_DIGITAL_RF_TECH_106A
:
511 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
512 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
515 case NFC_DIGITAL_RF_TECH_212F
:
516 case NFC_DIGITAL_RF_TECH_424F
:
517 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
518 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
526 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
527 struct sk_buff
*resp
)
530 struct digital_dep_req_res
*dep_req
;
540 rc
= ddev
->skb_check_crc(resp
);
542 PROTOCOL_ERR("14.4.1.6");
546 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
548 PROTOCOL_ERR("14.4.1.2");
552 size
= sizeof(struct digital_dep_req_res
);
553 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
555 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
556 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
563 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
564 if (ddev
->did
&& (ddev
->did
== resp
->data
[3])) {
570 } else if (ddev
->did
) {
575 if (size
> resp
->len
) {
580 skb_pull(resp
, size
);
582 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
583 case DIGITAL_NFC_DEP_PFB_I_PDU
:
584 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
585 ddev
->curr_nfc_dep_pni
= DIGITAL_NFC_DEP_PFB_PNI(pfb
);
587 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
588 pr_err("Received a ACK/NACK PDU\n");
591 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
592 pr_err("Received a SUPERVISOR PDU\n");
597 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
604 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
606 struct digital_dep_req_res
*dep_res
;
608 skb_push(skb
, sizeof(struct digital_dep_req_res
));
609 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
611 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
612 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
613 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
616 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
618 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
622 digital_skb_push_dep_sod(ddev
, skb
);
624 ddev
->skb_add_crc(skb
);
626 return digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
630 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
631 void *arg
, struct sk_buff
*resp
)
633 u8 rf_tech
= (unsigned long)arg
;
638 digital_tg_set_rf_tech(ddev
, rf_tech
);
640 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
642 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
647 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
650 struct digital_psl_res
*psl_res
;
654 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
658 skb_put(skb
, sizeof(struct digital_psl_res
));
660 psl_res
= (struct digital_psl_res
*)skb
->data
;
662 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
663 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
666 digital_skb_push_dep_sod(ddev
, skb
);
668 ddev
->skb_add_crc(skb
);
670 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
671 (void *)(unsigned long)rf_tech
);
678 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
679 struct sk_buff
*resp
)
682 struct digital_psl_req
*psl_req
;
692 rc
= ddev
->skb_check_crc(resp
);
694 PROTOCOL_ERR("14.4.1.6");
698 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
700 PROTOCOL_ERR("14.4.1.2");
704 psl_req
= (struct digital_psl_req
*)resp
->data
;
706 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
707 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
708 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
713 dsi
= (psl_req
->brs
>> 3) & 0x07;
716 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
719 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
722 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
725 pr_err("Unsupported dsi value %d\n", dsi
);
729 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
735 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
736 void *arg
, struct sk_buff
*resp
)
741 digital_poll_next_tech(ddev
);
746 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
749 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
750 digital_tg_recv_psl_req(ddev
, arg
, resp
);
752 digital_tg_recv_dep_req(ddev
, arg
, resp
);
755 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
756 struct digital_atr_req
*atr_req
)
758 struct digital_atr_res
*atr_res
;
764 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
768 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
772 skb_put(skb
, sizeof(struct digital_atr_res
));
773 atr_res
= (struct digital_atr_res
*)skb
->data
;
775 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
777 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
778 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
779 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
781 atr_res
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
783 skb_put(skb
, gb_len
);
785 atr_res
->pp
|= DIGITAL_GB_BIT
;
786 memcpy(atr_res
->gb
, gb
, gb_len
);
789 digital_skb_push_dep_sod(ddev
, skb
);
791 ddev
->skb_add_crc(skb
);
793 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
794 digital_tg_send_atr_res_complete
, NULL
);
801 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
802 struct sk_buff
*resp
)
805 struct digital_atr_req
*atr_req
;
806 size_t gb_len
, min_size
;
820 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
821 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
822 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
824 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
825 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
828 if (resp
->len
< min_size
) {
833 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
835 rc
= ddev
->skb_check_crc(resp
);
837 PROTOCOL_ERR("14.4.1.6");
841 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
843 PROTOCOL_ERR("14.4.1.2");
847 atr_req
= (struct digital_atr_req
*)resp
->data
;
849 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
850 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
||
851 atr_req
->did
> DIGITAL_DID_MAX
) {
856 ddev
->did
= atr_req
->did
;
858 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
859 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
863 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
867 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
869 poll_tech_count
= ddev
->poll_tech_count
;
870 ddev
->poll_tech_count
= 0;
872 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
873 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
875 ddev
->poll_tech_count
= poll_tech_count
;
882 digital_poll_next_tech(ddev
);