1 // SPDX-License-Identifier: GPL-2.0-only
3 * NFC Digital Protocol stack
4 * Copyright (c) 2013, Intel Corporation.
7 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
11 #define DIGITAL_NFC_DEP_N_RETRY_NACK 2
12 #define DIGITAL_NFC_DEP_N_RETRY_ATN 2
14 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
15 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
17 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
19 #define DIGITAL_CMD_ATR_REQ 0x00
20 #define DIGITAL_CMD_ATR_RES 0x01
21 #define DIGITAL_CMD_PSL_REQ 0x04
22 #define DIGITAL_CMD_PSL_RES 0x05
23 #define DIGITAL_CMD_DEP_REQ 0x06
24 #define DIGITAL_CMD_DEP_RES 0x07
26 #define DIGITAL_ATR_REQ_MIN_SIZE 16
27 #define DIGITAL_ATR_REQ_MAX_SIZE 64
29 #define DIGITAL_ATR_RES_TO_WT(s) ((s) & 0xF)
31 #define DIGITAL_DID_MAX 14
33 #define DIGITAL_PAYLOAD_SIZE_MAX 254
34 #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
35 #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
36 #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
37 #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
39 #define DIGITAL_GB_BIT 0x02
41 #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
42 #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
44 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
46 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
47 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
48 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
49 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
51 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
52 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
53 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
54 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
55 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
56 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
57 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
59 #define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
60 #define DIGITAL_NFC_DEP_RTOX_MAX 59
62 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
63 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
64 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
66 struct digital_atr_req
{
77 struct digital_atr_res
{
89 struct digital_psl_req
{
97 struct digital_psl_res
{
103 struct digital_dep_req_res
{
109 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
110 struct sk_buff
*resp
);
111 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
112 struct sk_buff
*resp
);
114 static const u8 digital_payload_bits_map
[4] = {
121 /* Response Waiting Time for ATR_RES PDU in ms
123 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
126 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
127 * dRWT(nfcdep) = 16 / f(c) s
128 * dT(nfcdep,initiator) = 100 ms
131 #define DIGITAL_ATR_RES_RWT 1337
133 /* Response Waiting Time for other DEP PDUs in ms
135 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
138 * rwt = (256 * 16 / f(c)) * 2^wt s
139 * dRWT(nfcdep) = 16 / f(c) s
140 * dT(nfcdep,initiator) = 100 ms
142 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
144 #define DIGITAL_NFC_DEP_IN_MAX_WT 14
145 #define DIGITAL_NFC_DEP_TG_MAX_WT 14
146 static const u16 digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
+ 1] = {
147 100, 101, 101, 102, 105,
148 110, 119, 139, 177, 255,
149 409, 719, 1337, 2575, 5049,
152 static u8
digital_payload_bits_to_size(u8 payload_bits
)
154 if (payload_bits
>= ARRAY_SIZE(digital_payload_bits_map
))
157 return digital_payload_bits_map
[payload_bits
];
160 static u8
digital_payload_size_to_bits(u8 payload_size
)
164 for (i
= 0; i
< ARRAY_SIZE(digital_payload_bits_map
); i
++)
165 if (digital_payload_bits_map
[i
] == payload_size
)
171 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
174 skb_push(skb
, sizeof(u8
));
176 skb
->data
[0] = skb
->len
;
178 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
179 *(u8
*)skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
182 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
190 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
191 skb_pull(skb
, sizeof(u8
));
194 if (size
!= skb
->len
)
197 skb_pull(skb
, sizeof(u8
));
202 static struct sk_buff
*
203 digital_send_dep_data_prep(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
,
204 struct digital_dep_req_res
*dep_req_res
,
205 struct digital_data_exch
*data_exch
)
207 struct sk_buff
*new_skb
;
209 if (skb
->len
> ddev
->remote_payload_max
) {
210 dep_req_res
->pfb
|= DIGITAL_NFC_DEP_PFB_MI_BIT
;
212 new_skb
= digital_skb_alloc(ddev
, ddev
->remote_payload_max
);
214 kfree_skb(ddev
->chaining_skb
);
215 ddev
->chaining_skb
= NULL
;
217 return ERR_PTR(-ENOMEM
);
220 skb_put_data(new_skb
, skb
->data
, ddev
->remote_payload_max
);
221 skb_pull(skb
, ddev
->remote_payload_max
);
223 ddev
->chaining_skb
= skb
;
224 ddev
->data_exch
= data_exch
;
226 ddev
->chaining_skb
= NULL
;
233 static struct sk_buff
*
234 digital_recv_dep_data_gather(struct nfc_digital_dev
*ddev
, u8 pfb
,
235 struct sk_buff
*resp
,
236 int (*send_ack
)(struct nfc_digital_dev
*ddev
,
237 struct digital_data_exch
239 struct digital_data_exch
*data_exch
)
241 struct sk_buff
*new_skb
;
244 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
) && (!ddev
->chaining_skb
)) {
246 nfc_alloc_recv_skb(8 * ddev
->local_payload_max
,
248 if (!ddev
->chaining_skb
) {
254 if (ddev
->chaining_skb
) {
255 if (resp
->len
> skb_tailroom(ddev
->chaining_skb
)) {
256 new_skb
= skb_copy_expand(ddev
->chaining_skb
,
259 8 * ddev
->local_payload_max
,
266 kfree_skb(ddev
->chaining_skb
);
267 ddev
->chaining_skb
= new_skb
;
270 skb_put_data(ddev
->chaining_skb
, resp
->data
, resp
->len
);
275 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
276 rc
= send_ack(ddev
, data_exch
);
283 resp
= ddev
->chaining_skb
;
284 ddev
->chaining_skb
= NULL
;
292 kfree_skb(ddev
->chaining_skb
);
293 ddev
->chaining_skb
= NULL
;
298 static void digital_in_recv_psl_res(struct nfc_digital_dev
*ddev
, void *arg
,
299 struct sk_buff
*resp
)
301 struct nfc_target
*target
= arg
;
302 struct digital_psl_res
*psl_res
;
311 rc
= ddev
->skb_check_crc(resp
);
313 PROTOCOL_ERR("14.4.1.6");
317 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
319 PROTOCOL_ERR("14.4.1.2");
323 psl_res
= (struct digital_psl_res
*)resp
->data
;
325 if ((resp
->len
!= sizeof(*psl_res
)) ||
326 (psl_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
) ||
327 (psl_res
->cmd
!= DIGITAL_CMD_PSL_RES
)) {
332 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
333 NFC_DIGITAL_RF_TECH_424F
);
337 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
338 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
);
342 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
) &&
343 (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)) {
344 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
345 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
348 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
350 nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
353 ddev
->curr_nfc_dep_pni
= 0;
359 ddev
->curr_protocol
= 0;
362 static int digital_in_send_psl_req(struct nfc_digital_dev
*ddev
,
363 struct nfc_target
*target
)
366 struct digital_psl_req
*psl_req
;
368 u8 payload_size
, payload_bits
;
370 skb
= digital_skb_alloc(ddev
, sizeof(*psl_req
));
374 skb_put(skb
, sizeof(*psl_req
));
376 psl_req
= (struct digital_psl_req
*)skb
->data
;
378 psl_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
379 psl_req
->cmd
= DIGITAL_CMD_PSL_REQ
;
381 psl_req
->brs
= (0x2 << 3) | 0x2; /* 424F both directions */
383 payload_size
= min(ddev
->local_payload_max
, ddev
->remote_payload_max
);
384 payload_bits
= digital_payload_size_to_bits(payload_size
);
385 psl_req
->fsl
= DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits
);
387 ddev
->local_payload_max
= payload_size
;
388 ddev
->remote_payload_max
= payload_size
;
390 digital_skb_push_dep_sod(ddev
, skb
);
392 ddev
->skb_add_crc(skb
);
394 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
395 digital_in_recv_psl_res
, target
);
402 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
403 struct sk_buff
*resp
)
405 struct nfc_target
*target
= arg
;
406 struct digital_atr_res
*atr_res
;
407 u8 gb_len
, payload_bits
;
417 rc
= ddev
->skb_check_crc(resp
);
419 PROTOCOL_ERR("14.4.1.6");
423 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
425 PROTOCOL_ERR("14.4.1.2");
429 if (resp
->len
< sizeof(struct digital_atr_res
)) {
434 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
436 atr_res
= (struct digital_atr_res
*)resp
->data
;
438 wt
= DIGITAL_ATR_RES_TO_WT(atr_res
->to
);
439 if (wt
> DIGITAL_NFC_DEP_IN_MAX_WT
)
440 wt
= DIGITAL_NFC_DEP_IN_MAX_WT
;
441 ddev
->dep_rwt
= digital_rwt_map
[wt
];
443 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_res
->pp
);
444 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
446 if (!ddev
->remote_payload_max
) {
451 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
455 if ((ddev
->protocols
& NFC_PROTO_FELICA_MASK
) &&
456 (ddev
->curr_rf_tech
!= NFC_DIGITAL_RF_TECH_424F
)) {
457 rc
= digital_in_send_psl_req(ddev
, target
);
462 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
465 ddev
->curr_nfc_dep_pni
= 0;
471 ddev
->curr_protocol
= 0;
474 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
475 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
479 struct digital_atr_req
*atr_req
;
484 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
486 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
487 PROTOCOL_ERR("14.6.1.1");
491 skb
= digital_skb_alloc(ddev
, size
);
495 skb_put(skb
, sizeof(struct digital_atr_req
));
497 atr_req
= (struct digital_atr_req
*)skb
->data
;
498 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
500 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
501 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
502 if (target
->nfcid2_len
)
503 memcpy(atr_req
->nfcid3
, target
->nfcid2
, NFC_NFCID2_MAXSIZE
);
505 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
511 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
512 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
513 atr_req
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
516 atr_req
->pp
|= DIGITAL_GB_BIT
;
517 skb_put_data(skb
, gb
, gb_len
);
520 digital_skb_push_dep_sod(ddev
, skb
);
522 ddev
->skb_add_crc(skb
);
524 rc
= digital_in_send_cmd(ddev
, skb
, DIGITAL_ATR_RES_RWT
,
525 digital_in_recv_atr_res
, target
);
532 static int digital_in_send_ack(struct nfc_digital_dev
*ddev
,
533 struct digital_data_exch
*data_exch
)
535 struct digital_dep_req_res
*dep_req
;
539 skb
= digital_skb_alloc(ddev
, 1);
543 skb_push(skb
, sizeof(struct digital_dep_req_res
));
545 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
547 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
548 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
549 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
550 ddev
->curr_nfc_dep_pni
;
552 digital_skb_push_dep_sod(ddev
, skb
);
554 ddev
->skb_add_crc(skb
);
556 ddev
->saved_skb
= pskb_copy(skb
, GFP_KERNEL
);
558 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
559 digital_in_recv_dep_res
, data_exch
);
562 kfree_skb(ddev
->saved_skb
);
563 ddev
->saved_skb
= NULL
;
569 static int digital_in_send_nack(struct nfc_digital_dev
*ddev
,
570 struct digital_data_exch
*data_exch
)
572 struct digital_dep_req_res
*dep_req
;
576 skb
= digital_skb_alloc(ddev
, 1);
580 skb_push(skb
, sizeof(struct digital_dep_req_res
));
582 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
584 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
585 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
586 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
587 DIGITAL_NFC_DEP_PFB_NACK_BIT
| ddev
->curr_nfc_dep_pni
;
589 digital_skb_push_dep_sod(ddev
, skb
);
591 ddev
->skb_add_crc(skb
);
593 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
594 digital_in_recv_dep_res
, data_exch
);
601 static int digital_in_send_atn(struct nfc_digital_dev
*ddev
,
602 struct digital_data_exch
*data_exch
)
604 struct digital_dep_req_res
*dep_req
;
608 skb
= digital_skb_alloc(ddev
, 1);
612 skb_push(skb
, sizeof(struct digital_dep_req_res
));
614 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
616 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
617 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
618 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
620 digital_skb_push_dep_sod(ddev
, skb
);
622 ddev
->skb_add_crc(skb
);
624 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
625 digital_in_recv_dep_res
, data_exch
);
632 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
633 struct digital_data_exch
*data_exch
, u8 rtox
)
635 struct digital_dep_req_res
*dep_req
;
640 rwt_int
= ddev
->dep_rwt
* rtox
;
641 if (rwt_int
> digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
])
642 rwt_int
= digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
];
644 skb
= digital_skb_alloc(ddev
, 1);
648 skb_put_u8(skb
, rtox
);
650 skb_push(skb
, sizeof(struct digital_dep_req_res
));
652 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
654 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
655 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
656 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
657 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
659 digital_skb_push_dep_sod(ddev
, skb
);
661 ddev
->skb_add_crc(skb
);
663 rc
= digital_in_send_cmd(ddev
, skb
, rwt_int
,
664 digital_in_recv_dep_res
, data_exch
);
671 static int digital_in_send_saved_skb(struct nfc_digital_dev
*ddev
,
672 struct digital_data_exch
*data_exch
)
676 if (!ddev
->saved_skb
)
679 skb_get(ddev
->saved_skb
);
681 rc
= digital_in_send_cmd(ddev
, ddev
->saved_skb
, ddev
->dep_rwt
,
682 digital_in_recv_dep_res
, data_exch
);
684 kfree_skb(ddev
->saved_skb
);
689 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
690 struct sk_buff
*resp
)
692 struct digital_data_exch
*data_exch
= arg
;
693 struct digital_dep_req_res
*dep_res
;
703 if ((rc
== -EIO
|| (rc
== -ETIMEDOUT
&& ddev
->nack_count
)) &&
704 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
707 rc
= digital_in_send_nack(ddev
, data_exch
);
712 } else if ((rc
== -ETIMEDOUT
) &&
713 (ddev
->atn_count
++ < DIGITAL_NFC_DEP_N_RETRY_ATN
)) {
714 ddev
->nack_count
= 0;
716 rc
= digital_in_send_atn(ddev
, data_exch
);
726 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
728 PROTOCOL_ERR("14.4.1.2");
732 rc
= ddev
->skb_check_crc(resp
);
734 if ((resp
->len
>= 4) &&
735 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
738 rc
= digital_in_send_nack(ddev
, data_exch
);
747 PROTOCOL_ERR("14.4.1.6");
752 ddev
->nack_count
= 0;
754 if (resp
->len
> ddev
->local_payload_max
) {
759 size
= sizeof(struct digital_dep_req_res
);
760 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
762 if (resp
->len
< size
|| dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
763 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
770 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
771 PROTOCOL_ERR("14.8.2.1");
776 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
781 if (size
> resp
->len
) {
786 skb_pull(resp
, size
);
788 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
789 case DIGITAL_NFC_DEP_PFB_I_PDU
:
790 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
791 PROTOCOL_ERR("14.12.3.3");
796 ddev
->curr_nfc_dep_pni
=
797 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
799 kfree_skb(ddev
->saved_skb
);
800 ddev
->saved_skb
= NULL
;
802 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
811 /* If resp is NULL then we're still chaining so return and
812 * wait for the next part of the PDU. Else, the PDU is
813 * complete so pass it up.
821 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
822 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) {
823 PROTOCOL_ERR("14.12.4.5");
828 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
829 PROTOCOL_ERR("14.12.3.3");
834 ddev
->curr_nfc_dep_pni
=
835 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
837 if (!ddev
->chaining_skb
) {
838 PROTOCOL_ERR("14.12.4.3");
843 /* The initiator has received a valid ACK. Free the last sent
844 * PDU and keep on sending chained skb.
846 kfree_skb(ddev
->saved_skb
);
847 ddev
->saved_skb
= NULL
;
849 rc
= digital_in_send_dep_req(ddev
, NULL
,
857 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
858 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) { /* ATN */
859 rc
= digital_in_send_saved_skb(ddev
, data_exch
);
866 if (ddev
->atn_count
|| ddev
->nack_count
) {
867 PROTOCOL_ERR("14.12.4.4");
872 rtox
= DIGITAL_NFC_DEP_RTOX_VALUE(resp
->data
[0]);
873 if (!rtox
|| rtox
> DIGITAL_NFC_DEP_RTOX_MAX
) {
874 PROTOCOL_ERR("14.8.4.1");
879 rc
= digital_in_send_rtox(ddev
, data_exch
, rtox
);
887 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
892 kfree_skb(ddev
->chaining_skb
);
893 ddev
->chaining_skb
= NULL
;
895 kfree_skb(ddev
->saved_skb
);
896 ddev
->saved_skb
= NULL
;
907 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
908 struct nfc_target
*target
, struct sk_buff
*skb
,
909 struct digital_data_exch
*data_exch
)
911 struct digital_dep_req_res
*dep_req
;
912 struct sk_buff
*chaining_skb
, *tmp_skb
;
915 skb_push(skb
, sizeof(struct digital_dep_req_res
));
917 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
919 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
920 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
921 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
924 ddev
->nack_count
= 0;
926 chaining_skb
= ddev
->chaining_skb
;
928 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_req
, data_exch
);
930 return PTR_ERR(tmp_skb
);
932 digital_skb_push_dep_sod(ddev
, tmp_skb
);
934 ddev
->skb_add_crc(tmp_skb
);
936 ddev
->saved_skb
= pskb_copy(tmp_skb
, GFP_KERNEL
);
938 rc
= digital_in_send_cmd(ddev
, tmp_skb
, ddev
->dep_rwt
,
939 digital_in_recv_dep_res
, data_exch
);
944 kfree_skb(chaining_skb
);
945 ddev
->chaining_skb
= NULL
;
947 kfree_skb(ddev
->saved_skb
);
948 ddev
->saved_skb
= NULL
;
954 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
956 ddev
->curr_rf_tech
= rf_tech
;
958 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
959 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
961 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
964 switch (ddev
->curr_rf_tech
) {
965 case NFC_DIGITAL_RF_TECH_106A
:
966 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
967 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
970 case NFC_DIGITAL_RF_TECH_212F
:
971 case NFC_DIGITAL_RF_TECH_424F
:
972 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
973 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
981 static int digital_tg_send_ack(struct nfc_digital_dev
*ddev
,
982 struct digital_data_exch
*data_exch
)
984 struct digital_dep_req_res
*dep_res
;
988 skb
= digital_skb_alloc(ddev
, 1);
992 skb_push(skb
, sizeof(struct digital_dep_req_res
));
994 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
996 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
997 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
998 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
999 ddev
->curr_nfc_dep_pni
;
1002 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1004 skb_put_data(skb
, &ddev
->did
, sizeof(ddev
->did
));
1007 ddev
->curr_nfc_dep_pni
=
1008 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1010 digital_skb_push_dep_sod(ddev
, skb
);
1012 ddev
->skb_add_crc(skb
);
1014 ddev
->saved_skb
= pskb_copy(skb
, GFP_KERNEL
);
1016 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1020 kfree_skb(ddev
->saved_skb
);
1021 ddev
->saved_skb
= NULL
;
1027 static int digital_tg_send_atn(struct nfc_digital_dev
*ddev
)
1029 struct digital_dep_req_res
*dep_res
;
1030 struct sk_buff
*skb
;
1033 skb
= digital_skb_alloc(ddev
, 1);
1037 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1039 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1041 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1042 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1043 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
1046 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1048 skb_put_data(skb
, &ddev
->did
, sizeof(ddev
->did
));
1051 digital_skb_push_dep_sod(ddev
, skb
);
1053 ddev
->skb_add_crc(skb
);
1055 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1063 static int digital_tg_send_saved_skb(struct nfc_digital_dev
*ddev
)
1067 if (!ddev
->saved_skb
)
1070 skb_get(ddev
->saved_skb
);
1072 rc
= digital_tg_send_cmd(ddev
, ddev
->saved_skb
, 1500,
1073 digital_tg_recv_dep_req
, NULL
);
1075 kfree_skb(ddev
->saved_skb
);
1080 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
1081 struct sk_buff
*resp
)
1084 struct digital_dep_req_res
*dep_req
;
1094 rc
= ddev
->skb_check_crc(resp
);
1096 PROTOCOL_ERR("14.4.1.6");
1100 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1102 PROTOCOL_ERR("14.4.1.2");
1106 if (resp
->len
> ddev
->local_payload_max
) {
1111 size
= sizeof(struct digital_dep_req_res
);
1112 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
1114 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1115 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
1122 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
1123 if (ddev
->did
&& (ddev
->did
== resp
->data
[3])) {
1129 } else if (ddev
->did
) {
1134 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
1139 if (size
> resp
->len
) {
1144 skb_pull(resp
, size
);
1146 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
1147 case DIGITAL_NFC_DEP_PFB_I_PDU
:
1148 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1150 if (ddev
->atn_count
) {
1151 /* The target has received (and replied to) at least one
1154 ddev
->atn_count
= 0;
1156 /* pni of resp PDU equal to the target current pni - 1
1157 * means resp is the previous DEP_REQ PDU received from
1158 * the initiator so the target replies with saved_skb
1159 * which is the previous DEP_RES saved in
1160 * digital_tg_send_dep_res().
1162 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) ==
1163 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
- 1)) {
1164 rc
= digital_tg_send_saved_skb(ddev
);
1171 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1172 * means the target probably did not received the last
1173 * DEP_REQ PDU sent by the initiator. The target
1174 * fallbacks to normal processing then.
1178 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
1179 PROTOCOL_ERR("14.12.3.4");
1184 kfree_skb(ddev
->saved_skb
);
1185 ddev
->saved_skb
= NULL
;
1187 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
1188 digital_tg_send_ack
, NULL
);
1195 /* If resp is NULL then we're still chaining so return and
1196 * wait for the next part of the PDU. Else, the PDU is
1197 * complete so pass it up.
1204 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
1205 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) { /* NACK */
1206 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) !=
1207 ddev
->curr_nfc_dep_pni
) {
1212 ddev
->atn_count
= 0;
1214 rc
= digital_tg_send_saved_skb(ddev
);
1222 if (ddev
->atn_count
) {
1223 /* The target has previously recevied one or more ATN
1226 ddev
->atn_count
= 0;
1228 /* If the ACK PNI is equal to the target PNI - 1 means
1229 * that the initiator did not receive the previous PDU
1230 * sent by the target so re-send it.
1232 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) ==
1233 ddev
->curr_nfc_dep_pni
) {
1234 rc
= digital_tg_send_saved_skb(ddev
);
1241 /* Otherwise, the target did not receive the previous
1242 * ACK PDU from the initiator. Fallback to normal
1243 * processing of chained PDU then.
1247 /* Keep on sending chained PDU */
1248 if (!ddev
->chaining_skb
||
1249 DIGITAL_NFC_DEP_PFB_PNI(pfb
) !=
1250 ddev
->curr_nfc_dep_pni
) {
1255 kfree_skb(ddev
->saved_skb
);
1256 ddev
->saved_skb
= NULL
;
1258 rc
= digital_tg_send_dep_res(ddev
, ddev
->chaining_skb
);
1263 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
1264 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
1269 rc
= digital_tg_send_atn(ddev
);
1278 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
1281 kfree_skb(ddev
->chaining_skb
);
1282 ddev
->chaining_skb
= NULL
;
1284 ddev
->atn_count
= 0;
1286 kfree_skb(ddev
->saved_skb
);
1287 ddev
->saved_skb
= NULL
;
1295 dev_kfree_skb(resp
);
1298 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
1300 struct digital_dep_req_res
*dep_res
;
1301 struct sk_buff
*chaining_skb
, *tmp_skb
;
1304 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1306 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1308 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1309 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1310 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
1313 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1315 skb_put_data(skb
, &ddev
->did
, sizeof(ddev
->did
));
1318 ddev
->curr_nfc_dep_pni
=
1319 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1321 chaining_skb
= ddev
->chaining_skb
;
1323 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_res
, NULL
);
1324 if (IS_ERR(tmp_skb
))
1325 return PTR_ERR(tmp_skb
);
1327 digital_skb_push_dep_sod(ddev
, tmp_skb
);
1329 ddev
->skb_add_crc(tmp_skb
);
1331 ddev
->saved_skb
= pskb_copy(tmp_skb
, GFP_KERNEL
);
1333 rc
= digital_tg_send_cmd(ddev
, tmp_skb
, 1500, digital_tg_recv_dep_req
,
1339 kfree_skb(chaining_skb
);
1340 ddev
->chaining_skb
= NULL
;
1342 kfree_skb(ddev
->saved_skb
);
1343 ddev
->saved_skb
= NULL
;
1349 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
1350 void *arg
, struct sk_buff
*resp
)
1352 u8 rf_tech
= (unsigned long)arg
;
1357 digital_tg_set_rf_tech(ddev
, rf_tech
);
1359 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
1361 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
1363 dev_kfree_skb(resp
);
1366 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
1369 struct digital_psl_res
*psl_res
;
1370 struct sk_buff
*skb
;
1373 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
1377 skb_put(skb
, sizeof(struct digital_psl_res
));
1379 psl_res
= (struct digital_psl_res
*)skb
->data
;
1381 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1382 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
1385 digital_skb_push_dep_sod(ddev
, skb
);
1387 ddev
->skb_add_crc(skb
);
1389 ddev
->curr_nfc_dep_pni
= 0;
1391 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
1392 (void *)(unsigned long)rf_tech
);
1399 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
1400 struct sk_buff
*resp
)
1403 struct digital_psl_req
*psl_req
;
1405 u8 dsi
, payload_size
, payload_bits
;
1413 rc
= ddev
->skb_check_crc(resp
);
1415 PROTOCOL_ERR("14.4.1.6");
1419 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1421 PROTOCOL_ERR("14.4.1.2");
1425 psl_req
= (struct digital_psl_req
*)resp
->data
;
1427 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
1428 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1429 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
1434 dsi
= (psl_req
->brs
>> 3) & 0x07;
1437 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
1440 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
1443 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
1446 pr_err("Unsupported dsi value %d\n", dsi
);
1450 payload_bits
= DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req
->fsl
);
1451 payload_size
= digital_payload_bits_to_size(payload_bits
);
1453 if (!payload_size
|| (payload_size
> min(ddev
->local_payload_max
,
1454 ddev
->remote_payload_max
))) {
1459 ddev
->local_payload_max
= payload_size
;
1460 ddev
->remote_payload_max
= payload_size
;
1462 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
1468 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
1469 void *arg
, struct sk_buff
*resp
)
1474 digital_poll_next_tech(ddev
);
1479 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
1482 ddev
->atn_count
= 0;
1484 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
1485 digital_tg_recv_psl_req(ddev
, arg
, resp
);
1487 digital_tg_recv_dep_req(ddev
, arg
, resp
);
1490 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
1491 struct digital_atr_req
*atr_req
)
1493 struct digital_atr_res
*atr_res
;
1494 struct sk_buff
*skb
;
1495 u8
*gb
, payload_bits
;
1499 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
1503 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
1507 skb_put(skb
, sizeof(struct digital_atr_res
));
1508 atr_res
= (struct digital_atr_res
*)skb
->data
;
1510 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
1512 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1513 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
1514 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
1515 atr_res
->to
= DIGITAL_NFC_DEP_TG_MAX_WT
;
1517 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
1518 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
1519 atr_res
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
1522 skb_put(skb
, gb_len
);
1524 atr_res
->pp
|= DIGITAL_GB_BIT
;
1525 memcpy(atr_res
->gb
, gb
, gb_len
);
1528 digital_skb_push_dep_sod(ddev
, skb
);
1530 ddev
->skb_add_crc(skb
);
1532 ddev
->curr_nfc_dep_pni
= 0;
1534 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
1535 digital_tg_send_atr_res_complete
, NULL
);
1542 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
1543 struct sk_buff
*resp
)
1546 struct digital_atr_req
*atr_req
;
1547 size_t gb_len
, min_size
;
1548 u8 poll_tech_count
, payload_bits
;
1561 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
1562 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
1563 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
1565 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
1566 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
1569 if (resp
->len
< min_size
) {
1574 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
1576 rc
= ddev
->skb_check_crc(resp
);
1578 PROTOCOL_ERR("14.4.1.6");
1582 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1584 PROTOCOL_ERR("14.4.1.2");
1588 atr_req
= (struct digital_atr_req
*)resp
->data
;
1590 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1591 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
||
1592 atr_req
->did
> DIGITAL_DID_MAX
) {
1597 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_req
->pp
);
1598 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
1600 if (!ddev
->remote_payload_max
) {
1605 ddev
->did
= atr_req
->did
;
1607 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1608 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
1612 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
1616 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
1618 poll_tech_count
= ddev
->poll_tech_count
;
1619 ddev
->poll_tech_count
= 0;
1621 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1622 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
1624 ddev
->poll_tech_count
= poll_tech_count
;
1631 digital_poll_next_tech(ddev
);
1633 dev_kfree_skb(resp
);