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_DID_MAX 14
40 #define DIGITAL_PAYLOAD_SIZE_MAX 254
41 #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
42 #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
43 #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
44 #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
46 #define DIGITAL_GB_BIT 0x02
48 #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
49 #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
51 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
53 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
54 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
55 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
56 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
58 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
59 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
60 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
61 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
62 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
63 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
64 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
66 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
67 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
68 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
70 struct digital_atr_req
{
81 struct digital_atr_res
{
93 struct digital_psl_req
{
101 struct digital_psl_res
{
107 struct digital_dep_req_res
{
113 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
114 struct sk_buff
*resp
);
115 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
116 struct sk_buff
*resp
);
118 static const u8 digital_payload_bits_map
[4] = {
125 static u8
digital_payload_bits_to_size(u8 payload_bits
)
127 if (payload_bits
>= ARRAY_SIZE(digital_payload_bits_map
))
130 return digital_payload_bits_map
[payload_bits
];
133 static u8
digital_payload_size_to_bits(u8 payload_size
)
137 for (i
= 0; i
< ARRAY_SIZE(digital_payload_bits_map
); i
++)
138 if (digital_payload_bits_map
[i
] == payload_size
)
144 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
147 skb_push(skb
, sizeof(u8
));
149 skb
->data
[0] = skb
->len
;
151 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
152 *skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
155 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
163 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
164 skb_pull(skb
, sizeof(u8
));
167 if (size
!= skb
->len
)
170 skb_pull(skb
, sizeof(u8
));
175 static struct sk_buff
*
176 digital_send_dep_data_prep(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
,
177 struct digital_dep_req_res
*dep_req_res
,
178 struct digital_data_exch
*data_exch
)
180 struct sk_buff
*new_skb
;
182 if (skb
->len
> ddev
->remote_payload_max
) {
183 dep_req_res
->pfb
|= DIGITAL_NFC_DEP_PFB_MI_BIT
;
185 new_skb
= digital_skb_alloc(ddev
, ddev
->remote_payload_max
);
187 kfree_skb(ddev
->chaining_skb
);
188 ddev
->chaining_skb
= NULL
;
190 return ERR_PTR(-ENOMEM
);
193 skb_reserve(new_skb
, ddev
->tx_headroom
+ NFC_HEADER_SIZE
+
194 DIGITAL_NFC_DEP_REQ_RES_HEADROOM
);
195 memcpy(skb_put(new_skb
, ddev
->remote_payload_max
), skb
->data
,
196 ddev
->remote_payload_max
);
197 skb_pull(skb
, ddev
->remote_payload_max
);
199 ddev
->chaining_skb
= skb
;
200 ddev
->data_exch
= data_exch
;
202 ddev
->chaining_skb
= NULL
;
209 static struct sk_buff
*
210 digital_recv_dep_data_gather(struct nfc_digital_dev
*ddev
, u8 pfb
,
211 struct sk_buff
*resp
,
212 int (*send_ack
)(struct nfc_digital_dev
*ddev
,
213 struct digital_data_exch
215 struct digital_data_exch
*data_exch
)
217 struct sk_buff
*new_skb
;
220 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
) && (!ddev
->chaining_skb
)) {
222 nfc_alloc_recv_skb(8 * ddev
->local_payload_max
,
224 if (!ddev
->chaining_skb
) {
230 if (ddev
->chaining_skb
) {
231 if (resp
->len
> skb_tailroom(ddev
->chaining_skb
)) {
232 new_skb
= skb_copy_expand(ddev
->chaining_skb
,
235 8 * ddev
->local_payload_max
,
242 kfree_skb(ddev
->chaining_skb
);
243 ddev
->chaining_skb
= new_skb
;
246 memcpy(skb_put(ddev
->chaining_skb
, resp
->len
), resp
->data
,
252 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
253 rc
= send_ack(ddev
, data_exch
);
260 resp
= ddev
->chaining_skb
;
261 ddev
->chaining_skb
= NULL
;
269 kfree_skb(ddev
->chaining_skb
);
270 ddev
->chaining_skb
= NULL
;
275 static void digital_in_recv_psl_res(struct nfc_digital_dev
*ddev
, void *arg
,
276 struct sk_buff
*resp
)
278 struct nfc_target
*target
= arg
;
279 struct digital_psl_res
*psl_res
;
288 rc
= ddev
->skb_check_crc(resp
);
290 PROTOCOL_ERR("14.4.1.6");
294 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
296 PROTOCOL_ERR("14.4.1.2");
300 psl_res
= (struct digital_psl_res
*)resp
->data
;
302 if ((resp
->len
!= sizeof(*psl_res
)) ||
303 (psl_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
) ||
304 (psl_res
->cmd
!= DIGITAL_CMD_PSL_RES
)) {
309 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
310 NFC_DIGITAL_RF_TECH_424F
);
314 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
315 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
);
319 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
) &&
320 (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)) {
321 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
322 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
325 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
327 nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
330 ddev
->curr_nfc_dep_pni
= 0;
336 ddev
->curr_protocol
= 0;
339 static int digital_in_send_psl_req(struct nfc_digital_dev
*ddev
,
340 struct nfc_target
*target
)
343 struct digital_psl_req
*psl_req
;
345 u8 payload_size
, payload_bits
;
347 skb
= digital_skb_alloc(ddev
, sizeof(*psl_req
));
351 skb_put(skb
, sizeof(*psl_req
));
353 psl_req
= (struct digital_psl_req
*)skb
->data
;
355 psl_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
356 psl_req
->cmd
= DIGITAL_CMD_PSL_REQ
;
358 psl_req
->brs
= (0x2 << 3) | 0x2; /* 424F both directions */
360 payload_size
= min(ddev
->local_payload_max
, ddev
->remote_payload_max
);
361 payload_bits
= digital_payload_size_to_bits(payload_size
);
362 psl_req
->fsl
= DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits
);
364 ddev
->local_payload_max
= payload_size
;
365 ddev
->remote_payload_max
= payload_size
;
367 digital_skb_push_dep_sod(ddev
, skb
);
369 ddev
->skb_add_crc(skb
);
371 rc
= digital_in_send_cmd(ddev
, skb
, 500, digital_in_recv_psl_res
,
379 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
380 struct sk_buff
*resp
)
382 struct nfc_target
*target
= arg
;
383 struct digital_atr_res
*atr_res
;
384 u8 gb_len
, payload_bits
;
393 rc
= ddev
->skb_check_crc(resp
);
395 PROTOCOL_ERR("14.4.1.6");
399 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
401 PROTOCOL_ERR("14.4.1.2");
405 if (resp
->len
< sizeof(struct digital_atr_res
)) {
410 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
412 atr_res
= (struct digital_atr_res
*)resp
->data
;
414 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_res
->pp
);
415 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
417 if (!ddev
->remote_payload_max
) {
422 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
426 if ((ddev
->protocols
& NFC_PROTO_FELICA_MASK
) &&
427 (ddev
->curr_rf_tech
!= NFC_DIGITAL_RF_TECH_424F
)) {
428 rc
= digital_in_send_psl_req(ddev
, target
);
433 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
436 ddev
->curr_nfc_dep_pni
= 0;
442 ddev
->curr_protocol
= 0;
445 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
446 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
450 struct digital_atr_req
*atr_req
;
455 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
457 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
458 PROTOCOL_ERR("14.6.1.1");
462 skb
= digital_skb_alloc(ddev
, size
);
466 skb_put(skb
, sizeof(struct digital_atr_req
));
468 atr_req
= (struct digital_atr_req
*)skb
->data
;
469 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
471 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
472 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
473 if (target
->nfcid2_len
)
474 memcpy(atr_req
->nfcid3
, target
->nfcid2
, NFC_NFCID2_MAXSIZE
);
476 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
482 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
483 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
484 atr_req
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
487 atr_req
->pp
|= DIGITAL_GB_BIT
;
488 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
491 digital_skb_push_dep_sod(ddev
, skb
);
493 ddev
->skb_add_crc(skb
);
495 rc
= digital_in_send_cmd(ddev
, skb
, 500, digital_in_recv_atr_res
,
503 static int digital_in_send_ack(struct nfc_digital_dev
*ddev
,
504 struct digital_data_exch
*data_exch
)
506 struct digital_dep_req_res
*dep_req
;
510 skb
= digital_skb_alloc(ddev
, 1);
514 skb_push(skb
, sizeof(struct digital_dep_req_res
));
516 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
518 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
519 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
520 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
521 ddev
->curr_nfc_dep_pni
;
523 digital_skb_push_dep_sod(ddev
, skb
);
525 ddev
->skb_add_crc(skb
);
527 ddev
->saved_skb
= skb_get(skb
);
528 ddev
->saved_skb_len
= skb
->len
;
530 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
534 kfree_skb(ddev
->saved_skb
);
535 ddev
->saved_skb
= NULL
;
541 static int digital_in_send_nack(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 DIGITAL_NFC_DEP_PFB_NACK_BIT
| ddev
->curr_nfc_dep_pni
;
561 digital_skb_push_dep_sod(ddev
, skb
);
563 ddev
->skb_add_crc(skb
);
565 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
573 static int digital_in_send_atn(struct nfc_digital_dev
*ddev
,
574 struct digital_data_exch
*data_exch
)
576 struct digital_dep_req_res
*dep_req
;
580 skb
= digital_skb_alloc(ddev
, 1);
584 skb_push(skb
, sizeof(struct digital_dep_req_res
));
586 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
588 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
589 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
590 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
592 digital_skb_push_dep_sod(ddev
, skb
);
594 ddev
->skb_add_crc(skb
);
596 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
604 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
605 struct digital_data_exch
*data_exch
, u8 rtox
)
607 struct digital_dep_req_res
*dep_req
;
611 skb
= digital_skb_alloc(ddev
, 1);
615 *skb_put(skb
, 1) = rtox
;
617 skb_push(skb
, sizeof(struct digital_dep_req_res
));
619 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
621 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
622 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
623 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
624 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
626 digital_skb_push_dep_sod(ddev
, skb
);
628 ddev
->skb_add_crc(skb
);
630 ddev
->saved_skb
= skb_get(skb
);
631 ddev
->saved_skb_len
= skb
->len
;
633 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
637 kfree_skb(ddev
->saved_skb
);
638 ddev
->saved_skb
= NULL
;
644 static int digital_in_send_saved_skb(struct nfc_digital_dev
*ddev
,
645 struct digital_data_exch
*data_exch
)
647 skb_get(ddev
->saved_skb
);
648 skb_push(ddev
->saved_skb
, ddev
->saved_skb_len
);
650 return digital_in_send_cmd(ddev
, ddev
->saved_skb
, 1500,
651 digital_in_recv_dep_res
, data_exch
);
654 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
655 struct sk_buff
*resp
)
657 struct digital_data_exch
*data_exch
= arg
;
658 struct digital_dep_req_res
*dep_res
;
667 if (((rc
!= -ETIMEDOUT
) || ddev
->nack_count
) &&
668 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
671 rc
= digital_in_send_nack(ddev
, data_exch
);
676 } else if ((rc
== -ETIMEDOUT
) &&
677 (ddev
->atn_count
++ < DIGITAL_NFC_DEP_N_RETRY_ATN
)) {
678 ddev
->nack_count
= 0;
680 rc
= digital_in_send_atn(ddev
, data_exch
);
690 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
692 PROTOCOL_ERR("14.4.1.2");
696 rc
= ddev
->skb_check_crc(resp
);
698 if ((resp
->len
>= 4) &&
699 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
702 rc
= digital_in_send_nack(ddev
, data_exch
);
711 PROTOCOL_ERR("14.4.1.6");
716 ddev
->nack_count
= 0;
718 if (resp
->len
> ddev
->local_payload_max
) {
723 size
= sizeof(struct digital_dep_req_res
);
724 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
726 if (resp
->len
< size
|| dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
727 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
734 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
735 PROTOCOL_ERR("14.8.2.1");
740 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
745 if (size
> resp
->len
) {
750 skb_pull(resp
, size
);
752 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
753 case DIGITAL_NFC_DEP_PFB_I_PDU
:
754 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
755 PROTOCOL_ERR("14.12.3.3");
760 ddev
->curr_nfc_dep_pni
=
761 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
763 kfree_skb(ddev
->saved_skb
);
764 ddev
->saved_skb
= NULL
;
766 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
775 /* If resp is NULL then we're still chaining so return and
776 * wait for the next part of the PDU. Else, the PDU is
777 * complete so pass it up.
785 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
786 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
787 PROTOCOL_ERR("14.12.3.3");
792 ddev
->curr_nfc_dep_pni
=
793 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
795 if (ddev
->chaining_skb
&& !DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) {
796 kfree_skb(ddev
->saved_skb
);
797 ddev
->saved_skb
= NULL
;
799 rc
= digital_in_send_dep_req(ddev
, NULL
,
808 pr_err("Received a ACK/NACK PDU\n");
812 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
813 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) { /* ATN */
814 rc
= digital_in_send_saved_skb(ddev
, data_exch
);
816 kfree_skb(ddev
->saved_skb
);
823 kfree_skb(ddev
->saved_skb
);
824 ddev
->saved_skb
= NULL
;
826 rc
= digital_in_send_rtox(ddev
, data_exch
, resp
->data
[0]);
835 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
840 kfree_skb(ddev
->chaining_skb
);
841 ddev
->chaining_skb
= NULL
;
843 kfree_skb(ddev
->saved_skb
);
844 ddev
->saved_skb
= NULL
;
850 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
851 struct nfc_target
*target
, struct sk_buff
*skb
,
852 struct digital_data_exch
*data_exch
)
854 struct digital_dep_req_res
*dep_req
;
855 struct sk_buff
*chaining_skb
, *tmp_skb
;
858 skb_push(skb
, sizeof(struct digital_dep_req_res
));
860 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
862 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
863 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
864 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
867 ddev
->nack_count
= 0;
869 chaining_skb
= ddev
->chaining_skb
;
871 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_req
, data_exch
);
873 return PTR_ERR(tmp_skb
);
875 digital_skb_push_dep_sod(ddev
, tmp_skb
);
877 ddev
->skb_add_crc(tmp_skb
);
879 ddev
->saved_skb
= skb_get(tmp_skb
);
880 ddev
->saved_skb_len
= tmp_skb
->len
;
882 rc
= digital_in_send_cmd(ddev
, tmp_skb
, 1500, digital_in_recv_dep_res
,
888 kfree_skb(chaining_skb
);
889 ddev
->chaining_skb
= NULL
;
891 kfree_skb(ddev
->saved_skb
);
892 ddev
->saved_skb
= NULL
;
898 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
900 ddev
->curr_rf_tech
= rf_tech
;
902 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
903 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
905 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
908 switch (ddev
->curr_rf_tech
) {
909 case NFC_DIGITAL_RF_TECH_106A
:
910 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
911 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
914 case NFC_DIGITAL_RF_TECH_212F
:
915 case NFC_DIGITAL_RF_TECH_424F
:
916 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
917 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
925 static int digital_tg_send_ack(struct nfc_digital_dev
*ddev
,
926 struct digital_data_exch
*data_exch
)
928 struct digital_dep_req_res
*dep_res
;
932 skb
= digital_skb_alloc(ddev
, 1);
936 skb_push(skb
, sizeof(struct digital_dep_req_res
));
938 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
940 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
941 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
942 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
943 ddev
->curr_nfc_dep_pni
;
946 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
948 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
952 ddev
->curr_nfc_dep_pni
=
953 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
955 digital_skb_push_dep_sod(ddev
, skb
);
957 ddev
->skb_add_crc(skb
);
959 ddev
->saved_skb
= skb_get(skb
);
960 ddev
->saved_skb_len
= skb
->len
;
962 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
966 kfree_skb(ddev
->saved_skb
);
967 ddev
->saved_skb
= NULL
;
973 static int digital_tg_send_atn(struct nfc_digital_dev
*ddev
)
975 struct digital_dep_req_res
*dep_res
;
979 skb
= digital_skb_alloc(ddev
, 1);
983 skb_push(skb
, sizeof(struct digital_dep_req_res
));
985 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
987 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
988 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
989 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
992 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
994 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
998 digital_skb_push_dep_sod(ddev
, skb
);
1000 ddev
->skb_add_crc(skb
);
1002 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1010 static int digital_tg_send_saved_skb(struct nfc_digital_dev
*ddev
)
1012 skb_get(ddev
->saved_skb
);
1013 skb_push(ddev
->saved_skb
, ddev
->saved_skb_len
);
1015 return digital_tg_send_cmd(ddev
, ddev
->saved_skb
, 1500,
1016 digital_tg_recv_dep_req
, NULL
);
1019 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
1020 struct sk_buff
*resp
)
1023 struct digital_dep_req_res
*dep_req
;
1033 rc
= ddev
->skb_check_crc(resp
);
1035 PROTOCOL_ERR("14.4.1.6");
1039 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1041 PROTOCOL_ERR("14.4.1.2");
1045 if (resp
->len
> ddev
->local_payload_max
) {
1050 size
= sizeof(struct digital_dep_req_res
);
1051 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
1053 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1054 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
1061 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
1062 if (ddev
->did
&& (ddev
->did
== resp
->data
[3])) {
1068 } else if (ddev
->did
) {
1073 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
1078 if (size
> resp
->len
) {
1083 skb_pull(resp
, size
);
1085 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
1086 case DIGITAL_NFC_DEP_PFB_I_PDU
:
1087 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1089 if ((ddev
->atn_count
&& (DIGITAL_NFC_DEP_PFB_PNI(pfb
- 1) !=
1090 ddev
->curr_nfc_dep_pni
)) ||
1091 (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
)) {
1092 PROTOCOL_ERR("14.12.3.4");
1097 if (ddev
->atn_count
) {
1098 ddev
->atn_count
= 0;
1100 rc
= digital_tg_send_saved_skb(ddev
);
1107 kfree_skb(ddev
->saved_skb
);
1108 ddev
->saved_skb
= NULL
;
1110 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
1111 digital_tg_send_ack
, NULL
);
1118 /* If resp is NULL then we're still chaining so return and
1119 * wait for the next part of the PDU. Else, the PDU is
1120 * complete so pass it up.
1127 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
1128 if (!DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) { /* ACK */
1129 if ((ddev
->atn_count
&&
1130 (DIGITAL_NFC_DEP_PFB_PNI(pfb
- 1) !=
1131 ddev
->curr_nfc_dep_pni
)) ||
1132 (DIGITAL_NFC_DEP_PFB_PNI(pfb
) !=
1133 ddev
->curr_nfc_dep_pni
) ||
1134 !ddev
->chaining_skb
|| !ddev
->saved_skb
) {
1139 if (ddev
->atn_count
) {
1140 ddev
->atn_count
= 0;
1142 rc
= digital_tg_send_saved_skb(ddev
);
1149 kfree_skb(ddev
->saved_skb
);
1150 ddev
->saved_skb
= NULL
;
1152 rc
= digital_tg_send_dep_res(ddev
, ddev
->chaining_skb
);
1156 if ((DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) !=
1157 ddev
->curr_nfc_dep_pni
) ||
1163 ddev
->atn_count
= 0;
1165 rc
= digital_tg_send_saved_skb(ddev
);
1167 kfree_skb(ddev
->saved_skb
);
1173 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
1174 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
1179 rc
= digital_tg_send_atn(ddev
);
1189 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
1192 kfree_skb(ddev
->chaining_skb
);
1193 ddev
->chaining_skb
= NULL
;
1195 ddev
->atn_count
= 0;
1197 kfree_skb(ddev
->saved_skb
);
1198 ddev
->saved_skb
= NULL
;
1204 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
1206 struct digital_dep_req_res
*dep_res
;
1207 struct sk_buff
*chaining_skb
, *tmp_skb
;
1210 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1212 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1214 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1215 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1216 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
1219 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1221 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
1225 ddev
->curr_nfc_dep_pni
=
1226 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1228 chaining_skb
= ddev
->chaining_skb
;
1230 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_res
, NULL
);
1231 if (IS_ERR(tmp_skb
))
1232 return PTR_ERR(tmp_skb
);
1234 digital_skb_push_dep_sod(ddev
, tmp_skb
);
1236 ddev
->skb_add_crc(tmp_skb
);
1238 ddev
->saved_skb
= skb_get(tmp_skb
);
1239 ddev
->saved_skb_len
= tmp_skb
->len
;
1241 rc
= digital_tg_send_cmd(ddev
, tmp_skb
, 1500, digital_tg_recv_dep_req
,
1247 kfree_skb(chaining_skb
);
1248 ddev
->chaining_skb
= NULL
;
1250 kfree_skb(ddev
->saved_skb
);
1251 ddev
->saved_skb
= NULL
;
1257 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
1258 void *arg
, struct sk_buff
*resp
)
1260 u8 rf_tech
= (unsigned long)arg
;
1265 digital_tg_set_rf_tech(ddev
, rf_tech
);
1267 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
1269 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
1271 dev_kfree_skb(resp
);
1274 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
1277 struct digital_psl_res
*psl_res
;
1278 struct sk_buff
*skb
;
1281 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
1285 skb_put(skb
, sizeof(struct digital_psl_res
));
1287 psl_res
= (struct digital_psl_res
*)skb
->data
;
1289 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1290 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
1293 digital_skb_push_dep_sod(ddev
, skb
);
1295 ddev
->skb_add_crc(skb
);
1297 ddev
->curr_nfc_dep_pni
= 0;
1299 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
1300 (void *)(unsigned long)rf_tech
);
1307 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
1308 struct sk_buff
*resp
)
1311 struct digital_psl_req
*psl_req
;
1313 u8 dsi
, payload_size
, payload_bits
;
1321 rc
= ddev
->skb_check_crc(resp
);
1323 PROTOCOL_ERR("14.4.1.6");
1327 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1329 PROTOCOL_ERR("14.4.1.2");
1333 psl_req
= (struct digital_psl_req
*)resp
->data
;
1335 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
1336 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1337 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
1342 dsi
= (psl_req
->brs
>> 3) & 0x07;
1345 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
1348 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
1351 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
1354 pr_err("Unsupported dsi value %d\n", dsi
);
1358 payload_bits
= DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req
->fsl
);
1359 payload_size
= digital_payload_bits_to_size(payload_bits
);
1361 if (!payload_size
|| (payload_size
> min(ddev
->local_payload_max
,
1362 ddev
->remote_payload_max
))) {
1367 ddev
->local_payload_max
= payload_size
;
1368 ddev
->remote_payload_max
= payload_size
;
1370 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
1376 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
1377 void *arg
, struct sk_buff
*resp
)
1382 digital_poll_next_tech(ddev
);
1387 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
1390 ddev
->atn_count
= 0;
1392 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
1393 digital_tg_recv_psl_req(ddev
, arg
, resp
);
1395 digital_tg_recv_dep_req(ddev
, arg
, resp
);
1398 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
1399 struct digital_atr_req
*atr_req
)
1401 struct digital_atr_res
*atr_res
;
1402 struct sk_buff
*skb
;
1403 u8
*gb
, payload_bits
;
1407 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
1411 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
1415 skb_put(skb
, sizeof(struct digital_atr_res
));
1416 atr_res
= (struct digital_atr_res
*)skb
->data
;
1418 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
1420 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1421 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
1422 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
1425 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
1426 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
1427 atr_res
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
1430 skb_put(skb
, gb_len
);
1432 atr_res
->pp
|= DIGITAL_GB_BIT
;
1433 memcpy(atr_res
->gb
, gb
, gb_len
);
1436 digital_skb_push_dep_sod(ddev
, skb
);
1438 ddev
->skb_add_crc(skb
);
1440 ddev
->curr_nfc_dep_pni
= 0;
1442 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
1443 digital_tg_send_atr_res_complete
, NULL
);
1450 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
1451 struct sk_buff
*resp
)
1454 struct digital_atr_req
*atr_req
;
1455 size_t gb_len
, min_size
;
1456 u8 poll_tech_count
, payload_bits
;
1469 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
1470 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
1471 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
1473 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
1474 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
1477 if (resp
->len
< min_size
) {
1482 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
1484 rc
= ddev
->skb_check_crc(resp
);
1486 PROTOCOL_ERR("14.4.1.6");
1490 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1492 PROTOCOL_ERR("14.4.1.2");
1496 atr_req
= (struct digital_atr_req
*)resp
->data
;
1498 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1499 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
||
1500 atr_req
->did
> DIGITAL_DID_MAX
) {
1505 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_req
->pp
);
1506 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
1508 if (!ddev
->remote_payload_max
) {
1513 ddev
->did
= atr_req
->did
;
1515 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1516 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
1520 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
1524 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
1526 poll_tech_count
= ddev
->poll_tech_count
;
1527 ddev
->poll_tech_count
= 0;
1529 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1530 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
1532 ddev
->poll_tech_count
= poll_tech_count
;
1539 digital_poll_next_tech(ddev
);
1541 dev_kfree_skb(resp
);