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_N_RETRY_NACK 2
21 #define DIGITAL_NFC_DEP_N_RETRY_ATN 2
23 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
24 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
26 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
28 #define DIGITAL_CMD_ATR_REQ 0x00
29 #define DIGITAL_CMD_ATR_RES 0x01
30 #define DIGITAL_CMD_PSL_REQ 0x04
31 #define DIGITAL_CMD_PSL_RES 0x05
32 #define DIGITAL_CMD_DEP_REQ 0x06
33 #define DIGITAL_CMD_DEP_RES 0x07
35 #define DIGITAL_ATR_REQ_MIN_SIZE 16
36 #define DIGITAL_ATR_REQ_MAX_SIZE 64
38 #define DIGITAL_ATR_RES_TO_WT(s) ((s) & 0xF)
40 #define DIGITAL_DID_MAX 14
42 #define DIGITAL_PAYLOAD_SIZE_MAX 254
43 #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
44 #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
45 #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
46 #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
48 #define DIGITAL_GB_BIT 0x02
50 #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
51 #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
53 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
55 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
56 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
57 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
58 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
60 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
61 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
62 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
63 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
64 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
65 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
66 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
68 #define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
69 #define DIGITAL_NFC_DEP_RTOX_MAX 59
71 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
72 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
73 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
75 struct digital_atr_req
{
86 struct digital_atr_res
{
98 struct digital_psl_req
{
106 struct digital_psl_res
{
112 struct digital_dep_req_res
{
118 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
119 struct sk_buff
*resp
);
120 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
121 struct sk_buff
*resp
);
123 static const u8 digital_payload_bits_map
[4] = {
130 /* Response Waiting Time for ATR_RES PDU in ms
132 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
135 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
136 * dRWT(nfcdep) = 16 / f(c) s
137 * dT(nfcdep,initiator) = 100 ms
140 #define DIGITAL_ATR_RES_RWT 1337
142 /* Response Waiting Time for other DEP PDUs in ms
144 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
147 * rwt = (256 * 16 / f(c)) * 2^wt s
148 * dRWT(nfcdep) = 16 / f(c) s
149 * dT(nfcdep,initiator) = 100 ms
151 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
153 #define DIGITAL_NFC_DEP_IN_MAX_WT 14
154 #define DIGITAL_NFC_DEP_TG_MAX_WT 14
155 static const u16 digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
+ 1] = {
156 100, 101, 101, 102, 105,
157 110, 119, 139, 177, 255,
158 409, 719, 1337, 2575, 5049,
161 static u8
digital_payload_bits_to_size(u8 payload_bits
)
163 if (payload_bits
>= ARRAY_SIZE(digital_payload_bits_map
))
166 return digital_payload_bits_map
[payload_bits
];
169 static u8
digital_payload_size_to_bits(u8 payload_size
)
173 for (i
= 0; i
< ARRAY_SIZE(digital_payload_bits_map
); i
++)
174 if (digital_payload_bits_map
[i
] == payload_size
)
180 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
183 skb_push(skb
, sizeof(u8
));
185 skb
->data
[0] = skb
->len
;
187 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
188 *(u8
*)skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
191 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
199 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
200 skb_pull(skb
, sizeof(u8
));
203 if (size
!= skb
->len
)
206 skb_pull(skb
, sizeof(u8
));
211 static struct sk_buff
*
212 digital_send_dep_data_prep(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
,
213 struct digital_dep_req_res
*dep_req_res
,
214 struct digital_data_exch
*data_exch
)
216 struct sk_buff
*new_skb
;
218 if (skb
->len
> ddev
->remote_payload_max
) {
219 dep_req_res
->pfb
|= DIGITAL_NFC_DEP_PFB_MI_BIT
;
221 new_skb
= digital_skb_alloc(ddev
, ddev
->remote_payload_max
);
223 kfree_skb(ddev
->chaining_skb
);
224 ddev
->chaining_skb
= NULL
;
226 return ERR_PTR(-ENOMEM
);
229 skb_put_data(new_skb
, skb
->data
, ddev
->remote_payload_max
);
230 skb_pull(skb
, ddev
->remote_payload_max
);
232 ddev
->chaining_skb
= skb
;
233 ddev
->data_exch
= data_exch
;
235 ddev
->chaining_skb
= NULL
;
242 static struct sk_buff
*
243 digital_recv_dep_data_gather(struct nfc_digital_dev
*ddev
, u8 pfb
,
244 struct sk_buff
*resp
,
245 int (*send_ack
)(struct nfc_digital_dev
*ddev
,
246 struct digital_data_exch
248 struct digital_data_exch
*data_exch
)
250 struct sk_buff
*new_skb
;
253 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
) && (!ddev
->chaining_skb
)) {
255 nfc_alloc_recv_skb(8 * ddev
->local_payload_max
,
257 if (!ddev
->chaining_skb
) {
263 if (ddev
->chaining_skb
) {
264 if (resp
->len
> skb_tailroom(ddev
->chaining_skb
)) {
265 new_skb
= skb_copy_expand(ddev
->chaining_skb
,
268 8 * ddev
->local_payload_max
,
275 kfree_skb(ddev
->chaining_skb
);
276 ddev
->chaining_skb
= new_skb
;
279 skb_put_data(ddev
->chaining_skb
, resp
->data
, resp
->len
);
284 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
285 rc
= send_ack(ddev
, data_exch
);
292 resp
= ddev
->chaining_skb
;
293 ddev
->chaining_skb
= NULL
;
301 kfree_skb(ddev
->chaining_skb
);
302 ddev
->chaining_skb
= NULL
;
307 static void digital_in_recv_psl_res(struct nfc_digital_dev
*ddev
, void *arg
,
308 struct sk_buff
*resp
)
310 struct nfc_target
*target
= arg
;
311 struct digital_psl_res
*psl_res
;
320 rc
= ddev
->skb_check_crc(resp
);
322 PROTOCOL_ERR("14.4.1.6");
326 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
328 PROTOCOL_ERR("14.4.1.2");
332 psl_res
= (struct digital_psl_res
*)resp
->data
;
334 if ((resp
->len
!= sizeof(*psl_res
)) ||
335 (psl_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
) ||
336 (psl_res
->cmd
!= DIGITAL_CMD_PSL_RES
)) {
341 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
342 NFC_DIGITAL_RF_TECH_424F
);
346 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
347 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
);
351 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
) &&
352 (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)) {
353 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
354 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
357 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
359 nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
362 ddev
->curr_nfc_dep_pni
= 0;
368 ddev
->curr_protocol
= 0;
371 static int digital_in_send_psl_req(struct nfc_digital_dev
*ddev
,
372 struct nfc_target
*target
)
375 struct digital_psl_req
*psl_req
;
377 u8 payload_size
, payload_bits
;
379 skb
= digital_skb_alloc(ddev
, sizeof(*psl_req
));
383 skb_put(skb
, sizeof(*psl_req
));
385 psl_req
= (struct digital_psl_req
*)skb
->data
;
387 psl_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
388 psl_req
->cmd
= DIGITAL_CMD_PSL_REQ
;
390 psl_req
->brs
= (0x2 << 3) | 0x2; /* 424F both directions */
392 payload_size
= min(ddev
->local_payload_max
, ddev
->remote_payload_max
);
393 payload_bits
= digital_payload_size_to_bits(payload_size
);
394 psl_req
->fsl
= DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits
);
396 ddev
->local_payload_max
= payload_size
;
397 ddev
->remote_payload_max
= payload_size
;
399 digital_skb_push_dep_sod(ddev
, skb
);
401 ddev
->skb_add_crc(skb
);
403 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
404 digital_in_recv_psl_res
, target
);
411 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
412 struct sk_buff
*resp
)
414 struct nfc_target
*target
= arg
;
415 struct digital_atr_res
*atr_res
;
416 u8 gb_len
, payload_bits
;
426 rc
= ddev
->skb_check_crc(resp
);
428 PROTOCOL_ERR("14.4.1.6");
432 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
434 PROTOCOL_ERR("14.4.1.2");
438 if (resp
->len
< sizeof(struct digital_atr_res
)) {
443 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
445 atr_res
= (struct digital_atr_res
*)resp
->data
;
447 wt
= DIGITAL_ATR_RES_TO_WT(atr_res
->to
);
448 if (wt
> DIGITAL_NFC_DEP_IN_MAX_WT
)
449 wt
= DIGITAL_NFC_DEP_IN_MAX_WT
;
450 ddev
->dep_rwt
= digital_rwt_map
[wt
];
452 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_res
->pp
);
453 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
455 if (!ddev
->remote_payload_max
) {
460 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
464 if ((ddev
->protocols
& NFC_PROTO_FELICA_MASK
) &&
465 (ddev
->curr_rf_tech
!= NFC_DIGITAL_RF_TECH_424F
)) {
466 rc
= digital_in_send_psl_req(ddev
, target
);
471 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
474 ddev
->curr_nfc_dep_pni
= 0;
480 ddev
->curr_protocol
= 0;
483 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
484 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
488 struct digital_atr_req
*atr_req
;
493 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
495 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
496 PROTOCOL_ERR("14.6.1.1");
500 skb
= digital_skb_alloc(ddev
, size
);
504 skb_put(skb
, sizeof(struct digital_atr_req
));
506 atr_req
= (struct digital_atr_req
*)skb
->data
;
507 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
509 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
510 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
511 if (target
->nfcid2_len
)
512 memcpy(atr_req
->nfcid3
, target
->nfcid2
, NFC_NFCID2_MAXSIZE
);
514 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
520 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
521 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
522 atr_req
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
525 atr_req
->pp
|= DIGITAL_GB_BIT
;
526 skb_put_data(skb
, gb
, gb_len
);
529 digital_skb_push_dep_sod(ddev
, skb
);
531 ddev
->skb_add_crc(skb
);
533 rc
= digital_in_send_cmd(ddev
, skb
, DIGITAL_ATR_RES_RWT
,
534 digital_in_recv_atr_res
, target
);
541 static int digital_in_send_ack(struct nfc_digital_dev
*ddev
,
542 struct digital_data_exch
*data_exch
)
544 struct digital_dep_req_res
*dep_req
;
548 skb
= digital_skb_alloc(ddev
, 1);
552 skb_push(skb
, sizeof(struct digital_dep_req_res
));
554 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
556 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
557 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
558 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
559 ddev
->curr_nfc_dep_pni
;
561 digital_skb_push_dep_sod(ddev
, skb
);
563 ddev
->skb_add_crc(skb
);
565 ddev
->saved_skb
= pskb_copy(skb
, GFP_KERNEL
);
567 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
568 digital_in_recv_dep_res
, data_exch
);
571 kfree_skb(ddev
->saved_skb
);
572 ddev
->saved_skb
= NULL
;
578 static int digital_in_send_nack(struct nfc_digital_dev
*ddev
,
579 struct digital_data_exch
*data_exch
)
581 struct digital_dep_req_res
*dep_req
;
585 skb
= digital_skb_alloc(ddev
, 1);
589 skb_push(skb
, sizeof(struct digital_dep_req_res
));
591 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
593 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
594 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
595 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
596 DIGITAL_NFC_DEP_PFB_NACK_BIT
| ddev
->curr_nfc_dep_pni
;
598 digital_skb_push_dep_sod(ddev
, skb
);
600 ddev
->skb_add_crc(skb
);
602 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
603 digital_in_recv_dep_res
, data_exch
);
610 static int digital_in_send_atn(struct nfc_digital_dev
*ddev
,
611 struct digital_data_exch
*data_exch
)
613 struct digital_dep_req_res
*dep_req
;
617 skb
= digital_skb_alloc(ddev
, 1);
621 skb_push(skb
, sizeof(struct digital_dep_req_res
));
623 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
625 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
626 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
627 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
629 digital_skb_push_dep_sod(ddev
, skb
);
631 ddev
->skb_add_crc(skb
);
633 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
634 digital_in_recv_dep_res
, data_exch
);
641 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
642 struct digital_data_exch
*data_exch
, u8 rtox
)
644 struct digital_dep_req_res
*dep_req
;
649 rwt_int
= ddev
->dep_rwt
* rtox
;
650 if (rwt_int
> digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
])
651 rwt_int
= digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
];
653 skb
= digital_skb_alloc(ddev
, 1);
657 skb_put_u8(skb
, rtox
);
659 skb_push(skb
, sizeof(struct digital_dep_req_res
));
661 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
663 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
664 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
665 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
666 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
668 digital_skb_push_dep_sod(ddev
, skb
);
670 ddev
->skb_add_crc(skb
);
672 rc
= digital_in_send_cmd(ddev
, skb
, rwt_int
,
673 digital_in_recv_dep_res
, data_exch
);
680 static int digital_in_send_saved_skb(struct nfc_digital_dev
*ddev
,
681 struct digital_data_exch
*data_exch
)
685 if (!ddev
->saved_skb
)
688 skb_get(ddev
->saved_skb
);
690 rc
= digital_in_send_cmd(ddev
, ddev
->saved_skb
, ddev
->dep_rwt
,
691 digital_in_recv_dep_res
, data_exch
);
693 kfree_skb(ddev
->saved_skb
);
698 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
699 struct sk_buff
*resp
)
701 struct digital_data_exch
*data_exch
= arg
;
702 struct digital_dep_req_res
*dep_res
;
712 if ((rc
== -EIO
|| (rc
== -ETIMEDOUT
&& ddev
->nack_count
)) &&
713 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
716 rc
= digital_in_send_nack(ddev
, data_exch
);
721 } else if ((rc
== -ETIMEDOUT
) &&
722 (ddev
->atn_count
++ < DIGITAL_NFC_DEP_N_RETRY_ATN
)) {
723 ddev
->nack_count
= 0;
725 rc
= digital_in_send_atn(ddev
, data_exch
);
735 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
737 PROTOCOL_ERR("14.4.1.2");
741 rc
= ddev
->skb_check_crc(resp
);
743 if ((resp
->len
>= 4) &&
744 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
747 rc
= digital_in_send_nack(ddev
, data_exch
);
756 PROTOCOL_ERR("14.4.1.6");
761 ddev
->nack_count
= 0;
763 if (resp
->len
> ddev
->local_payload_max
) {
768 size
= sizeof(struct digital_dep_req_res
);
769 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
771 if (resp
->len
< size
|| dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
772 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
779 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
780 PROTOCOL_ERR("14.8.2.1");
785 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
790 if (size
> resp
->len
) {
795 skb_pull(resp
, size
);
797 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
798 case DIGITAL_NFC_DEP_PFB_I_PDU
:
799 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
800 PROTOCOL_ERR("14.12.3.3");
805 ddev
->curr_nfc_dep_pni
=
806 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
808 kfree_skb(ddev
->saved_skb
);
809 ddev
->saved_skb
= NULL
;
811 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
820 /* If resp is NULL then we're still chaining so return and
821 * wait for the next part of the PDU. Else, the PDU is
822 * complete so pass it up.
830 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
831 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) {
832 PROTOCOL_ERR("14.12.4.5");
837 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
838 PROTOCOL_ERR("14.12.3.3");
843 ddev
->curr_nfc_dep_pni
=
844 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
846 if (!ddev
->chaining_skb
) {
847 PROTOCOL_ERR("14.12.4.3");
852 /* The initiator has received a valid ACK. Free the last sent
853 * PDU and keep on sending chained skb.
855 kfree_skb(ddev
->saved_skb
);
856 ddev
->saved_skb
= NULL
;
858 rc
= digital_in_send_dep_req(ddev
, NULL
,
866 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
867 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) { /* ATN */
868 rc
= digital_in_send_saved_skb(ddev
, data_exch
);
875 if (ddev
->atn_count
|| ddev
->nack_count
) {
876 PROTOCOL_ERR("14.12.4.4");
881 rtox
= DIGITAL_NFC_DEP_RTOX_VALUE(resp
->data
[0]);
882 if (!rtox
|| rtox
> DIGITAL_NFC_DEP_RTOX_MAX
) {
883 PROTOCOL_ERR("14.8.4.1");
888 rc
= digital_in_send_rtox(ddev
, data_exch
, rtox
);
896 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
901 kfree_skb(ddev
->chaining_skb
);
902 ddev
->chaining_skb
= NULL
;
904 kfree_skb(ddev
->saved_skb
);
905 ddev
->saved_skb
= NULL
;
916 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
917 struct nfc_target
*target
, struct sk_buff
*skb
,
918 struct digital_data_exch
*data_exch
)
920 struct digital_dep_req_res
*dep_req
;
921 struct sk_buff
*chaining_skb
, *tmp_skb
;
924 skb_push(skb
, sizeof(struct digital_dep_req_res
));
926 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
928 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
929 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
930 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
933 ddev
->nack_count
= 0;
935 chaining_skb
= ddev
->chaining_skb
;
937 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_req
, data_exch
);
939 return PTR_ERR(tmp_skb
);
941 digital_skb_push_dep_sod(ddev
, tmp_skb
);
943 ddev
->skb_add_crc(tmp_skb
);
945 ddev
->saved_skb
= pskb_copy(tmp_skb
, GFP_KERNEL
);
947 rc
= digital_in_send_cmd(ddev
, tmp_skb
, ddev
->dep_rwt
,
948 digital_in_recv_dep_res
, data_exch
);
953 kfree_skb(chaining_skb
);
954 ddev
->chaining_skb
= NULL
;
956 kfree_skb(ddev
->saved_skb
);
957 ddev
->saved_skb
= NULL
;
963 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
965 ddev
->curr_rf_tech
= rf_tech
;
967 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
968 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
970 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
973 switch (ddev
->curr_rf_tech
) {
974 case NFC_DIGITAL_RF_TECH_106A
:
975 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
976 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
979 case NFC_DIGITAL_RF_TECH_212F
:
980 case NFC_DIGITAL_RF_TECH_424F
:
981 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
982 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
990 static int digital_tg_send_ack(struct nfc_digital_dev
*ddev
,
991 struct digital_data_exch
*data_exch
)
993 struct digital_dep_req_res
*dep_res
;
997 skb
= digital_skb_alloc(ddev
, 1);
1001 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1003 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1005 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1006 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1007 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
1008 ddev
->curr_nfc_dep_pni
;
1011 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1013 skb_put_data(skb
, &ddev
->did
, sizeof(ddev
->did
));
1016 ddev
->curr_nfc_dep_pni
=
1017 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1019 digital_skb_push_dep_sod(ddev
, skb
);
1021 ddev
->skb_add_crc(skb
);
1023 ddev
->saved_skb
= pskb_copy(skb
, GFP_KERNEL
);
1025 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1029 kfree_skb(ddev
->saved_skb
);
1030 ddev
->saved_skb
= NULL
;
1036 static int digital_tg_send_atn(struct nfc_digital_dev
*ddev
)
1038 struct digital_dep_req_res
*dep_res
;
1039 struct sk_buff
*skb
;
1042 skb
= digital_skb_alloc(ddev
, 1);
1046 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1048 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1050 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1051 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1052 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
1055 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1057 skb_put_data(skb
, &ddev
->did
, sizeof(ddev
->did
));
1060 digital_skb_push_dep_sod(ddev
, skb
);
1062 ddev
->skb_add_crc(skb
);
1064 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1072 static int digital_tg_send_saved_skb(struct nfc_digital_dev
*ddev
)
1076 if (!ddev
->saved_skb
)
1079 skb_get(ddev
->saved_skb
);
1081 rc
= digital_tg_send_cmd(ddev
, ddev
->saved_skb
, 1500,
1082 digital_tg_recv_dep_req
, NULL
);
1084 kfree_skb(ddev
->saved_skb
);
1089 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
1090 struct sk_buff
*resp
)
1093 struct digital_dep_req_res
*dep_req
;
1103 rc
= ddev
->skb_check_crc(resp
);
1105 PROTOCOL_ERR("14.4.1.6");
1109 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1111 PROTOCOL_ERR("14.4.1.2");
1115 if (resp
->len
> ddev
->local_payload_max
) {
1120 size
= sizeof(struct digital_dep_req_res
);
1121 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
1123 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1124 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
1131 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
1132 if (ddev
->did
&& (ddev
->did
== resp
->data
[3])) {
1138 } else if (ddev
->did
) {
1143 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
1148 if (size
> resp
->len
) {
1153 skb_pull(resp
, size
);
1155 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
1156 case DIGITAL_NFC_DEP_PFB_I_PDU
:
1157 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1159 if (ddev
->atn_count
) {
1160 /* The target has received (and replied to) at least one
1163 ddev
->atn_count
= 0;
1165 /* pni of resp PDU equal to the target current pni - 1
1166 * means resp is the previous DEP_REQ PDU received from
1167 * the initiator so the target replies with saved_skb
1168 * which is the previous DEP_RES saved in
1169 * digital_tg_send_dep_res().
1171 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) ==
1172 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
- 1)) {
1173 rc
= digital_tg_send_saved_skb(ddev
);
1180 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1181 * means the target probably did not received the last
1182 * DEP_REQ PDU sent by the initiator. The target
1183 * fallbacks to normal processing then.
1187 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
1188 PROTOCOL_ERR("14.12.3.4");
1193 kfree_skb(ddev
->saved_skb
);
1194 ddev
->saved_skb
= NULL
;
1196 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
1197 digital_tg_send_ack
, NULL
);
1204 /* If resp is NULL then we're still chaining so return and
1205 * wait for the next part of the PDU. Else, the PDU is
1206 * complete so pass it up.
1213 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
1214 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) { /* NACK */
1215 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) !=
1216 ddev
->curr_nfc_dep_pni
) {
1221 ddev
->atn_count
= 0;
1223 rc
= digital_tg_send_saved_skb(ddev
);
1231 if (ddev
->atn_count
) {
1232 /* The target has previously recevied one or more ATN
1235 ddev
->atn_count
= 0;
1237 /* If the ACK PNI is equal to the target PNI - 1 means
1238 * that the initiator did not receive the previous PDU
1239 * sent by the target so re-send it.
1241 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) ==
1242 ddev
->curr_nfc_dep_pni
) {
1243 rc
= digital_tg_send_saved_skb(ddev
);
1250 /* Otherwise, the target did not receive the previous
1251 * ACK PDU from the initiator. Fallback to normal
1252 * processing of chained PDU then.
1256 /* Keep on sending chained PDU */
1257 if (!ddev
->chaining_skb
||
1258 DIGITAL_NFC_DEP_PFB_PNI(pfb
) !=
1259 ddev
->curr_nfc_dep_pni
) {
1264 kfree_skb(ddev
->saved_skb
);
1265 ddev
->saved_skb
= NULL
;
1267 rc
= digital_tg_send_dep_res(ddev
, ddev
->chaining_skb
);
1272 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
1273 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
1278 rc
= digital_tg_send_atn(ddev
);
1287 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
1290 kfree_skb(ddev
->chaining_skb
);
1291 ddev
->chaining_skb
= NULL
;
1293 ddev
->atn_count
= 0;
1295 kfree_skb(ddev
->saved_skb
);
1296 ddev
->saved_skb
= NULL
;
1304 dev_kfree_skb(resp
);
1307 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
1309 struct digital_dep_req_res
*dep_res
;
1310 struct sk_buff
*chaining_skb
, *tmp_skb
;
1313 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1315 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1317 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1318 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1319 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
1322 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1324 skb_put_data(skb
, &ddev
->did
, sizeof(ddev
->did
));
1327 ddev
->curr_nfc_dep_pni
=
1328 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1330 chaining_skb
= ddev
->chaining_skb
;
1332 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_res
, NULL
);
1333 if (IS_ERR(tmp_skb
))
1334 return PTR_ERR(tmp_skb
);
1336 digital_skb_push_dep_sod(ddev
, tmp_skb
);
1338 ddev
->skb_add_crc(tmp_skb
);
1340 ddev
->saved_skb
= pskb_copy(tmp_skb
, GFP_KERNEL
);
1342 rc
= digital_tg_send_cmd(ddev
, tmp_skb
, 1500, digital_tg_recv_dep_req
,
1348 kfree_skb(chaining_skb
);
1349 ddev
->chaining_skb
= NULL
;
1351 kfree_skb(ddev
->saved_skb
);
1352 ddev
->saved_skb
= NULL
;
1358 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
1359 void *arg
, struct sk_buff
*resp
)
1361 u8 rf_tech
= (unsigned long)arg
;
1366 digital_tg_set_rf_tech(ddev
, rf_tech
);
1368 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
1370 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
1372 dev_kfree_skb(resp
);
1375 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
1378 struct digital_psl_res
*psl_res
;
1379 struct sk_buff
*skb
;
1382 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
1386 skb_put(skb
, sizeof(struct digital_psl_res
));
1388 psl_res
= (struct digital_psl_res
*)skb
->data
;
1390 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1391 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
1394 digital_skb_push_dep_sod(ddev
, skb
);
1396 ddev
->skb_add_crc(skb
);
1398 ddev
->curr_nfc_dep_pni
= 0;
1400 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
1401 (void *)(unsigned long)rf_tech
);
1408 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
1409 struct sk_buff
*resp
)
1412 struct digital_psl_req
*psl_req
;
1414 u8 dsi
, payload_size
, payload_bits
;
1422 rc
= ddev
->skb_check_crc(resp
);
1424 PROTOCOL_ERR("14.4.1.6");
1428 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1430 PROTOCOL_ERR("14.4.1.2");
1434 psl_req
= (struct digital_psl_req
*)resp
->data
;
1436 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
1437 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1438 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
1443 dsi
= (psl_req
->brs
>> 3) & 0x07;
1446 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
1449 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
1452 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
1455 pr_err("Unsupported dsi value %d\n", dsi
);
1459 payload_bits
= DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req
->fsl
);
1460 payload_size
= digital_payload_bits_to_size(payload_bits
);
1462 if (!payload_size
|| (payload_size
> min(ddev
->local_payload_max
,
1463 ddev
->remote_payload_max
))) {
1468 ddev
->local_payload_max
= payload_size
;
1469 ddev
->remote_payload_max
= payload_size
;
1471 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
1477 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
1478 void *arg
, struct sk_buff
*resp
)
1483 digital_poll_next_tech(ddev
);
1488 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
1491 ddev
->atn_count
= 0;
1493 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
1494 digital_tg_recv_psl_req(ddev
, arg
, resp
);
1496 digital_tg_recv_dep_req(ddev
, arg
, resp
);
1499 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
1500 struct digital_atr_req
*atr_req
)
1502 struct digital_atr_res
*atr_res
;
1503 struct sk_buff
*skb
;
1504 u8
*gb
, payload_bits
;
1508 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
1512 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
1516 skb_put(skb
, sizeof(struct digital_atr_res
));
1517 atr_res
= (struct digital_atr_res
*)skb
->data
;
1519 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
1521 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1522 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
1523 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
1524 atr_res
->to
= DIGITAL_NFC_DEP_TG_MAX_WT
;
1526 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
1527 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
1528 atr_res
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
1531 skb_put(skb
, gb_len
);
1533 atr_res
->pp
|= DIGITAL_GB_BIT
;
1534 memcpy(atr_res
->gb
, gb
, gb_len
);
1537 digital_skb_push_dep_sod(ddev
, skb
);
1539 ddev
->skb_add_crc(skb
);
1541 ddev
->curr_nfc_dep_pni
= 0;
1543 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
1544 digital_tg_send_atr_res_complete
, NULL
);
1551 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
1552 struct sk_buff
*resp
)
1555 struct digital_atr_req
*atr_req
;
1556 size_t gb_len
, min_size
;
1557 u8 poll_tech_count
, payload_bits
;
1570 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
1571 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
1572 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
1574 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
1575 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
1578 if (resp
->len
< min_size
) {
1583 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
1585 rc
= ddev
->skb_check_crc(resp
);
1587 PROTOCOL_ERR("14.4.1.6");
1591 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1593 PROTOCOL_ERR("14.4.1.2");
1597 atr_req
= (struct digital_atr_req
*)resp
->data
;
1599 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1600 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
||
1601 atr_req
->did
> DIGITAL_DID_MAX
) {
1606 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_req
->pp
);
1607 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
1609 if (!ddev
->remote_payload_max
) {
1614 ddev
->did
= atr_req
->did
;
1616 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1617 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
1621 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
1625 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
1627 poll_tech_count
= ddev
->poll_tech_count
;
1628 ddev
->poll_tech_count
= 0;
1630 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1631 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
1633 ddev
->poll_tech_count
= poll_tech_count
;
1640 digital_poll_next_tech(ddev
);
1642 dev_kfree_skb(resp
);