Revert "userfaultfd: selftest: vm: allow to build in vm/ directory"
[linux/fpc-iii.git] / net / nfc / digital_dep.c
blobf72be7433df35fc1acfa3105a151b40876c0d47f
1 /*
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
12 * more details.
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
18 #include "digital.h"
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 {
71 u8 dir;
72 u8 cmd;
73 u8 nfcid3[10];
74 u8 did;
75 u8 bs;
76 u8 br;
77 u8 pp;
78 u8 gb[0];
79 } __packed;
81 struct digital_atr_res {
82 u8 dir;
83 u8 cmd;
84 u8 nfcid3[10];
85 u8 did;
86 u8 bs;
87 u8 br;
88 u8 to;
89 u8 pp;
90 u8 gb[0];
91 } __packed;
93 struct digital_psl_req {
94 u8 dir;
95 u8 cmd;
96 u8 did;
97 u8 brs;
98 u8 fsl;
99 } __packed;
101 struct digital_psl_res {
102 u8 dir;
103 u8 cmd;
104 u8 did;
105 } __packed;
107 struct digital_dep_req_res {
108 u8 dir;
109 u8 cmd;
110 u8 pfb;
111 } __packed;
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] = {
119 [0] = 64,
120 [1] = 128,
121 [2] = 192,
122 [3] = 254
125 static u8 digital_payload_bits_to_size(u8 payload_bits)
127 if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
128 return 0;
130 return digital_payload_bits_map[payload_bits];
133 static u8 digital_payload_size_to_bits(u8 payload_size)
135 int i;
137 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
138 if (digital_payload_bits_map[i] == payload_size)
139 return i;
141 return 0xff;
144 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
145 struct sk_buff *skb)
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,
156 struct sk_buff *skb)
158 u8 size;
160 if (skb->len < 2)
161 return -EIO;
163 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
164 skb_pull(skb, sizeof(u8));
166 size = skb->data[0];
167 if (size != skb->len)
168 return -EIO;
170 skb_pull(skb, sizeof(u8));
172 return 0;
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);
186 if (!new_skb) {
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;
201 } else {
202 ddev->chaining_skb = NULL;
203 new_skb = skb;
206 return new_skb;
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
214 *data_exch),
215 struct digital_data_exch *data_exch)
217 struct sk_buff *new_skb;
218 int rc;
220 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
221 ddev->chaining_skb =
222 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
223 GFP_KERNEL);
224 if (!ddev->chaining_skb) {
225 rc = -ENOMEM;
226 goto error;
230 if (ddev->chaining_skb) {
231 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
232 new_skb = skb_copy_expand(ddev->chaining_skb,
233 skb_headroom(
234 ddev->chaining_skb),
235 8 * ddev->local_payload_max,
236 GFP_KERNEL);
237 if (!new_skb) {
238 rc = -ENOMEM;
239 goto error;
242 kfree_skb(ddev->chaining_skb);
243 ddev->chaining_skb = new_skb;
246 memcpy(skb_put(ddev->chaining_skb, resp->len), resp->data,
247 resp->len);
249 kfree_skb(resp);
250 resp = NULL;
252 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
253 rc = send_ack(ddev, data_exch);
254 if (rc)
255 goto error;
257 return NULL;
260 resp = ddev->chaining_skb;
261 ddev->chaining_skb = NULL;
264 return resp;
266 error:
267 kfree_skb(resp);
269 kfree_skb(ddev->chaining_skb);
270 ddev->chaining_skb = NULL;
272 return ERR_PTR(rc);
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;
280 int rc;
282 if (IS_ERR(resp)) {
283 rc = PTR_ERR(resp);
284 resp = NULL;
285 goto exit;
288 rc = ddev->skb_check_crc(resp);
289 if (rc) {
290 PROTOCOL_ERR("14.4.1.6");
291 goto exit;
294 rc = digital_skb_pull_dep_sod(ddev, resp);
295 if (rc) {
296 PROTOCOL_ERR("14.4.1.2");
297 goto exit;
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)) {
305 rc = -EIO;
306 goto exit;
309 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
310 NFC_DIGITAL_RF_TECH_424F);
311 if (rc)
312 goto exit;
314 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
315 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
316 if (rc)
317 goto exit;
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,
328 NFC_RF_INITIATOR);
330 ddev->curr_nfc_dep_pni = 0;
332 exit:
333 dev_kfree_skb(resp);
335 if (rc)
336 ddev->curr_protocol = 0;
339 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
340 struct nfc_target *target)
342 struct sk_buff *skb;
343 struct digital_psl_req *psl_req;
344 int rc;
345 u8 payload_size, payload_bits;
347 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
348 if (!skb)
349 return -ENOMEM;
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;
357 psl_req->did = 0;
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,
372 target);
373 if (rc)
374 kfree_skb(skb);
376 return rc;
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;
385 int rc;
387 if (IS_ERR(resp)) {
388 rc = PTR_ERR(resp);
389 resp = NULL;
390 goto exit;
393 rc = ddev->skb_check_crc(resp);
394 if (rc) {
395 PROTOCOL_ERR("14.4.1.6");
396 goto exit;
399 rc = digital_skb_pull_dep_sod(ddev, resp);
400 if (rc) {
401 PROTOCOL_ERR("14.4.1.2");
402 goto exit;
405 if (resp->len < sizeof(struct digital_atr_res)) {
406 rc = -EIO;
407 goto exit;
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) {
418 rc = -EINVAL;
419 goto exit;
422 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
423 if (rc)
424 goto exit;
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);
429 if (!rc)
430 goto exit;
433 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
434 NFC_RF_INITIATOR);
436 ddev->curr_nfc_dep_pni = 0;
438 exit:
439 dev_kfree_skb(resp);
441 if (rc)
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,
447 size_t gb_len)
449 struct sk_buff *skb;
450 struct digital_atr_req *atr_req;
451 uint size;
452 int rc;
453 u8 payload_bits;
455 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
457 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
458 PROTOCOL_ERR("14.6.1.1");
459 return -EINVAL;
462 skb = digital_skb_alloc(ddev, size);
463 if (!skb)
464 return -ENOMEM;
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);
475 else
476 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
478 atr_req->did = 0;
479 atr_req->bs = 0;
480 atr_req->br = 0;
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);
486 if (gb_len) {
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,
496 target);
497 if (rc)
498 kfree_skb(skb);
500 return rc;
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;
507 struct sk_buff *skb;
508 int rc;
510 skb = digital_skb_alloc(ddev, 1);
511 if (!skb)
512 return -ENOMEM;
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,
531 data_exch);
532 if (rc) {
533 kfree_skb(skb);
534 kfree_skb(ddev->saved_skb);
535 ddev->saved_skb = NULL;
538 return rc;
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;
545 struct sk_buff *skb;
546 int rc;
548 skb = digital_skb_alloc(ddev, 1);
549 if (!skb)
550 return -ENOMEM;
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,
566 data_exch);
567 if (rc)
568 kfree_skb(skb);
570 return rc;
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;
577 struct sk_buff *skb;
578 int rc;
580 skb = digital_skb_alloc(ddev, 1);
581 if (!skb)
582 return -ENOMEM;
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,
597 data_exch);
598 if (rc)
599 kfree_skb(skb);
601 return rc;
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;
608 struct sk_buff *skb;
609 int rc;
611 skb = digital_skb_alloc(ddev, 1);
612 if (!skb)
613 return -ENOMEM;
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,
634 data_exch);
635 if (rc) {
636 kfree_skb(skb);
637 kfree_skb(ddev->saved_skb);
638 ddev->saved_skb = NULL;
641 return rc;
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;
659 u8 pfb;
660 uint size;
661 int rc;
663 if (IS_ERR(resp)) {
664 rc = PTR_ERR(resp);
665 resp = NULL;
667 if (((rc != -ETIMEDOUT) || ddev->nack_count) &&
668 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
669 ddev->atn_count = 0;
671 rc = digital_in_send_nack(ddev, data_exch);
672 if (rc)
673 goto error;
675 return;
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);
681 if (rc)
682 goto error;
684 return;
687 goto exit;
690 rc = digital_skb_pull_dep_sod(ddev, resp);
691 if (rc) {
692 PROTOCOL_ERR("14.4.1.2");
693 goto exit;
696 rc = ddev->skb_check_crc(resp);
697 if (rc) {
698 if ((resp->len >= 4) &&
699 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
700 ddev->atn_count = 0;
702 rc = digital_in_send_nack(ddev, data_exch);
703 if (rc)
704 goto error;
706 kfree_skb(resp);
708 return;
711 PROTOCOL_ERR("14.4.1.6");
712 goto error;
715 ddev->atn_count = 0;
716 ddev->nack_count = 0;
718 if (resp->len > ddev->local_payload_max) {
719 rc = -EMSGSIZE;
720 goto exit;
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) {
728 rc = -EIO;
729 goto error;
732 pfb = dep_res->pfb;
734 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
735 PROTOCOL_ERR("14.8.2.1");
736 rc = -EIO;
737 goto error;
740 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
741 rc = -EIO;
742 goto exit;
745 if (size > resp->len) {
746 rc = -EIO;
747 goto error;
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");
756 rc = -EIO;
757 goto error;
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,
767 digital_in_send_ack,
768 data_exch);
769 if (IS_ERR(resp)) {
770 rc = PTR_ERR(resp);
771 resp = NULL;
772 goto error;
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.
779 if (!resp)
780 return;
782 rc = 0;
783 break;
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");
788 rc = -EIO;
789 goto exit;
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,
800 ddev->chaining_skb,
801 ddev->data_exch);
802 if (rc)
803 goto error;
805 return;
808 pr_err("Received a ACK/NACK PDU\n");
809 rc = -EINVAL;
810 goto exit;
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);
815 if (rc) {
816 kfree_skb(ddev->saved_skb);
817 goto error;
820 return;
823 kfree_skb(ddev->saved_skb);
824 ddev->saved_skb = NULL;
826 rc = digital_in_send_rtox(ddev, data_exch, resp->data[0]);
827 if (rc)
828 goto error;
830 kfree_skb(resp);
831 return;
834 exit:
835 data_exch->cb(data_exch->cb_context, resp, rc);
837 error:
838 kfree(data_exch);
840 kfree_skb(ddev->chaining_skb);
841 ddev->chaining_skb = NULL;
843 kfree_skb(ddev->saved_skb);
844 ddev->saved_skb = NULL;
846 if (rc)
847 kfree_skb(resp);
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;
856 int rc;
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;
866 ddev->atn_count = 0;
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);
872 if (IS_ERR(tmp_skb))
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,
883 data_exch);
884 if (rc) {
885 if (tmp_skb != skb)
886 kfree_skb(tmp_skb);
888 kfree_skb(chaining_skb);
889 ddev->chaining_skb = NULL;
891 kfree_skb(ddev->saved_skb);
892 ddev->saved_skb = NULL;
895 return rc;
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))
906 return;
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;
912 break;
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;
918 break;
920 default:
921 break;
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;
929 struct sk_buff *skb;
930 int rc;
932 skb = digital_skb_alloc(ddev, 1);
933 if (!skb)
934 return -ENOMEM;
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;
945 if (ddev->did) {
946 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
948 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
949 sizeof(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,
963 data_exch);
964 if (rc) {
965 kfree_skb(skb);
966 kfree_skb(ddev->saved_skb);
967 ddev->saved_skb = NULL;
970 return rc;
973 static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
975 struct digital_dep_req_res *dep_res;
976 struct sk_buff *skb;
977 int rc;
979 skb = digital_skb_alloc(ddev, 1);
980 if (!skb)
981 return -ENOMEM;
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;
991 if (ddev->did) {
992 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
994 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
995 sizeof(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,
1003 NULL);
1004 if (rc)
1005 kfree_skb(skb);
1007 return rc;
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)
1022 int rc;
1023 struct digital_dep_req_res *dep_req;
1024 u8 pfb;
1025 size_t size;
1027 if (IS_ERR(resp)) {
1028 rc = PTR_ERR(resp);
1029 resp = NULL;
1030 goto exit;
1033 rc = ddev->skb_check_crc(resp);
1034 if (rc) {
1035 PROTOCOL_ERR("14.4.1.6");
1036 goto exit;
1039 rc = digital_skb_pull_dep_sod(ddev, resp);
1040 if (rc) {
1041 PROTOCOL_ERR("14.4.1.2");
1042 goto exit;
1045 if (resp->len > ddev->local_payload_max) {
1046 rc = -EMSGSIZE;
1047 goto exit;
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) {
1055 rc = -EIO;
1056 goto exit;
1059 pfb = dep_req->pfb;
1061 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1062 if (ddev->did && (ddev->did == resp->data[3])) {
1063 size++;
1064 } else {
1065 rc = -EIO;
1066 goto exit;
1068 } else if (ddev->did) {
1069 rc = -EIO;
1070 goto exit;
1073 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1074 rc = -EIO;
1075 goto exit;
1078 if (size > resp->len) {
1079 rc = -EIO;
1080 goto exit;
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");
1093 rc = -EIO;
1094 goto exit;
1097 if (ddev->atn_count) {
1098 ddev->atn_count = 0;
1100 rc = digital_tg_send_saved_skb(ddev);
1101 if (rc)
1102 goto exit;
1104 return;
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);
1112 if (IS_ERR(resp)) {
1113 rc = PTR_ERR(resp);
1114 resp = NULL;
1115 goto exit;
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.
1122 if (!resp)
1123 return;
1125 rc = 0;
1126 break;
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) {
1135 rc = -EIO;
1136 goto exit;
1139 if (ddev->atn_count) {
1140 ddev->atn_count = 0;
1142 rc = digital_tg_send_saved_skb(ddev);
1143 if (rc)
1144 goto exit;
1146 return;
1149 kfree_skb(ddev->saved_skb);
1150 ddev->saved_skb = NULL;
1152 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1153 if (rc)
1154 goto exit;
1155 } else { /* NACK */
1156 if ((DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1157 ddev->curr_nfc_dep_pni) ||
1158 !ddev->saved_skb) {
1159 rc = -EIO;
1160 goto exit;
1163 ddev->atn_count = 0;
1165 rc = digital_tg_send_saved_skb(ddev);
1166 if (rc) {
1167 kfree_skb(ddev->saved_skb);
1168 goto exit;
1172 return;
1173 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1174 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1175 rc = -EINVAL;
1176 goto exit;
1179 rc = digital_tg_send_atn(ddev);
1180 if (rc)
1181 goto exit;
1183 ddev->atn_count++;
1185 kfree_skb(resp);
1186 return;
1189 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1191 exit:
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;
1200 if (rc)
1201 kfree_skb(resp);
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;
1208 int rc;
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;
1218 if (ddev->did) {
1219 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1221 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1222 sizeof(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,
1242 NULL);
1243 if (rc) {
1244 if (tmp_skb != skb)
1245 kfree_skb(tmp_skb);
1247 kfree_skb(chaining_skb);
1248 ddev->chaining_skb = NULL;
1250 kfree_skb(ddev->saved_skb);
1251 ddev->saved_skb = NULL;
1254 return rc;
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;
1262 if (IS_ERR(resp))
1263 return;
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,
1275 u8 rf_tech)
1277 struct digital_psl_res *psl_res;
1278 struct sk_buff *skb;
1279 int rc;
1281 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1282 if (!skb)
1283 return -ENOMEM;
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;
1291 psl_res->did = did;
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);
1301 if (rc)
1302 kfree_skb(skb);
1304 return rc;
1307 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1308 struct sk_buff *resp)
1310 int rc;
1311 struct digital_psl_req *psl_req;
1312 u8 rf_tech;
1313 u8 dsi, payload_size, payload_bits;
1315 if (IS_ERR(resp)) {
1316 rc = PTR_ERR(resp);
1317 resp = NULL;
1318 goto exit;
1321 rc = ddev->skb_check_crc(resp);
1322 if (rc) {
1323 PROTOCOL_ERR("14.4.1.6");
1324 goto exit;
1327 rc = digital_skb_pull_dep_sod(ddev, resp);
1328 if (rc) {
1329 PROTOCOL_ERR("14.4.1.2");
1330 goto exit;
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) {
1338 rc = -EIO;
1339 goto exit;
1342 dsi = (psl_req->brs >> 3) & 0x07;
1343 switch (dsi) {
1344 case 0:
1345 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1346 break;
1347 case 1:
1348 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1349 break;
1350 case 2:
1351 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1352 break;
1353 default:
1354 pr_err("Unsupported dsi value %d\n", dsi);
1355 goto exit;
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))) {
1363 rc = -EINVAL;
1364 goto exit;
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);
1372 exit:
1373 kfree_skb(resp);
1376 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1377 void *arg, struct sk_buff *resp)
1379 int offset;
1381 if (IS_ERR(resp)) {
1382 digital_poll_next_tech(ddev);
1383 return;
1386 offset = 2;
1387 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1388 offset++;
1390 ddev->atn_count = 0;
1392 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1393 digital_tg_recv_psl_req(ddev, arg, resp);
1394 else
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;
1404 size_t gb_len;
1405 int rc;
1407 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1408 if (!gb)
1409 gb_len = 0;
1411 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1412 if (!skb)
1413 return -ENOMEM;
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));
1423 atr_res->to = 8;
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);
1429 if (gb_len) {
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);
1444 if (rc)
1445 kfree_skb(skb);
1447 return rc;
1450 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1451 struct sk_buff *resp)
1453 int rc;
1454 struct digital_atr_req *atr_req;
1455 size_t gb_len, min_size;
1456 u8 poll_tech_count, payload_bits;
1458 if (IS_ERR(resp)) {
1459 rc = PTR_ERR(resp);
1460 resp = NULL;
1461 goto exit;
1464 if (!resp->len) {
1465 rc = -EIO;
1466 goto exit;
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);
1472 } else {
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) {
1478 rc = -EIO;
1479 goto exit;
1482 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1484 rc = ddev->skb_check_crc(resp);
1485 if (rc) {
1486 PROTOCOL_ERR("14.4.1.6");
1487 goto exit;
1490 rc = digital_skb_pull_dep_sod(ddev, resp);
1491 if (rc) {
1492 PROTOCOL_ERR("14.4.1.2");
1493 goto exit;
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) {
1501 rc = -EINVAL;
1502 goto exit;
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) {
1509 rc = -EINVAL;
1510 goto exit;
1513 ddev->did = atr_req->did;
1515 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1516 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1517 if (rc)
1518 goto exit;
1520 rc = digital_tg_send_atr_res(ddev, atr_req);
1521 if (rc)
1522 goto exit;
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);
1531 if (rc) {
1532 ddev->poll_tech_count = poll_tech_count;
1533 goto exit;
1536 rc = 0;
1537 exit:
1538 if (rc)
1539 digital_poll_next_tech(ddev);
1541 dev_kfree_skb(resp);