Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / net / nfc / digital_dep.c
blob5971fb6f51cc76a034bfc4459f2e9fae710fde6a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NFC Digital Protocol stack
4 * Copyright (c) 2013, Intel Corporation.
5 */
7 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
9 #include "digital.h"
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 {
64 u8 dir;
65 u8 cmd;
66 u8 nfcid3[10];
67 u8 did;
68 u8 bs;
69 u8 br;
70 u8 pp;
71 u8 gb[];
72 } __packed;
74 struct digital_atr_res {
75 u8 dir;
76 u8 cmd;
77 u8 nfcid3[10];
78 u8 did;
79 u8 bs;
80 u8 br;
81 u8 to;
82 u8 pp;
83 u8 gb[];
84 } __packed;
86 struct digital_psl_req {
87 u8 dir;
88 u8 cmd;
89 u8 did;
90 u8 brs;
91 u8 fsl;
92 } __packed;
94 struct digital_psl_res {
95 u8 dir;
96 u8 cmd;
97 u8 did;
98 } __packed;
100 struct digital_dep_req_res {
101 u8 dir;
102 u8 cmd;
103 u8 pfb;
104 } __packed;
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] = {
112 [0] = 64,
113 [1] = 128,
114 [2] = 192,
115 [3] = 254
118 /* Response Waiting Time for ATR_RES PDU in ms
120 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
122 * with:
123 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
124 * dRWT(nfcdep) = 16 / f(c) s
125 * dT(nfcdep,initiator) = 100 ms
126 * f(c) = 13560000 Hz
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)
134 * with:
135 * rwt = (256 * 16 / f(c)) * 2^wt s
136 * dRWT(nfcdep) = 16 / f(c) s
137 * dT(nfcdep,initiator) = 100 ms
138 * f(c) = 13560000 Hz
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))
152 return 0;
154 return digital_payload_bits_map[payload_bits];
157 static u8 digital_payload_size_to_bits(u8 payload_size)
159 int i;
161 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
162 if (digital_payload_bits_map[i] == payload_size)
163 return i;
165 return 0xff;
168 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
169 struct sk_buff *skb)
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,
180 struct sk_buff *skb)
182 u8 size;
184 if (skb->len < 2)
185 return -EIO;
187 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
188 skb_pull(skb, sizeof(u8));
190 size = skb->data[0];
191 if (size != skb->len)
192 return -EIO;
194 skb_pull(skb, sizeof(u8));
196 return 0;
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);
210 if (!new_skb) {
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;
222 } else {
223 ddev->chaining_skb = NULL;
224 new_skb = skb;
227 return new_skb;
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
235 *data_exch),
236 struct digital_data_exch *data_exch)
238 struct sk_buff *new_skb;
239 int rc;
241 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
242 ddev->chaining_skb =
243 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
244 GFP_KERNEL);
245 if (!ddev->chaining_skb) {
246 rc = -ENOMEM;
247 goto error;
251 if (ddev->chaining_skb) {
252 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
253 new_skb = skb_copy_expand(ddev->chaining_skb,
254 skb_headroom(
255 ddev->chaining_skb),
256 8 * ddev->local_payload_max,
257 GFP_KERNEL);
258 if (!new_skb) {
259 rc = -ENOMEM;
260 goto error;
263 kfree_skb(ddev->chaining_skb);
264 ddev->chaining_skb = new_skb;
267 skb_put_data(ddev->chaining_skb, resp->data, resp->len);
269 kfree_skb(resp);
270 resp = NULL;
272 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
273 rc = send_ack(ddev, data_exch);
274 if (rc)
275 goto error;
277 return NULL;
280 resp = ddev->chaining_skb;
281 ddev->chaining_skb = NULL;
284 return resp;
286 error:
287 kfree_skb(resp);
289 kfree_skb(ddev->chaining_skb);
290 ddev->chaining_skb = NULL;
292 return ERR_PTR(rc);
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;
300 int rc;
302 if (IS_ERR(resp)) {
303 rc = PTR_ERR(resp);
304 resp = NULL;
305 goto exit;
308 rc = ddev->skb_check_crc(resp);
309 if (rc) {
310 PROTOCOL_ERR("14.4.1.6");
311 goto exit;
314 rc = digital_skb_pull_dep_sod(ddev, resp);
315 if (rc) {
316 PROTOCOL_ERR("14.4.1.2");
317 goto exit;
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)) {
325 rc = -EIO;
326 goto exit;
329 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
330 NFC_DIGITAL_RF_TECH_424F);
331 if (rc)
332 goto exit;
334 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
335 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
336 if (rc)
337 goto exit;
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,
348 NFC_RF_INITIATOR);
350 ddev->curr_nfc_dep_pni = 0;
352 exit:
353 dev_kfree_skb(resp);
355 if (rc)
356 ddev->curr_protocol = 0;
359 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
360 struct nfc_target *target)
362 struct sk_buff *skb;
363 struct digital_psl_req *psl_req;
364 int rc;
365 u8 payload_size, payload_bits;
367 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
368 if (!skb)
369 return -ENOMEM;
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;
377 psl_req->did = 0;
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);
393 if (rc)
394 kfree_skb(skb);
396 return rc;
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;
405 u8 wt;
406 int rc;
408 if (IS_ERR(resp)) {
409 rc = PTR_ERR(resp);
410 resp = NULL;
411 goto exit;
414 rc = ddev->skb_check_crc(resp);
415 if (rc) {
416 PROTOCOL_ERR("14.4.1.6");
417 goto exit;
420 rc = digital_skb_pull_dep_sod(ddev, resp);
421 if (rc) {
422 PROTOCOL_ERR("14.4.1.2");
423 goto exit;
426 if (resp->len < sizeof(struct digital_atr_res)) {
427 rc = -EIO;
428 goto exit;
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) {
444 rc = -EINVAL;
445 goto exit;
448 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
449 if (rc)
450 goto exit;
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);
455 if (!rc)
456 goto exit;
459 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
460 NFC_RF_INITIATOR);
462 ddev->curr_nfc_dep_pni = 0;
464 exit:
465 dev_kfree_skb(resp);
467 if (rc)
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,
473 size_t gb_len)
475 struct sk_buff *skb;
476 struct digital_atr_req *atr_req;
477 uint size;
478 int rc;
479 u8 payload_bits;
481 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
483 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
484 PROTOCOL_ERR("14.6.1.1");
485 return -EINVAL;
488 skb = digital_skb_alloc(ddev, size);
489 if (!skb)
490 return -ENOMEM;
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);
501 else
502 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
504 atr_req->did = 0;
505 atr_req->bs = 0;
506 atr_req->br = 0;
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);
512 if (gb_len) {
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);
523 if (rc)
524 kfree_skb(skb);
526 return rc;
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;
533 struct sk_buff *skb;
534 int rc;
536 skb = digital_skb_alloc(ddev, 1);
537 if (!skb)
538 return -ENOMEM;
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);
557 if (rc) {
558 kfree_skb(skb);
559 kfree_skb(ddev->saved_skb);
560 ddev->saved_skb = NULL;
563 return rc;
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;
570 struct sk_buff *skb;
571 int rc;
573 skb = digital_skb_alloc(ddev, 1);
574 if (!skb)
575 return -ENOMEM;
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);
592 if (rc)
593 kfree_skb(skb);
595 return rc;
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;
602 struct sk_buff *skb;
603 int rc;
605 skb = digital_skb_alloc(ddev, 1);
606 if (!skb)
607 return -ENOMEM;
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);
623 if (rc)
624 kfree_skb(skb);
626 return rc;
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;
633 struct sk_buff *skb;
634 int rc;
635 u16 rwt_int;
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);
642 if (!skb)
643 return -ENOMEM;
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);
662 if (rc)
663 kfree_skb(skb);
665 return rc;
668 static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
669 struct digital_data_exch *data_exch)
671 int rc;
673 if (!ddev->saved_skb)
674 return -EINVAL;
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);
680 if (rc)
681 kfree_skb(ddev->saved_skb);
683 return rc;
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;
691 u8 pfb;
692 uint size;
693 int rc;
694 u8 rtox;
696 if (IS_ERR(resp)) {
697 rc = PTR_ERR(resp);
698 resp = NULL;
700 if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
701 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
702 ddev->atn_count = 0;
704 rc = digital_in_send_nack(ddev, data_exch);
705 if (rc)
706 goto error;
708 return;
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);
714 if (rc)
715 goto error;
717 return;
720 goto exit;
723 rc = digital_skb_pull_dep_sod(ddev, resp);
724 if (rc) {
725 PROTOCOL_ERR("14.4.1.2");
726 goto exit;
729 rc = ddev->skb_check_crc(resp);
730 if (rc) {
731 if ((resp->len >= 4) &&
732 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
733 ddev->atn_count = 0;
735 rc = digital_in_send_nack(ddev, data_exch);
736 if (rc)
737 goto error;
739 kfree_skb(resp);
741 return;
744 PROTOCOL_ERR("14.4.1.6");
745 goto error;
748 ddev->atn_count = 0;
749 ddev->nack_count = 0;
751 if (resp->len > ddev->local_payload_max) {
752 rc = -EMSGSIZE;
753 goto exit;
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) {
761 rc = -EIO;
762 goto error;
765 pfb = dep_res->pfb;
767 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
768 PROTOCOL_ERR("14.8.2.1");
769 rc = -EIO;
770 goto error;
773 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
774 rc = -EIO;
775 goto exit;
778 if (size > resp->len) {
779 rc = -EIO;
780 goto error;
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");
789 rc = -EIO;
790 goto error;
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,
800 digital_in_send_ack,
801 data_exch);
802 if (IS_ERR(resp)) {
803 rc = PTR_ERR(resp);
804 resp = NULL;
805 goto error;
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.
812 if (!resp)
813 return;
815 rc = 0;
816 break;
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");
821 rc = -EIO;
822 goto exit;
825 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
826 PROTOCOL_ERR("14.12.3.3");
827 rc = -EIO;
828 goto exit;
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");
836 rc = -EIO;
837 goto exit;
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,
847 ddev->chaining_skb,
848 ddev->data_exch);
849 if (rc)
850 goto error;
852 goto free_resp;
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);
857 if (rc)
858 goto error;
860 goto free_resp;
863 if (ddev->atn_count || ddev->nack_count) {
864 PROTOCOL_ERR("14.12.4.4");
865 rc = -EIO;
866 goto error;
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");
872 rc = -EIO;
873 goto error;
876 rc = digital_in_send_rtox(ddev, data_exch, rtox);
877 if (rc)
878 goto error;
880 goto free_resp;
883 exit:
884 data_exch->cb(data_exch->cb_context, resp, rc);
886 error:
887 kfree(data_exch);
889 kfree_skb(ddev->chaining_skb);
890 ddev->chaining_skb = NULL;
892 kfree_skb(ddev->saved_skb);
893 ddev->saved_skb = NULL;
895 if (rc)
896 kfree_skb(resp);
898 return;
900 free_resp:
901 dev_kfree_skb(resp);
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;
910 int rc;
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;
920 ddev->atn_count = 0;
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);
926 if (IS_ERR(tmp_skb))
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);
937 if (rc) {
938 if (tmp_skb != skb)
939 kfree_skb(tmp_skb);
941 kfree_skb(chaining_skb);
942 ddev->chaining_skb = NULL;
944 kfree_skb(ddev->saved_skb);
945 ddev->saved_skb = NULL;
948 return rc;
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))
959 return;
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;
965 break;
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;
971 break;
973 default:
974 break;
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;
982 struct sk_buff *skb;
983 int rc;
985 skb = digital_skb_alloc(ddev, 1);
986 if (!skb)
987 return -ENOMEM;
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;
998 if (ddev->did) {
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,
1014 data_exch);
1015 if (rc) {
1016 kfree_skb(skb);
1017 kfree_skb(ddev->saved_skb);
1018 ddev->saved_skb = NULL;
1021 return rc;
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;
1028 int rc;
1030 skb = digital_skb_alloc(ddev, 1);
1031 if (!skb)
1032 return -ENOMEM;
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;
1042 if (ddev->did) {
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,
1053 NULL);
1054 if (rc)
1055 kfree_skb(skb);
1057 return rc;
1060 static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1062 int rc;
1064 if (!ddev->saved_skb)
1065 return -EINVAL;
1067 skb_get(ddev->saved_skb);
1069 rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1070 digital_tg_recv_dep_req, NULL);
1071 if (rc)
1072 kfree_skb(ddev->saved_skb);
1074 return rc;
1077 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1078 struct sk_buff *resp)
1080 int rc;
1081 struct digital_dep_req_res *dep_req;
1082 u8 pfb;
1083 size_t size;
1085 if (IS_ERR(resp)) {
1086 rc = PTR_ERR(resp);
1087 resp = NULL;
1088 goto exit;
1091 rc = ddev->skb_check_crc(resp);
1092 if (rc) {
1093 PROTOCOL_ERR("14.4.1.6");
1094 goto exit;
1097 rc = digital_skb_pull_dep_sod(ddev, resp);
1098 if (rc) {
1099 PROTOCOL_ERR("14.4.1.2");
1100 goto exit;
1103 if (resp->len > ddev->local_payload_max) {
1104 rc = -EMSGSIZE;
1105 goto exit;
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) {
1113 rc = -EIO;
1114 goto exit;
1117 pfb = dep_req->pfb;
1119 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1120 if (ddev->did && (ddev->did == resp->data[3])) {
1121 size++;
1122 } else {
1123 rc = -EIO;
1124 goto exit;
1126 } else if (ddev->did) {
1127 rc = -EIO;
1128 goto exit;
1131 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1132 rc = -EIO;
1133 goto exit;
1136 if (size > resp->len) {
1137 rc = -EIO;
1138 goto exit;
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
1149 * ATN DEP_REQ.
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);
1162 if (rc)
1163 goto exit;
1165 goto free_resp;
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");
1177 rc = -EIO;
1178 goto exit;
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);
1186 if (IS_ERR(resp)) {
1187 rc = PTR_ERR(resp);
1188 resp = NULL;
1189 goto exit;
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.
1196 if (!resp)
1197 return;
1199 rc = 0;
1200 break;
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) {
1205 rc = -EIO;
1206 goto exit;
1209 ddev->atn_count = 0;
1211 rc = digital_tg_send_saved_skb(ddev);
1212 if (rc)
1213 goto exit;
1215 goto free_resp;
1218 /* ACK */
1219 if (ddev->atn_count) {
1220 /* The target has previously recevied one or more ATN
1221 * PDUs.
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);
1232 if (rc)
1233 goto exit;
1235 goto free_resp;
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) {
1248 rc = -EIO;
1249 goto exit;
1252 kfree_skb(ddev->saved_skb);
1253 ddev->saved_skb = NULL;
1255 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1256 if (rc)
1257 goto exit;
1259 goto free_resp;
1260 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1261 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1262 rc = -EINVAL;
1263 goto exit;
1266 rc = digital_tg_send_atn(ddev);
1267 if (rc)
1268 goto exit;
1270 ddev->atn_count++;
1272 goto free_resp;
1275 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1277 exit:
1278 kfree_skb(ddev->chaining_skb);
1279 ddev->chaining_skb = NULL;
1281 ddev->atn_count = 0;
1283 kfree_skb(ddev->saved_skb);
1284 ddev->saved_skb = NULL;
1286 if (rc)
1287 kfree_skb(resp);
1289 return;
1291 free_resp:
1292 dev_kfree_skb(resp);
1295 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1297 struct digital_dep_req_res *dep_res;
1298 struct sk_buff *chaining_skb, *tmp_skb;
1299 int rc;
1301 skb_push(skb, sizeof(struct digital_dep_req_res));
1303 dep_res = (struct digital_dep_req_res *)skb->data;
1305 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1306 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1307 dep_res->pfb = ddev->curr_nfc_dep_pni;
1309 if (ddev->did) {
1310 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1312 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1315 ddev->curr_nfc_dep_pni =
1316 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1318 chaining_skb = ddev->chaining_skb;
1320 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1321 if (IS_ERR(tmp_skb))
1322 return PTR_ERR(tmp_skb);
1324 digital_skb_push_dep_sod(ddev, tmp_skb);
1326 ddev->skb_add_crc(tmp_skb);
1328 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
1330 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1331 NULL);
1332 if (rc) {
1333 if (tmp_skb != skb)
1334 kfree_skb(tmp_skb);
1336 kfree_skb(chaining_skb);
1337 ddev->chaining_skb = NULL;
1339 kfree_skb(ddev->saved_skb);
1340 ddev->saved_skb = NULL;
1343 return rc;
1346 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1347 void *arg, struct sk_buff *resp)
1349 u8 rf_tech = (unsigned long)arg;
1351 if (IS_ERR(resp))
1352 return;
1354 digital_tg_set_rf_tech(ddev, rf_tech);
1356 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1358 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1360 dev_kfree_skb(resp);
1363 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1364 u8 rf_tech)
1366 struct digital_psl_res *psl_res;
1367 struct sk_buff *skb;
1368 int rc;
1370 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1371 if (!skb)
1372 return -ENOMEM;
1374 skb_put(skb, sizeof(struct digital_psl_res));
1376 psl_res = (struct digital_psl_res *)skb->data;
1378 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1379 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1380 psl_res->did = did;
1382 digital_skb_push_dep_sod(ddev, skb);
1384 ddev->skb_add_crc(skb);
1386 ddev->curr_nfc_dep_pni = 0;
1388 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1389 (void *)(unsigned long)rf_tech);
1390 if (rc)
1391 kfree_skb(skb);
1393 return rc;
1396 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1397 struct sk_buff *resp)
1399 int rc;
1400 struct digital_psl_req *psl_req;
1401 u8 rf_tech;
1402 u8 dsi, payload_size, payload_bits;
1404 if (IS_ERR(resp)) {
1405 rc = PTR_ERR(resp);
1406 resp = NULL;
1407 goto exit;
1410 rc = ddev->skb_check_crc(resp);
1411 if (rc) {
1412 PROTOCOL_ERR("14.4.1.6");
1413 goto exit;
1416 rc = digital_skb_pull_dep_sod(ddev, resp);
1417 if (rc) {
1418 PROTOCOL_ERR("14.4.1.2");
1419 goto exit;
1422 psl_req = (struct digital_psl_req *)resp->data;
1424 if (resp->len != sizeof(struct digital_psl_req) ||
1425 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1426 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1427 rc = -EIO;
1428 goto exit;
1431 dsi = (psl_req->brs >> 3) & 0x07;
1432 switch (dsi) {
1433 case 0:
1434 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1435 break;
1436 case 1:
1437 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1438 break;
1439 case 2:
1440 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1441 break;
1442 default:
1443 pr_err("Unsupported dsi value %d\n", dsi);
1444 goto exit;
1447 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1448 payload_size = digital_payload_bits_to_size(payload_bits);
1450 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1451 ddev->remote_payload_max))) {
1452 rc = -EINVAL;
1453 goto exit;
1456 ddev->local_payload_max = payload_size;
1457 ddev->remote_payload_max = payload_size;
1459 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1461 exit:
1462 kfree_skb(resp);
1465 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1466 void *arg, struct sk_buff *resp)
1468 int offset;
1470 if (IS_ERR(resp)) {
1471 digital_poll_next_tech(ddev);
1472 return;
1475 offset = 2;
1476 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1477 offset++;
1479 ddev->atn_count = 0;
1481 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1482 digital_tg_recv_psl_req(ddev, arg, resp);
1483 else
1484 digital_tg_recv_dep_req(ddev, arg, resp);
1487 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1488 struct digital_atr_req *atr_req)
1490 struct digital_atr_res *atr_res;
1491 struct sk_buff *skb;
1492 u8 *gb, payload_bits;
1493 size_t gb_len;
1494 int rc;
1496 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1497 if (!gb)
1498 gb_len = 0;
1500 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1501 if (!skb)
1502 return -ENOMEM;
1504 skb_put(skb, sizeof(struct digital_atr_res));
1505 atr_res = (struct digital_atr_res *)skb->data;
1507 memset(atr_res, 0, sizeof(struct digital_atr_res));
1509 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1510 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1511 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1512 atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
1514 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1515 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1516 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1518 if (gb_len) {
1519 skb_put(skb, gb_len);
1521 atr_res->pp |= DIGITAL_GB_BIT;
1522 memcpy(atr_res->gb, gb, gb_len);
1525 digital_skb_push_dep_sod(ddev, skb);
1527 ddev->skb_add_crc(skb);
1529 ddev->curr_nfc_dep_pni = 0;
1531 rc = digital_tg_send_cmd(ddev, skb, 999,
1532 digital_tg_send_atr_res_complete, NULL);
1533 if (rc)
1534 kfree_skb(skb);
1536 return rc;
1539 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1540 struct sk_buff *resp)
1542 int rc;
1543 struct digital_atr_req *atr_req;
1544 size_t gb_len, min_size;
1545 u8 poll_tech_count, payload_bits;
1547 if (IS_ERR(resp)) {
1548 rc = PTR_ERR(resp);
1549 resp = NULL;
1550 goto exit;
1553 if (!resp->len) {
1554 rc = -EIO;
1555 goto exit;
1558 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1559 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1560 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1561 } else {
1562 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1563 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1566 if (resp->len < min_size) {
1567 rc = -EIO;
1568 goto exit;
1571 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1573 rc = ddev->skb_check_crc(resp);
1574 if (rc) {
1575 PROTOCOL_ERR("14.4.1.6");
1576 goto exit;
1579 rc = digital_skb_pull_dep_sod(ddev, resp);
1580 if (rc) {
1581 PROTOCOL_ERR("14.4.1.2");
1582 goto exit;
1585 atr_req = (struct digital_atr_req *)resp->data;
1587 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1588 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1589 atr_req->did > DIGITAL_DID_MAX) {
1590 rc = -EINVAL;
1591 goto exit;
1594 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1595 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1597 if (!ddev->remote_payload_max) {
1598 rc = -EINVAL;
1599 goto exit;
1602 ddev->did = atr_req->did;
1604 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1605 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1606 if (rc)
1607 goto exit;
1609 rc = digital_tg_send_atr_res(ddev, atr_req);
1610 if (rc)
1611 goto exit;
1613 gb_len = resp->len - sizeof(struct digital_atr_req);
1615 poll_tech_count = ddev->poll_tech_count;
1616 ddev->poll_tech_count = 0;
1618 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1619 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1620 if (rc) {
1621 ddev->poll_tech_count = poll_tech_count;
1622 goto exit;
1625 rc = 0;
1626 exit:
1627 if (rc)
1628 digital_poll_next_tech(ddev);
1630 dev_kfree_skb(resp);