perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / net / nfc / digital_dep.c
blob4f9a973988b2b13fc5426525b487a23cbabb84f3
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_ATR_RES_TO_WT(s) ((s) & 0xF)
40 #define DIGITAL_DID_MAX 14
42 #define DIGITAL_PAYLOAD_SIZE_MAX 254
43 #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
44 #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
45 #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
46 #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
48 #define DIGITAL_GB_BIT 0x02
50 #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
51 #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
53 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
55 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
56 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
57 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
58 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
60 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
61 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
62 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
63 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
64 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
65 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
66 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
68 #define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
69 #define DIGITAL_NFC_DEP_RTOX_MAX 59
71 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
72 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
73 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
75 struct digital_atr_req {
76 u8 dir;
77 u8 cmd;
78 u8 nfcid3[10];
79 u8 did;
80 u8 bs;
81 u8 br;
82 u8 pp;
83 u8 gb[0];
84 } __packed;
86 struct digital_atr_res {
87 u8 dir;
88 u8 cmd;
89 u8 nfcid3[10];
90 u8 did;
91 u8 bs;
92 u8 br;
93 u8 to;
94 u8 pp;
95 u8 gb[0];
96 } __packed;
98 struct digital_psl_req {
99 u8 dir;
100 u8 cmd;
101 u8 did;
102 u8 brs;
103 u8 fsl;
104 } __packed;
106 struct digital_psl_res {
107 u8 dir;
108 u8 cmd;
109 u8 did;
110 } __packed;
112 struct digital_dep_req_res {
113 u8 dir;
114 u8 cmd;
115 u8 pfb;
116 } __packed;
118 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
119 struct sk_buff *resp);
120 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
121 struct sk_buff *resp);
123 static const u8 digital_payload_bits_map[4] = {
124 [0] = 64,
125 [1] = 128,
126 [2] = 192,
127 [3] = 254
130 /* Response Waiting Time for ATR_RES PDU in ms
132 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
134 * with:
135 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
136 * dRWT(nfcdep) = 16 / f(c) s
137 * dT(nfcdep,initiator) = 100 ms
138 * f(c) = 13560000 Hz
140 #define DIGITAL_ATR_RES_RWT 1337
142 /* Response Waiting Time for other DEP PDUs in ms
144 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
146 * with:
147 * rwt = (256 * 16 / f(c)) * 2^wt s
148 * dRWT(nfcdep) = 16 / f(c) s
149 * dT(nfcdep,initiator) = 100 ms
150 * f(c) = 13560000 Hz
151 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
153 #define DIGITAL_NFC_DEP_IN_MAX_WT 14
154 #define DIGITAL_NFC_DEP_TG_MAX_WT 14
155 static const u16 digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT + 1] = {
156 100, 101, 101, 102, 105,
157 110, 119, 139, 177, 255,
158 409, 719, 1337, 2575, 5049,
161 static u8 digital_payload_bits_to_size(u8 payload_bits)
163 if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
164 return 0;
166 return digital_payload_bits_map[payload_bits];
169 static u8 digital_payload_size_to_bits(u8 payload_size)
171 int i;
173 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
174 if (digital_payload_bits_map[i] == payload_size)
175 return i;
177 return 0xff;
180 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
181 struct sk_buff *skb)
183 skb_push(skb, sizeof(u8));
185 skb->data[0] = skb->len;
187 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
188 *(u8 *)skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
191 static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
192 struct sk_buff *skb)
194 u8 size;
196 if (skb->len < 2)
197 return -EIO;
199 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
200 skb_pull(skb, sizeof(u8));
202 size = skb->data[0];
203 if (size != skb->len)
204 return -EIO;
206 skb_pull(skb, sizeof(u8));
208 return 0;
211 static struct sk_buff *
212 digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
213 struct digital_dep_req_res *dep_req_res,
214 struct digital_data_exch *data_exch)
216 struct sk_buff *new_skb;
218 if (skb->len > ddev->remote_payload_max) {
219 dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
221 new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
222 if (!new_skb) {
223 kfree_skb(ddev->chaining_skb);
224 ddev->chaining_skb = NULL;
226 return ERR_PTR(-ENOMEM);
229 skb_put_data(new_skb, skb->data, ddev->remote_payload_max);
230 skb_pull(skb, ddev->remote_payload_max);
232 ddev->chaining_skb = skb;
233 ddev->data_exch = data_exch;
234 } else {
235 ddev->chaining_skb = NULL;
236 new_skb = skb;
239 return new_skb;
242 static struct sk_buff *
243 digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
244 struct sk_buff *resp,
245 int (*send_ack)(struct nfc_digital_dev *ddev,
246 struct digital_data_exch
247 *data_exch),
248 struct digital_data_exch *data_exch)
250 struct sk_buff *new_skb;
251 int rc;
253 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
254 ddev->chaining_skb =
255 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
256 GFP_KERNEL);
257 if (!ddev->chaining_skb) {
258 rc = -ENOMEM;
259 goto error;
263 if (ddev->chaining_skb) {
264 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
265 new_skb = skb_copy_expand(ddev->chaining_skb,
266 skb_headroom(
267 ddev->chaining_skb),
268 8 * ddev->local_payload_max,
269 GFP_KERNEL);
270 if (!new_skb) {
271 rc = -ENOMEM;
272 goto error;
275 kfree_skb(ddev->chaining_skb);
276 ddev->chaining_skb = new_skb;
279 skb_put_data(ddev->chaining_skb, resp->data, resp->len);
281 kfree_skb(resp);
282 resp = NULL;
284 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
285 rc = send_ack(ddev, data_exch);
286 if (rc)
287 goto error;
289 return NULL;
292 resp = ddev->chaining_skb;
293 ddev->chaining_skb = NULL;
296 return resp;
298 error:
299 kfree_skb(resp);
301 kfree_skb(ddev->chaining_skb);
302 ddev->chaining_skb = NULL;
304 return ERR_PTR(rc);
307 static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
308 struct sk_buff *resp)
310 struct nfc_target *target = arg;
311 struct digital_psl_res *psl_res;
312 int rc;
314 if (IS_ERR(resp)) {
315 rc = PTR_ERR(resp);
316 resp = NULL;
317 goto exit;
320 rc = ddev->skb_check_crc(resp);
321 if (rc) {
322 PROTOCOL_ERR("14.4.1.6");
323 goto exit;
326 rc = digital_skb_pull_dep_sod(ddev, resp);
327 if (rc) {
328 PROTOCOL_ERR("14.4.1.2");
329 goto exit;
332 psl_res = (struct digital_psl_res *)resp->data;
334 if ((resp->len != sizeof(*psl_res)) ||
335 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
336 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
337 rc = -EIO;
338 goto exit;
341 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
342 NFC_DIGITAL_RF_TECH_424F);
343 if (rc)
344 goto exit;
346 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
347 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
348 if (rc)
349 goto exit;
351 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
352 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
353 ddev->skb_add_crc = digital_skb_add_crc_f;
354 ddev->skb_check_crc = digital_skb_check_crc_f;
357 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
359 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
360 NFC_RF_INITIATOR);
362 ddev->curr_nfc_dep_pni = 0;
364 exit:
365 dev_kfree_skb(resp);
367 if (rc)
368 ddev->curr_protocol = 0;
371 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
372 struct nfc_target *target)
374 struct sk_buff *skb;
375 struct digital_psl_req *psl_req;
376 int rc;
377 u8 payload_size, payload_bits;
379 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
380 if (!skb)
381 return -ENOMEM;
383 skb_put(skb, sizeof(*psl_req));
385 psl_req = (struct digital_psl_req *)skb->data;
387 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
388 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
389 psl_req->did = 0;
390 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
392 payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
393 payload_bits = digital_payload_size_to_bits(payload_size);
394 psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
396 ddev->local_payload_max = payload_size;
397 ddev->remote_payload_max = payload_size;
399 digital_skb_push_dep_sod(ddev, skb);
401 ddev->skb_add_crc(skb);
403 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
404 digital_in_recv_psl_res, target);
405 if (rc)
406 kfree_skb(skb);
408 return rc;
411 static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
412 struct sk_buff *resp)
414 struct nfc_target *target = arg;
415 struct digital_atr_res *atr_res;
416 u8 gb_len, payload_bits;
417 u8 wt;
418 int rc;
420 if (IS_ERR(resp)) {
421 rc = PTR_ERR(resp);
422 resp = NULL;
423 goto exit;
426 rc = ddev->skb_check_crc(resp);
427 if (rc) {
428 PROTOCOL_ERR("14.4.1.6");
429 goto exit;
432 rc = digital_skb_pull_dep_sod(ddev, resp);
433 if (rc) {
434 PROTOCOL_ERR("14.4.1.2");
435 goto exit;
438 if (resp->len < sizeof(struct digital_atr_res)) {
439 rc = -EIO;
440 goto exit;
443 gb_len = resp->len - sizeof(struct digital_atr_res);
445 atr_res = (struct digital_atr_res *)resp->data;
447 wt = DIGITAL_ATR_RES_TO_WT(atr_res->to);
448 if (wt > DIGITAL_NFC_DEP_IN_MAX_WT)
449 wt = DIGITAL_NFC_DEP_IN_MAX_WT;
450 ddev->dep_rwt = digital_rwt_map[wt];
452 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
453 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
455 if (!ddev->remote_payload_max) {
456 rc = -EINVAL;
457 goto exit;
460 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
461 if (rc)
462 goto exit;
464 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
465 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
466 rc = digital_in_send_psl_req(ddev, target);
467 if (!rc)
468 goto exit;
471 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
472 NFC_RF_INITIATOR);
474 ddev->curr_nfc_dep_pni = 0;
476 exit:
477 dev_kfree_skb(resp);
479 if (rc)
480 ddev->curr_protocol = 0;
483 int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
484 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
485 size_t gb_len)
487 struct sk_buff *skb;
488 struct digital_atr_req *atr_req;
489 uint size;
490 int rc;
491 u8 payload_bits;
493 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
495 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
496 PROTOCOL_ERR("14.6.1.1");
497 return -EINVAL;
500 skb = digital_skb_alloc(ddev, size);
501 if (!skb)
502 return -ENOMEM;
504 skb_put(skb, sizeof(struct digital_atr_req));
506 atr_req = (struct digital_atr_req *)skb->data;
507 memset(atr_req, 0, sizeof(struct digital_atr_req));
509 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
510 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
511 if (target->nfcid2_len)
512 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
513 else
514 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
516 atr_req->did = 0;
517 atr_req->bs = 0;
518 atr_req->br = 0;
520 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
521 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
522 atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
524 if (gb_len) {
525 atr_req->pp |= DIGITAL_GB_BIT;
526 skb_put_data(skb, gb, gb_len);
529 digital_skb_push_dep_sod(ddev, skb);
531 ddev->skb_add_crc(skb);
533 rc = digital_in_send_cmd(ddev, skb, DIGITAL_ATR_RES_RWT,
534 digital_in_recv_atr_res, target);
535 if (rc)
536 kfree_skb(skb);
538 return rc;
541 static int digital_in_send_ack(struct nfc_digital_dev *ddev,
542 struct digital_data_exch *data_exch)
544 struct digital_dep_req_res *dep_req;
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 ddev->curr_nfc_dep_pni;
561 digital_skb_push_dep_sod(ddev, skb);
563 ddev->skb_add_crc(skb);
565 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
567 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
568 digital_in_recv_dep_res, data_exch);
569 if (rc) {
570 kfree_skb(skb);
571 kfree_skb(ddev->saved_skb);
572 ddev->saved_skb = NULL;
575 return rc;
578 static int digital_in_send_nack(struct nfc_digital_dev *ddev,
579 struct digital_data_exch *data_exch)
581 struct digital_dep_req_res *dep_req;
582 struct sk_buff *skb;
583 int rc;
585 skb = digital_skb_alloc(ddev, 1);
586 if (!skb)
587 return -ENOMEM;
589 skb_push(skb, sizeof(struct digital_dep_req_res));
591 dep_req = (struct digital_dep_req_res *)skb->data;
593 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
594 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
595 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
596 DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
598 digital_skb_push_dep_sod(ddev, skb);
600 ddev->skb_add_crc(skb);
602 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
603 digital_in_recv_dep_res, data_exch);
604 if (rc)
605 kfree_skb(skb);
607 return rc;
610 static int digital_in_send_atn(struct nfc_digital_dev *ddev,
611 struct digital_data_exch *data_exch)
613 struct digital_dep_req_res *dep_req;
614 struct sk_buff *skb;
615 int rc;
617 skb = digital_skb_alloc(ddev, 1);
618 if (!skb)
619 return -ENOMEM;
621 skb_push(skb, sizeof(struct digital_dep_req_res));
623 dep_req = (struct digital_dep_req_res *)skb->data;
625 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
626 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
627 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
629 digital_skb_push_dep_sod(ddev, skb);
631 ddev->skb_add_crc(skb);
633 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
634 digital_in_recv_dep_res, data_exch);
635 if (rc)
636 kfree_skb(skb);
638 return rc;
641 static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
642 struct digital_data_exch *data_exch, u8 rtox)
644 struct digital_dep_req_res *dep_req;
645 struct sk_buff *skb;
646 int rc;
647 u16 rwt_int;
649 rwt_int = ddev->dep_rwt * rtox;
650 if (rwt_int > digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT])
651 rwt_int = digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT];
653 skb = digital_skb_alloc(ddev, 1);
654 if (!skb)
655 return -ENOMEM;
657 skb_put_u8(skb, rtox);
659 skb_push(skb, sizeof(struct digital_dep_req_res));
661 dep_req = (struct digital_dep_req_res *)skb->data;
663 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
664 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
665 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
666 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
668 digital_skb_push_dep_sod(ddev, skb);
670 ddev->skb_add_crc(skb);
672 rc = digital_in_send_cmd(ddev, skb, rwt_int,
673 digital_in_recv_dep_res, data_exch);
674 if (rc)
675 kfree_skb(skb);
677 return rc;
680 static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
681 struct digital_data_exch *data_exch)
683 int rc;
685 if (!ddev->saved_skb)
686 return -EINVAL;
688 skb_get(ddev->saved_skb);
690 rc = digital_in_send_cmd(ddev, ddev->saved_skb, ddev->dep_rwt,
691 digital_in_recv_dep_res, data_exch);
692 if (rc)
693 kfree_skb(ddev->saved_skb);
695 return rc;
698 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
699 struct sk_buff *resp)
701 struct digital_data_exch *data_exch = arg;
702 struct digital_dep_req_res *dep_res;
703 u8 pfb;
704 uint size;
705 int rc;
706 u8 rtox;
708 if (IS_ERR(resp)) {
709 rc = PTR_ERR(resp);
710 resp = NULL;
712 if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
713 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
714 ddev->atn_count = 0;
716 rc = digital_in_send_nack(ddev, data_exch);
717 if (rc)
718 goto error;
720 return;
721 } else if ((rc == -ETIMEDOUT) &&
722 (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
723 ddev->nack_count = 0;
725 rc = digital_in_send_atn(ddev, data_exch);
726 if (rc)
727 goto error;
729 return;
732 goto exit;
735 rc = digital_skb_pull_dep_sod(ddev, resp);
736 if (rc) {
737 PROTOCOL_ERR("14.4.1.2");
738 goto exit;
741 rc = ddev->skb_check_crc(resp);
742 if (rc) {
743 if ((resp->len >= 4) &&
744 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
745 ddev->atn_count = 0;
747 rc = digital_in_send_nack(ddev, data_exch);
748 if (rc)
749 goto error;
751 kfree_skb(resp);
753 return;
756 PROTOCOL_ERR("14.4.1.6");
757 goto error;
760 ddev->atn_count = 0;
761 ddev->nack_count = 0;
763 if (resp->len > ddev->local_payload_max) {
764 rc = -EMSGSIZE;
765 goto exit;
768 size = sizeof(struct digital_dep_req_res);
769 dep_res = (struct digital_dep_req_res *)resp->data;
771 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
772 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
773 rc = -EIO;
774 goto error;
777 pfb = dep_res->pfb;
779 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
780 PROTOCOL_ERR("14.8.2.1");
781 rc = -EIO;
782 goto error;
785 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
786 rc = -EIO;
787 goto exit;
790 if (size > resp->len) {
791 rc = -EIO;
792 goto error;
795 skb_pull(resp, size);
797 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
798 case DIGITAL_NFC_DEP_PFB_I_PDU:
799 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
800 PROTOCOL_ERR("14.12.3.3");
801 rc = -EIO;
802 goto error;
805 ddev->curr_nfc_dep_pni =
806 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
808 kfree_skb(ddev->saved_skb);
809 ddev->saved_skb = NULL;
811 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
812 digital_in_send_ack,
813 data_exch);
814 if (IS_ERR(resp)) {
815 rc = PTR_ERR(resp);
816 resp = NULL;
817 goto error;
820 /* If resp is NULL then we're still chaining so return and
821 * wait for the next part of the PDU. Else, the PDU is
822 * complete so pass it up.
824 if (!resp)
825 return;
827 rc = 0;
828 break;
830 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
831 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
832 PROTOCOL_ERR("14.12.4.5");
833 rc = -EIO;
834 goto exit;
837 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
838 PROTOCOL_ERR("14.12.3.3");
839 rc = -EIO;
840 goto exit;
843 ddev->curr_nfc_dep_pni =
844 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
846 if (!ddev->chaining_skb) {
847 PROTOCOL_ERR("14.12.4.3");
848 rc = -EIO;
849 goto exit;
852 /* The initiator has received a valid ACK. Free the last sent
853 * PDU and keep on sending chained skb.
855 kfree_skb(ddev->saved_skb);
856 ddev->saved_skb = NULL;
858 rc = digital_in_send_dep_req(ddev, NULL,
859 ddev->chaining_skb,
860 ddev->data_exch);
861 if (rc)
862 goto error;
864 goto free_resp;
866 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
867 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
868 rc = digital_in_send_saved_skb(ddev, data_exch);
869 if (rc)
870 goto error;
872 goto free_resp;
875 if (ddev->atn_count || ddev->nack_count) {
876 PROTOCOL_ERR("14.12.4.4");
877 rc = -EIO;
878 goto error;
881 rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
882 if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
883 PROTOCOL_ERR("14.8.4.1");
884 rc = -EIO;
885 goto error;
888 rc = digital_in_send_rtox(ddev, data_exch, rtox);
889 if (rc)
890 goto error;
892 goto free_resp;
895 exit:
896 data_exch->cb(data_exch->cb_context, resp, rc);
898 error:
899 kfree(data_exch);
901 kfree_skb(ddev->chaining_skb);
902 ddev->chaining_skb = NULL;
904 kfree_skb(ddev->saved_skb);
905 ddev->saved_skb = NULL;
907 if (rc)
908 kfree_skb(resp);
910 return;
912 free_resp:
913 dev_kfree_skb(resp);
916 int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
917 struct nfc_target *target, struct sk_buff *skb,
918 struct digital_data_exch *data_exch)
920 struct digital_dep_req_res *dep_req;
921 struct sk_buff *chaining_skb, *tmp_skb;
922 int rc;
924 skb_push(skb, sizeof(struct digital_dep_req_res));
926 dep_req = (struct digital_dep_req_res *)skb->data;
928 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
929 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
930 dep_req->pfb = ddev->curr_nfc_dep_pni;
932 ddev->atn_count = 0;
933 ddev->nack_count = 0;
935 chaining_skb = ddev->chaining_skb;
937 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
938 if (IS_ERR(tmp_skb))
939 return PTR_ERR(tmp_skb);
941 digital_skb_push_dep_sod(ddev, tmp_skb);
943 ddev->skb_add_crc(tmp_skb);
945 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
947 rc = digital_in_send_cmd(ddev, tmp_skb, ddev->dep_rwt,
948 digital_in_recv_dep_res, data_exch);
949 if (rc) {
950 if (tmp_skb != skb)
951 kfree_skb(tmp_skb);
953 kfree_skb(chaining_skb);
954 ddev->chaining_skb = NULL;
956 kfree_skb(ddev->saved_skb);
957 ddev->saved_skb = NULL;
960 return rc;
963 static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
965 ddev->curr_rf_tech = rf_tech;
967 ddev->skb_add_crc = digital_skb_add_crc_none;
968 ddev->skb_check_crc = digital_skb_check_crc_none;
970 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
971 return;
973 switch (ddev->curr_rf_tech) {
974 case NFC_DIGITAL_RF_TECH_106A:
975 ddev->skb_add_crc = digital_skb_add_crc_a;
976 ddev->skb_check_crc = digital_skb_check_crc_a;
977 break;
979 case NFC_DIGITAL_RF_TECH_212F:
980 case NFC_DIGITAL_RF_TECH_424F:
981 ddev->skb_add_crc = digital_skb_add_crc_f;
982 ddev->skb_check_crc = digital_skb_check_crc_f;
983 break;
985 default:
986 break;
990 static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
991 struct digital_data_exch *data_exch)
993 struct digital_dep_req_res *dep_res;
994 struct sk_buff *skb;
995 int rc;
997 skb = digital_skb_alloc(ddev, 1);
998 if (!skb)
999 return -ENOMEM;
1001 skb_push(skb, sizeof(struct digital_dep_req_res));
1003 dep_res = (struct digital_dep_req_res *)skb->data;
1005 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1006 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1007 dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
1008 ddev->curr_nfc_dep_pni;
1010 if (ddev->did) {
1011 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1013 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1016 ddev->curr_nfc_dep_pni =
1017 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1019 digital_skb_push_dep_sod(ddev, skb);
1021 ddev->skb_add_crc(skb);
1023 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
1025 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1026 data_exch);
1027 if (rc) {
1028 kfree_skb(skb);
1029 kfree_skb(ddev->saved_skb);
1030 ddev->saved_skb = NULL;
1033 return rc;
1036 static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
1038 struct digital_dep_req_res *dep_res;
1039 struct sk_buff *skb;
1040 int rc;
1042 skb = digital_skb_alloc(ddev, 1);
1043 if (!skb)
1044 return -ENOMEM;
1046 skb_push(skb, sizeof(struct digital_dep_req_res));
1048 dep_res = (struct digital_dep_req_res *)skb->data;
1050 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1051 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1052 dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
1054 if (ddev->did) {
1055 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1057 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1060 digital_skb_push_dep_sod(ddev, skb);
1062 ddev->skb_add_crc(skb);
1064 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1065 NULL);
1066 if (rc)
1067 kfree_skb(skb);
1069 return rc;
1072 static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1074 int rc;
1076 if (!ddev->saved_skb)
1077 return -EINVAL;
1079 skb_get(ddev->saved_skb);
1081 rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1082 digital_tg_recv_dep_req, NULL);
1083 if (rc)
1084 kfree_skb(ddev->saved_skb);
1086 return rc;
1089 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1090 struct sk_buff *resp)
1092 int rc;
1093 struct digital_dep_req_res *dep_req;
1094 u8 pfb;
1095 size_t size;
1097 if (IS_ERR(resp)) {
1098 rc = PTR_ERR(resp);
1099 resp = NULL;
1100 goto exit;
1103 rc = ddev->skb_check_crc(resp);
1104 if (rc) {
1105 PROTOCOL_ERR("14.4.1.6");
1106 goto exit;
1109 rc = digital_skb_pull_dep_sod(ddev, resp);
1110 if (rc) {
1111 PROTOCOL_ERR("14.4.1.2");
1112 goto exit;
1115 if (resp->len > ddev->local_payload_max) {
1116 rc = -EMSGSIZE;
1117 goto exit;
1120 size = sizeof(struct digital_dep_req_res);
1121 dep_req = (struct digital_dep_req_res *)resp->data;
1123 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1124 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1125 rc = -EIO;
1126 goto exit;
1129 pfb = dep_req->pfb;
1131 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1132 if (ddev->did && (ddev->did == resp->data[3])) {
1133 size++;
1134 } else {
1135 rc = -EIO;
1136 goto exit;
1138 } else if (ddev->did) {
1139 rc = -EIO;
1140 goto exit;
1143 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1144 rc = -EIO;
1145 goto exit;
1148 if (size > resp->len) {
1149 rc = -EIO;
1150 goto exit;
1153 skb_pull(resp, size);
1155 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
1156 case DIGITAL_NFC_DEP_PFB_I_PDU:
1157 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1159 if (ddev->atn_count) {
1160 /* The target has received (and replied to) at least one
1161 * ATN DEP_REQ.
1163 ddev->atn_count = 0;
1165 /* pni of resp PDU equal to the target current pni - 1
1166 * means resp is the previous DEP_REQ PDU received from
1167 * the initiator so the target replies with saved_skb
1168 * which is the previous DEP_RES saved in
1169 * digital_tg_send_dep_res().
1171 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) ==
1172 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni - 1)) {
1173 rc = digital_tg_send_saved_skb(ddev);
1174 if (rc)
1175 goto exit;
1177 goto free_resp;
1180 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1181 * means the target probably did not received the last
1182 * DEP_REQ PDU sent by the initiator. The target
1183 * fallbacks to normal processing then.
1187 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
1188 PROTOCOL_ERR("14.12.3.4");
1189 rc = -EIO;
1190 goto exit;
1193 kfree_skb(ddev->saved_skb);
1194 ddev->saved_skb = NULL;
1196 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1197 digital_tg_send_ack, NULL);
1198 if (IS_ERR(resp)) {
1199 rc = PTR_ERR(resp);
1200 resp = NULL;
1201 goto exit;
1204 /* If resp is NULL then we're still chaining so return and
1205 * wait for the next part of the PDU. Else, the PDU is
1206 * complete so pass it up.
1208 if (!resp)
1209 return;
1211 rc = 0;
1212 break;
1213 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
1214 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* NACK */
1215 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1216 ddev->curr_nfc_dep_pni) {
1217 rc = -EIO;
1218 goto exit;
1221 ddev->atn_count = 0;
1223 rc = digital_tg_send_saved_skb(ddev);
1224 if (rc)
1225 goto exit;
1227 goto free_resp;
1230 /* ACK */
1231 if (ddev->atn_count) {
1232 /* The target has previously recevied one or more ATN
1233 * PDUs.
1235 ddev->atn_count = 0;
1237 /* If the ACK PNI is equal to the target PNI - 1 means
1238 * that the initiator did not receive the previous PDU
1239 * sent by the target so re-send it.
1241 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) ==
1242 ddev->curr_nfc_dep_pni) {
1243 rc = digital_tg_send_saved_skb(ddev);
1244 if (rc)
1245 goto exit;
1247 goto free_resp;
1250 /* Otherwise, the target did not receive the previous
1251 * ACK PDU from the initiator. Fallback to normal
1252 * processing of chained PDU then.
1256 /* Keep on sending chained PDU */
1257 if (!ddev->chaining_skb ||
1258 DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1259 ddev->curr_nfc_dep_pni) {
1260 rc = -EIO;
1261 goto exit;
1264 kfree_skb(ddev->saved_skb);
1265 ddev->saved_skb = NULL;
1267 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1268 if (rc)
1269 goto exit;
1271 goto free_resp;
1272 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1273 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1274 rc = -EINVAL;
1275 goto exit;
1278 rc = digital_tg_send_atn(ddev);
1279 if (rc)
1280 goto exit;
1282 ddev->atn_count++;
1284 goto free_resp;
1287 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1289 exit:
1290 kfree_skb(ddev->chaining_skb);
1291 ddev->chaining_skb = NULL;
1293 ddev->atn_count = 0;
1295 kfree_skb(ddev->saved_skb);
1296 ddev->saved_skb = NULL;
1298 if (rc)
1299 kfree_skb(resp);
1301 return;
1303 free_resp:
1304 dev_kfree_skb(resp);
1307 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1309 struct digital_dep_req_res *dep_res;
1310 struct sk_buff *chaining_skb, *tmp_skb;
1311 int rc;
1313 skb_push(skb, sizeof(struct digital_dep_req_res));
1315 dep_res = (struct digital_dep_req_res *)skb->data;
1317 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1318 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1319 dep_res->pfb = ddev->curr_nfc_dep_pni;
1321 if (ddev->did) {
1322 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1324 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1327 ddev->curr_nfc_dep_pni =
1328 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1330 chaining_skb = ddev->chaining_skb;
1332 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1333 if (IS_ERR(tmp_skb))
1334 return PTR_ERR(tmp_skb);
1336 digital_skb_push_dep_sod(ddev, tmp_skb);
1338 ddev->skb_add_crc(tmp_skb);
1340 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
1342 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1343 NULL);
1344 if (rc) {
1345 if (tmp_skb != skb)
1346 kfree_skb(tmp_skb);
1348 kfree_skb(chaining_skb);
1349 ddev->chaining_skb = NULL;
1351 kfree_skb(ddev->saved_skb);
1352 ddev->saved_skb = NULL;
1355 return rc;
1358 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1359 void *arg, struct sk_buff *resp)
1361 u8 rf_tech = (unsigned long)arg;
1363 if (IS_ERR(resp))
1364 return;
1366 digital_tg_set_rf_tech(ddev, rf_tech);
1368 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1370 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1372 dev_kfree_skb(resp);
1375 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1376 u8 rf_tech)
1378 struct digital_psl_res *psl_res;
1379 struct sk_buff *skb;
1380 int rc;
1382 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1383 if (!skb)
1384 return -ENOMEM;
1386 skb_put(skb, sizeof(struct digital_psl_res));
1388 psl_res = (struct digital_psl_res *)skb->data;
1390 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1391 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1392 psl_res->did = did;
1394 digital_skb_push_dep_sod(ddev, skb);
1396 ddev->skb_add_crc(skb);
1398 ddev->curr_nfc_dep_pni = 0;
1400 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1401 (void *)(unsigned long)rf_tech);
1402 if (rc)
1403 kfree_skb(skb);
1405 return rc;
1408 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1409 struct sk_buff *resp)
1411 int rc;
1412 struct digital_psl_req *psl_req;
1413 u8 rf_tech;
1414 u8 dsi, payload_size, payload_bits;
1416 if (IS_ERR(resp)) {
1417 rc = PTR_ERR(resp);
1418 resp = NULL;
1419 goto exit;
1422 rc = ddev->skb_check_crc(resp);
1423 if (rc) {
1424 PROTOCOL_ERR("14.4.1.6");
1425 goto exit;
1428 rc = digital_skb_pull_dep_sod(ddev, resp);
1429 if (rc) {
1430 PROTOCOL_ERR("14.4.1.2");
1431 goto exit;
1434 psl_req = (struct digital_psl_req *)resp->data;
1436 if (resp->len != sizeof(struct digital_psl_req) ||
1437 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1438 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1439 rc = -EIO;
1440 goto exit;
1443 dsi = (psl_req->brs >> 3) & 0x07;
1444 switch (dsi) {
1445 case 0:
1446 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1447 break;
1448 case 1:
1449 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1450 break;
1451 case 2:
1452 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1453 break;
1454 default:
1455 pr_err("Unsupported dsi value %d\n", dsi);
1456 goto exit;
1459 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1460 payload_size = digital_payload_bits_to_size(payload_bits);
1462 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1463 ddev->remote_payload_max))) {
1464 rc = -EINVAL;
1465 goto exit;
1468 ddev->local_payload_max = payload_size;
1469 ddev->remote_payload_max = payload_size;
1471 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1473 exit:
1474 kfree_skb(resp);
1477 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1478 void *arg, struct sk_buff *resp)
1480 int offset;
1482 if (IS_ERR(resp)) {
1483 digital_poll_next_tech(ddev);
1484 return;
1487 offset = 2;
1488 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1489 offset++;
1491 ddev->atn_count = 0;
1493 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1494 digital_tg_recv_psl_req(ddev, arg, resp);
1495 else
1496 digital_tg_recv_dep_req(ddev, arg, resp);
1499 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1500 struct digital_atr_req *atr_req)
1502 struct digital_atr_res *atr_res;
1503 struct sk_buff *skb;
1504 u8 *gb, payload_bits;
1505 size_t gb_len;
1506 int rc;
1508 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1509 if (!gb)
1510 gb_len = 0;
1512 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1513 if (!skb)
1514 return -ENOMEM;
1516 skb_put(skb, sizeof(struct digital_atr_res));
1517 atr_res = (struct digital_atr_res *)skb->data;
1519 memset(atr_res, 0, sizeof(struct digital_atr_res));
1521 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1522 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1523 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1524 atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
1526 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1527 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1528 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1530 if (gb_len) {
1531 skb_put(skb, gb_len);
1533 atr_res->pp |= DIGITAL_GB_BIT;
1534 memcpy(atr_res->gb, gb, gb_len);
1537 digital_skb_push_dep_sod(ddev, skb);
1539 ddev->skb_add_crc(skb);
1541 ddev->curr_nfc_dep_pni = 0;
1543 rc = digital_tg_send_cmd(ddev, skb, 999,
1544 digital_tg_send_atr_res_complete, NULL);
1545 if (rc)
1546 kfree_skb(skb);
1548 return rc;
1551 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1552 struct sk_buff *resp)
1554 int rc;
1555 struct digital_atr_req *atr_req;
1556 size_t gb_len, min_size;
1557 u8 poll_tech_count, payload_bits;
1559 if (IS_ERR(resp)) {
1560 rc = PTR_ERR(resp);
1561 resp = NULL;
1562 goto exit;
1565 if (!resp->len) {
1566 rc = -EIO;
1567 goto exit;
1570 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1571 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1572 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1573 } else {
1574 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1575 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1578 if (resp->len < min_size) {
1579 rc = -EIO;
1580 goto exit;
1583 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1585 rc = ddev->skb_check_crc(resp);
1586 if (rc) {
1587 PROTOCOL_ERR("14.4.1.6");
1588 goto exit;
1591 rc = digital_skb_pull_dep_sod(ddev, resp);
1592 if (rc) {
1593 PROTOCOL_ERR("14.4.1.2");
1594 goto exit;
1597 atr_req = (struct digital_atr_req *)resp->data;
1599 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1600 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1601 atr_req->did > DIGITAL_DID_MAX) {
1602 rc = -EINVAL;
1603 goto exit;
1606 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1607 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1609 if (!ddev->remote_payload_max) {
1610 rc = -EINVAL;
1611 goto exit;
1614 ddev->did = atr_req->did;
1616 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1617 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1618 if (rc)
1619 goto exit;
1621 rc = digital_tg_send_atr_res(ddev, atr_req);
1622 if (rc)
1623 goto exit;
1625 gb_len = resp->len - sizeof(struct digital_atr_req);
1627 poll_tech_count = ddev->poll_tech_count;
1628 ddev->poll_tech_count = 0;
1630 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1631 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1632 if (rc) {
1633 ddev->poll_tech_count = poll_tech_count;
1634 goto exit;
1637 rc = 0;
1638 exit:
1639 if (rc)
1640 digital_poll_next_tech(ddev);
1642 dev_kfree_skb(resp);