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_PFB_TYPE(pfb) ((pfb) & 0xE0)
43 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
44 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
45 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
46 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
48 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
49 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
50 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
51 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
52 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
53 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
54 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
56 #define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
57 #define DIGITAL_NFC_DEP_RTOX_MAX 59
59 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
60 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
61 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
63 struct digital_atr_req {
74 struct digital_atr_res {
86 struct digital_psl_req {
94 struct digital_psl_res {
100 struct digital_dep_req_res {
106 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
107 struct sk_buff *resp);
108 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
109 struct sk_buff *resp);
111 static const u8 digital_payload_bits_map[4] = {
118 /* Response Waiting Time for ATR_RES PDU in ms
120 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
123 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
124 * dRWT(nfcdep) = 16 / f(c) s
125 * dT(nfcdep,initiator) = 100 ms
128 #define DIGITAL_ATR_RES_RWT 1337
130 /* Response Waiting Time for other DEP PDUs in ms
132 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
135 * rwt = (256 * 16 / f(c)) * 2^wt s
136 * dRWT(nfcdep) = 16 / f(c) s
137 * dT(nfcdep,initiator) = 100 ms
139 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
141 #define DIGITAL_NFC_DEP_IN_MAX_WT 14
142 #define DIGITAL_NFC_DEP_TG_MAX_WT 14
143 static const u16 digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT + 1] = {
144 100, 101, 101, 102, 105,
145 110, 119, 139, 177, 255,
146 409, 719, 1337, 2575, 5049,
149 static u8 digital_payload_bits_to_size(u8 payload_bits)
151 if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
154 return digital_payload_bits_map[payload_bits];
157 static u8 digital_payload_size_to_bits(u8 payload_size)
161 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
162 if (digital_payload_bits_map[i] == payload_size)
168 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
171 skb_push(skb, sizeof(u8));
173 skb->data[0] = skb->len;
175 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
176 *(u8 *)skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
179 static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
187 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
188 skb_pull(skb, sizeof(u8));
191 if (size != skb->len)
194 skb_pull(skb, sizeof(u8));
199 static struct sk_buff *
200 digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
201 struct digital_dep_req_res *dep_req_res,
202 struct digital_data_exch *data_exch)
204 struct sk_buff *new_skb;
206 if (skb->len > ddev->remote_payload_max) {
207 dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
209 new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
211 kfree_skb(ddev->chaining_skb);
212 ddev->chaining_skb = NULL;
214 return ERR_PTR(-ENOMEM);
217 skb_put_data(new_skb, skb->data, ddev->remote_payload_max);
218 skb_pull(skb, ddev->remote_payload_max);
220 ddev->chaining_skb = skb;
221 ddev->data_exch = data_exch;
223 ddev->chaining_skb = NULL;
230 static struct sk_buff *
231 digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
232 struct sk_buff *resp,
233 int (*send_ack)(struct nfc_digital_dev *ddev,
234 struct digital_data_exch
236 struct digital_data_exch *data_exch)
238 struct sk_buff *new_skb;
241 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
243 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
245 if (!ddev->chaining_skb) {
251 if (ddev->chaining_skb) {
252 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
253 new_skb = skb_copy_expand(ddev->chaining_skb,
256 8 * ddev->local_payload_max,
263 kfree_skb(ddev->chaining_skb);
264 ddev->chaining_skb = new_skb;
267 skb_put_data(ddev->chaining_skb, resp->data, resp->len);
272 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
273 rc = send_ack(ddev, data_exch);
280 resp = ddev->chaining_skb;
281 ddev->chaining_skb = NULL;
289 kfree_skb(ddev->chaining_skb);
290 ddev->chaining_skb = NULL;
295 static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
296 struct sk_buff *resp)
298 struct nfc_target *target = arg;
299 struct digital_psl_res *psl_res;
308 rc = ddev->skb_check_crc(resp);
310 PROTOCOL_ERR("14.4.1.6");
314 rc = digital_skb_pull_dep_sod(ddev, resp);
316 PROTOCOL_ERR("14.4.1.2");
320 psl_res = (struct digital_psl_res *)resp->data;
322 if ((resp->len != sizeof(*psl_res)) ||
323 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
324 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
329 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
330 NFC_DIGITAL_RF_TECH_424F);
334 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
335 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
339 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
340 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
341 ddev->skb_add_crc = digital_skb_add_crc_f;
342 ddev->skb_check_crc = digital_skb_check_crc_f;
345 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
347 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
350 ddev->curr_nfc_dep_pni = 0;
356 ddev->curr_protocol = 0;
359 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
360 struct nfc_target *target)
363 struct digital_psl_req *psl_req;
365 u8 payload_size, payload_bits;
367 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
371 skb_put(skb, sizeof(*psl_req));
373 psl_req = (struct digital_psl_req *)skb->data;
375 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
376 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
378 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
380 payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
381 payload_bits = digital_payload_size_to_bits(payload_size);
382 psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
384 ddev->local_payload_max = payload_size;
385 ddev->remote_payload_max = payload_size;
387 digital_skb_push_dep_sod(ddev, skb);
389 ddev->skb_add_crc(skb);
391 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
392 digital_in_recv_psl_res, target);
399 static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
400 struct sk_buff *resp)
402 struct nfc_target *target = arg;
403 struct digital_atr_res *atr_res;
404 u8 gb_len, payload_bits;
414 rc = ddev->skb_check_crc(resp);
416 PROTOCOL_ERR("14.4.1.6");
420 rc = digital_skb_pull_dep_sod(ddev, resp);
422 PROTOCOL_ERR("14.4.1.2");
426 if (resp->len < sizeof(struct digital_atr_res)) {
431 gb_len = resp->len - sizeof(struct digital_atr_res);
433 atr_res = (struct digital_atr_res *)resp->data;
435 wt = DIGITAL_ATR_RES_TO_WT(atr_res->to);
436 if (wt > DIGITAL_NFC_DEP_IN_MAX_WT)
437 wt = DIGITAL_NFC_DEP_IN_MAX_WT;
438 ddev->dep_rwt = digital_rwt_map[wt];
440 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
441 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
443 if (!ddev->remote_payload_max) {
448 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
452 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
453 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
454 rc = digital_in_send_psl_req(ddev, target);
459 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
462 ddev->curr_nfc_dep_pni = 0;
468 ddev->curr_protocol = 0;
471 int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
472 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
476 struct digital_atr_req *atr_req;
481 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
483 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
484 PROTOCOL_ERR("14.6.1.1");
488 skb = digital_skb_alloc(ddev, size);
492 skb_put(skb, sizeof(struct digital_atr_req));
494 atr_req = (struct digital_atr_req *)skb->data;
495 memset(atr_req, 0, sizeof(struct digital_atr_req));
497 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
498 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
499 if (target->nfcid2_len)
500 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
502 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
508 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
509 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
510 atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
513 atr_req->pp |= DIGITAL_GB_BIT;
514 skb_put_data(skb, gb, gb_len);
517 digital_skb_push_dep_sod(ddev, skb);
519 ddev->skb_add_crc(skb);
521 rc = digital_in_send_cmd(ddev, skb, DIGITAL_ATR_RES_RWT,
522 digital_in_recv_atr_res, target);
529 static int digital_in_send_ack(struct nfc_digital_dev *ddev,
530 struct digital_data_exch *data_exch)
532 struct digital_dep_req_res *dep_req;
536 skb = digital_skb_alloc(ddev, 1);
540 skb_push(skb, sizeof(struct digital_dep_req_res));
542 dep_req = (struct digital_dep_req_res *)skb->data;
544 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
545 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
546 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
547 ddev->curr_nfc_dep_pni;
549 digital_skb_push_dep_sod(ddev, skb);
551 ddev->skb_add_crc(skb);
553 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
555 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
556 digital_in_recv_dep_res, data_exch);
559 kfree_skb(ddev->saved_skb);
560 ddev->saved_skb = NULL;
566 static int digital_in_send_nack(struct nfc_digital_dev *ddev,
567 struct digital_data_exch *data_exch)
569 struct digital_dep_req_res *dep_req;
573 skb = digital_skb_alloc(ddev, 1);
577 skb_push(skb, sizeof(struct digital_dep_req_res));
579 dep_req = (struct digital_dep_req_res *)skb->data;
581 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
582 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
583 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
584 DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
586 digital_skb_push_dep_sod(ddev, skb);
588 ddev->skb_add_crc(skb);
590 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
591 digital_in_recv_dep_res, data_exch);
598 static int digital_in_send_atn(struct nfc_digital_dev *ddev,
599 struct digital_data_exch *data_exch)
601 struct digital_dep_req_res *dep_req;
605 skb = digital_skb_alloc(ddev, 1);
609 skb_push(skb, sizeof(struct digital_dep_req_res));
611 dep_req = (struct digital_dep_req_res *)skb->data;
613 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
614 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
615 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
617 digital_skb_push_dep_sod(ddev, skb);
619 ddev->skb_add_crc(skb);
621 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
622 digital_in_recv_dep_res, data_exch);
629 static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
630 struct digital_data_exch *data_exch, u8 rtox)
632 struct digital_dep_req_res *dep_req;
637 rwt_int = ddev->dep_rwt * rtox;
638 if (rwt_int > digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT])
639 rwt_int = digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT];
641 skb = digital_skb_alloc(ddev, 1);
645 skb_put_u8(skb, rtox);
647 skb_push(skb, sizeof(struct digital_dep_req_res));
649 dep_req = (struct digital_dep_req_res *)skb->data;
651 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
652 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
653 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
654 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
656 digital_skb_push_dep_sod(ddev, skb);
658 ddev->skb_add_crc(skb);
660 rc = digital_in_send_cmd(ddev, skb, rwt_int,
661 digital_in_recv_dep_res, data_exch);
668 static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
669 struct digital_data_exch *data_exch)
673 if (!ddev->saved_skb)
676 skb_get(ddev->saved_skb);
678 rc = digital_in_send_cmd(ddev, ddev->saved_skb, ddev->dep_rwt,
679 digital_in_recv_dep_res, data_exch);
681 kfree_skb(ddev->saved_skb);
686 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
687 struct sk_buff *resp)
689 struct digital_data_exch *data_exch = arg;
690 struct digital_dep_req_res *dep_res;
700 if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
701 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
704 rc = digital_in_send_nack(ddev, data_exch);
709 } else if ((rc == -ETIMEDOUT) &&
710 (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
711 ddev->nack_count = 0;
713 rc = digital_in_send_atn(ddev, data_exch);
723 rc = digital_skb_pull_dep_sod(ddev, resp);
725 PROTOCOL_ERR("14.4.1.2");
729 rc = ddev->skb_check_crc(resp);
731 if ((resp->len >= 4) &&
732 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
735 rc = digital_in_send_nack(ddev, data_exch);
744 PROTOCOL_ERR("14.4.1.6");
749 ddev->nack_count = 0;
751 if (resp->len > ddev->local_payload_max) {
756 size = sizeof(struct digital_dep_req_res);
757 dep_res = (struct digital_dep_req_res *)resp->data;
759 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
760 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
767 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
768 PROTOCOL_ERR("14.8.2.1");
773 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
778 if (size > resp->len) {
783 skb_pull(resp, size);
785 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
786 case DIGITAL_NFC_DEP_PFB_I_PDU:
787 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
788 PROTOCOL_ERR("14.12.3.3");
793 ddev->curr_nfc_dep_pni =
794 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
796 kfree_skb(ddev->saved_skb);
797 ddev->saved_skb = NULL;
799 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
808 /* If resp is NULL then we're still chaining so return and
809 * wait for the next part of the PDU. Else, the PDU is
810 * complete so pass it up.
818 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
819 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
820 PROTOCOL_ERR("14.12.4.5");
825 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
826 PROTOCOL_ERR("14.12.3.3");
831 ddev->curr_nfc_dep_pni =
832 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
834 if (!ddev->chaining_skb) {
835 PROTOCOL_ERR("14.12.4.3");
840 /* The initiator has received a valid ACK. Free the last sent
841 * PDU and keep on sending chained skb.
843 kfree_skb(ddev->saved_skb);
844 ddev->saved_skb = NULL;
846 rc = digital_in_send_dep_req(ddev, NULL,
854 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
855 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
856 rc = digital_in_send_saved_skb(ddev, data_exch);
863 if (ddev->atn_count || ddev->nack_count) {
864 PROTOCOL_ERR("14.12.4.4");
869 rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
870 if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
871 PROTOCOL_ERR("14.8.4.1");
876 rc = digital_in_send_rtox(ddev, data_exch, rtox);
884 data_exch->cb(data_exch->cb_context, resp, rc);
889 kfree_skb(ddev->chaining_skb);
890 ddev->chaining_skb = NULL;
892 kfree_skb(ddev->saved_skb);
893 ddev->saved_skb = NULL;
904 int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
905 struct nfc_target *target, struct sk_buff *skb,
906 struct digital_data_exch *data_exch)
908 struct digital_dep_req_res *dep_req;
909 struct sk_buff *chaining_skb, *tmp_skb;
912 skb_push(skb, sizeof(struct digital_dep_req_res));
914 dep_req = (struct digital_dep_req_res *)skb->data;
916 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
917 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
918 dep_req->pfb = ddev->curr_nfc_dep_pni;
921 ddev->nack_count = 0;
923 chaining_skb = ddev->chaining_skb;
925 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
927 return PTR_ERR(tmp_skb);
929 digital_skb_push_dep_sod(ddev, tmp_skb);
931 ddev->skb_add_crc(tmp_skb);
933 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
935 rc = digital_in_send_cmd(ddev, tmp_skb, ddev->dep_rwt,
936 digital_in_recv_dep_res, data_exch);
941 kfree_skb(chaining_skb);
942 ddev->chaining_skb = NULL;
944 kfree_skb(ddev->saved_skb);
945 ddev->saved_skb = NULL;
951 static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
953 ddev->curr_rf_tech = rf_tech;
955 ddev->skb_add_crc = digital_skb_add_crc_none;
956 ddev->skb_check_crc = digital_skb_check_crc_none;
958 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
961 switch (ddev->curr_rf_tech) {
962 case NFC_DIGITAL_RF_TECH_106A:
963 ddev->skb_add_crc = digital_skb_add_crc_a;
964 ddev->skb_check_crc = digital_skb_check_crc_a;
967 case NFC_DIGITAL_RF_TECH_212F:
968 case NFC_DIGITAL_RF_TECH_424F:
969 ddev->skb_add_crc = digital_skb_add_crc_f;
970 ddev->skb_check_crc = digital_skb_check_crc_f;
978 static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
979 struct digital_data_exch *data_exch)
981 struct digital_dep_req_res *dep_res;
985 skb = digital_skb_alloc(ddev, 1);
989 skb_push(skb, sizeof(struct digital_dep_req_res));
991 dep_res = (struct digital_dep_req_res *)skb->data;
993 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
994 dep_res->cmd = DIGITAL_CMD_DEP_RES;
995 dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
996 ddev->curr_nfc_dep_pni;
999 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1001 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1004 ddev->curr_nfc_dep_pni =
1005 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1007 digital_skb_push_dep_sod(ddev, skb);
1009 ddev->skb_add_crc(skb);
1011 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
1013 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1017 kfree_skb(ddev->saved_skb);
1018 ddev->saved_skb = NULL;
1024 static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
1026 struct digital_dep_req_res *dep_res;
1027 struct sk_buff *skb;
1030 skb = digital_skb_alloc(ddev, 1);
1034 skb_push(skb, sizeof(struct digital_dep_req_res));
1036 dep_res = (struct digital_dep_req_res *)skb->data;
1038 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1039 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1040 dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
1043 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1045 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1048 digital_skb_push_dep_sod(ddev, skb);
1050 ddev->skb_add_crc(skb);
1052 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1060 static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1064 if (!ddev->saved_skb)
1067 skb_get(ddev->saved_skb);
1069 rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1070 digital_tg_recv_dep_req, NULL);
1072 kfree_skb(ddev->saved_skb);
1077 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1078 struct sk_buff *resp)
1081 struct digital_dep_req_res *dep_req;
1091 rc = ddev->skb_check_crc(resp);
1093 PROTOCOL_ERR("14.4.1.6");
1097 rc = digital_skb_pull_dep_sod(ddev, resp);
1099 PROTOCOL_ERR("14.4.1.2");
1103 if (resp->len > ddev->local_payload_max) {
1108 size = sizeof(struct digital_dep_req_res);
1109 dep_req = (struct digital_dep_req_res *)resp->data;
1111 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1112 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1119 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1120 if (ddev->did && (ddev->did == resp->data[3])) {
1126 } else if (ddev->did) {
1131 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1136 if (size > resp->len) {
1141 skb_pull(resp, size);
1143 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
1144 case DIGITAL_NFC_DEP_PFB_I_PDU:
1145 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1147 if (ddev->atn_count) {
1148 /* The target has received (and replied to) at least one
1151 ddev->atn_count = 0;
1153 /* pni of resp PDU equal to the target current pni - 1
1154 * means resp is the previous DEP_REQ PDU received from
1155 * the initiator so the target replies with saved_skb
1156 * which is the previous DEP_RES saved in
1157 * digital_tg_send_dep_res().
1159 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) ==
1160 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni - 1)) {
1161 rc = digital_tg_send_saved_skb(ddev);
1168 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1169 * means the target probably did not received the last
1170 * DEP_REQ PDU sent by the initiator. The target
1171 * fallbacks to normal processing then.
1175 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
1176 PROTOCOL_ERR("14.12.3.4");
1181 kfree_skb(ddev->saved_skb);
1182 ddev->saved_skb = NULL;
1184 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1185 digital_tg_send_ack, NULL);
1192 /* If resp is NULL then we're still chaining so return and
1193 * wait for the next part of the PDU. Else, the PDU is
1194 * complete so pass it up.
1201 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
1202 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* NACK */
1203 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1204 ddev->curr_nfc_dep_pni) {
1209 ddev->atn_count = 0;
1211 rc = digital_tg_send_saved_skb(ddev);
1219 if (ddev->atn_count) {
1220 /* The target has previously received one or more ATN
1223 ddev->atn_count = 0;
1225 /* If the ACK PNI is equal to the target PNI - 1 means
1226 * that the initiator did not receive the previous PDU
1227 * sent by the target so re-send it.
1229 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) ==
1230 ddev->curr_nfc_dep_pni) {
1231 rc = digital_tg_send_saved_skb(ddev);
1238 /* Otherwise, the target did not receive the previous
1239 * ACK PDU from the initiator. Fallback to normal
1240 * processing of chained PDU then.
1244 /* Keep on sending chained PDU */
1245 if (!ddev->chaining_skb ||
1246 DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1247 ddev->curr_nfc_dep_pni) {
1252 kfree_skb(ddev->saved_skb);
1253 ddev->saved_skb = NULL;
1255 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1260 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1261 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1266 rc = digital_tg_send_atn(ddev);
1275 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1280 kfree_skb(ddev->chaining_skb);
1281 ddev->chaining_skb = NULL;
1283 ddev->atn_count = 0;
1285 kfree_skb(ddev->saved_skb);
1286 ddev->saved_skb = NULL;
1294 dev_kfree_skb(resp);
1297 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1299 struct digital_dep_req_res *dep_res;
1300 struct sk_buff *chaining_skb, *tmp_skb;
1303 skb_push(skb, sizeof(struct digital_dep_req_res));
1305 dep_res = (struct digital_dep_req_res *)skb->data;
1307 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1308 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1309 dep_res->pfb = ddev->curr_nfc_dep_pni;
1312 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1314 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1317 ddev->curr_nfc_dep_pni =
1318 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1320 chaining_skb = ddev->chaining_skb;
1322 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1323 if (IS_ERR(tmp_skb))
1324 return PTR_ERR(tmp_skb);
1326 digital_skb_push_dep_sod(ddev, tmp_skb);
1328 ddev->skb_add_crc(tmp_skb);
1330 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
1332 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1338 kfree_skb(chaining_skb);
1339 ddev->chaining_skb = NULL;
1341 kfree_skb(ddev->saved_skb);
1342 ddev->saved_skb = NULL;
1348 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1349 void *arg, struct sk_buff *resp)
1351 u8 rf_tech = (unsigned long)arg;
1356 digital_tg_set_rf_tech(ddev, rf_tech);
1358 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1360 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1362 dev_kfree_skb(resp);
1365 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1368 struct digital_psl_res *psl_res;
1369 struct sk_buff *skb;
1372 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1376 skb_put(skb, sizeof(struct digital_psl_res));
1378 psl_res = (struct digital_psl_res *)skb->data;
1380 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1381 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1384 digital_skb_push_dep_sod(ddev, skb);
1386 ddev->skb_add_crc(skb);
1388 ddev->curr_nfc_dep_pni = 0;
1390 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1391 (void *)(unsigned long)rf_tech);
1398 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1399 struct sk_buff *resp)
1402 struct digital_psl_req *psl_req;
1404 u8 dsi, payload_size, payload_bits;
1412 rc = ddev->skb_check_crc(resp);
1414 PROTOCOL_ERR("14.4.1.6");
1418 rc = digital_skb_pull_dep_sod(ddev, resp);
1420 PROTOCOL_ERR("14.4.1.2");
1424 psl_req = (struct digital_psl_req *)resp->data;
1426 if (resp->len != sizeof(struct digital_psl_req) ||
1427 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1428 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1433 dsi = (psl_req->brs >> 3) & 0x07;
1436 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1439 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1442 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1445 pr_err("Unsupported dsi value %d\n", dsi);
1449 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1450 payload_size = digital_payload_bits_to_size(payload_bits);
1452 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1453 ddev->remote_payload_max))) {
1458 ddev->local_payload_max = payload_size;
1459 ddev->remote_payload_max = payload_size;
1461 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1467 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1468 void *arg, struct sk_buff *resp)
1473 digital_poll_next_tech(ddev);
1478 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1481 ddev->atn_count = 0;
1483 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1484 digital_tg_recv_psl_req(ddev, arg, resp);
1486 digital_tg_recv_dep_req(ddev, arg, resp);
1489 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1490 struct digital_atr_req *atr_req)
1492 struct digital_atr_res *atr_res;
1493 struct sk_buff *skb;
1494 u8 *gb, payload_bits;
1498 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1502 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1506 skb_put(skb, sizeof(struct digital_atr_res));
1507 atr_res = (struct digital_atr_res *)skb->data;
1509 memset(atr_res, 0, sizeof(struct digital_atr_res));
1511 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1512 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1513 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1514 atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
1516 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1517 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1518 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1521 skb_put(skb, gb_len);
1523 atr_res->pp |= DIGITAL_GB_BIT;
1524 memcpy(atr_res->gb, gb, gb_len);
1527 digital_skb_push_dep_sod(ddev, skb);
1529 ddev->skb_add_crc(skb);
1531 ddev->curr_nfc_dep_pni = 0;
1533 rc = digital_tg_send_cmd(ddev, skb, 999,
1534 digital_tg_send_atr_res_complete, NULL);
1541 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1542 struct sk_buff *resp)
1545 struct digital_atr_req *atr_req;
1546 size_t gb_len, min_size;
1547 u8 poll_tech_count, payload_bits;
1560 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1561 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1562 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1564 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1565 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1568 if (resp->len < min_size) {
1573 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1575 rc = ddev->skb_check_crc(resp);
1577 PROTOCOL_ERR("14.4.1.6");
1581 rc = digital_skb_pull_dep_sod(ddev, resp);
1583 PROTOCOL_ERR("14.4.1.2");
1587 atr_req = (struct digital_atr_req *)resp->data;
1589 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1590 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1591 atr_req->did > DIGITAL_DID_MAX) {
1596 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1597 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1599 if (!ddev->remote_payload_max) {
1604 ddev->did = atr_req->did;
1606 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1607 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1611 rc = digital_tg_send_atr_res(ddev, atr_req);
1615 gb_len = resp->len - sizeof(struct digital_atr_req);
1617 poll_tech_count = ddev->poll_tech_count;
1618 ddev->poll_tech_count = 0;
1620 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1621 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1623 ddev->poll_tech_count = poll_tech_count;
1630 digital_poll_next_tech(ddev);
1632 dev_kfree_skb(resp);