Linux 5.7.7
[linux/fpc-iii.git] / net / nfc / digital_dep.c
blob304b1a9bb18a26faf7094c041b05035f9ad07258
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_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
42 #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
44 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
46 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
47 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
48 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
49 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
51 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
52 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
53 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
54 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
55 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
56 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
57 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
59 #define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
60 #define DIGITAL_NFC_DEP_RTOX_MAX 59
62 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
63 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
64 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
66 struct digital_atr_req {
67 u8 dir;
68 u8 cmd;
69 u8 nfcid3[10];
70 u8 did;
71 u8 bs;
72 u8 br;
73 u8 pp;
74 u8 gb[];
75 } __packed;
77 struct digital_atr_res {
78 u8 dir;
79 u8 cmd;
80 u8 nfcid3[10];
81 u8 did;
82 u8 bs;
83 u8 br;
84 u8 to;
85 u8 pp;
86 u8 gb[];
87 } __packed;
89 struct digital_psl_req {
90 u8 dir;
91 u8 cmd;
92 u8 did;
93 u8 brs;
94 u8 fsl;
95 } __packed;
97 struct digital_psl_res {
98 u8 dir;
99 u8 cmd;
100 u8 did;
101 } __packed;
103 struct digital_dep_req_res {
104 u8 dir;
105 u8 cmd;
106 u8 pfb;
107 } __packed;
109 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
110 struct sk_buff *resp);
111 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
112 struct sk_buff *resp);
114 static const u8 digital_payload_bits_map[4] = {
115 [0] = 64,
116 [1] = 128,
117 [2] = 192,
118 [3] = 254
121 /* Response Waiting Time for ATR_RES PDU in ms
123 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
125 * with:
126 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
127 * dRWT(nfcdep) = 16 / f(c) s
128 * dT(nfcdep,initiator) = 100 ms
129 * f(c) = 13560000 Hz
131 #define DIGITAL_ATR_RES_RWT 1337
133 /* Response Waiting Time for other DEP PDUs in ms
135 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
137 * with:
138 * rwt = (256 * 16 / f(c)) * 2^wt s
139 * dRWT(nfcdep) = 16 / f(c) s
140 * dT(nfcdep,initiator) = 100 ms
141 * f(c) = 13560000 Hz
142 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
144 #define DIGITAL_NFC_DEP_IN_MAX_WT 14
145 #define DIGITAL_NFC_DEP_TG_MAX_WT 14
146 static const u16 digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT + 1] = {
147 100, 101, 101, 102, 105,
148 110, 119, 139, 177, 255,
149 409, 719, 1337, 2575, 5049,
152 static u8 digital_payload_bits_to_size(u8 payload_bits)
154 if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
155 return 0;
157 return digital_payload_bits_map[payload_bits];
160 static u8 digital_payload_size_to_bits(u8 payload_size)
162 int i;
164 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
165 if (digital_payload_bits_map[i] == payload_size)
166 return i;
168 return 0xff;
171 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
172 struct sk_buff *skb)
174 skb_push(skb, sizeof(u8));
176 skb->data[0] = skb->len;
178 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
179 *(u8 *)skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
182 static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
183 struct sk_buff *skb)
185 u8 size;
187 if (skb->len < 2)
188 return -EIO;
190 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
191 skb_pull(skb, sizeof(u8));
193 size = skb->data[0];
194 if (size != skb->len)
195 return -EIO;
197 skb_pull(skb, sizeof(u8));
199 return 0;
202 static struct sk_buff *
203 digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
204 struct digital_dep_req_res *dep_req_res,
205 struct digital_data_exch *data_exch)
207 struct sk_buff *new_skb;
209 if (skb->len > ddev->remote_payload_max) {
210 dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
212 new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
213 if (!new_skb) {
214 kfree_skb(ddev->chaining_skb);
215 ddev->chaining_skb = NULL;
217 return ERR_PTR(-ENOMEM);
220 skb_put_data(new_skb, skb->data, ddev->remote_payload_max);
221 skb_pull(skb, ddev->remote_payload_max);
223 ddev->chaining_skb = skb;
224 ddev->data_exch = data_exch;
225 } else {
226 ddev->chaining_skb = NULL;
227 new_skb = skb;
230 return new_skb;
233 static struct sk_buff *
234 digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
235 struct sk_buff *resp,
236 int (*send_ack)(struct nfc_digital_dev *ddev,
237 struct digital_data_exch
238 *data_exch),
239 struct digital_data_exch *data_exch)
241 struct sk_buff *new_skb;
242 int rc;
244 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
245 ddev->chaining_skb =
246 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
247 GFP_KERNEL);
248 if (!ddev->chaining_skb) {
249 rc = -ENOMEM;
250 goto error;
254 if (ddev->chaining_skb) {
255 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
256 new_skb = skb_copy_expand(ddev->chaining_skb,
257 skb_headroom(
258 ddev->chaining_skb),
259 8 * ddev->local_payload_max,
260 GFP_KERNEL);
261 if (!new_skb) {
262 rc = -ENOMEM;
263 goto error;
266 kfree_skb(ddev->chaining_skb);
267 ddev->chaining_skb = new_skb;
270 skb_put_data(ddev->chaining_skb, resp->data, resp->len);
272 kfree_skb(resp);
273 resp = NULL;
275 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
276 rc = send_ack(ddev, data_exch);
277 if (rc)
278 goto error;
280 return NULL;
283 resp = ddev->chaining_skb;
284 ddev->chaining_skb = NULL;
287 return resp;
289 error:
290 kfree_skb(resp);
292 kfree_skb(ddev->chaining_skb);
293 ddev->chaining_skb = NULL;
295 return ERR_PTR(rc);
298 static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
299 struct sk_buff *resp)
301 struct nfc_target *target = arg;
302 struct digital_psl_res *psl_res;
303 int rc;
305 if (IS_ERR(resp)) {
306 rc = PTR_ERR(resp);
307 resp = NULL;
308 goto exit;
311 rc = ddev->skb_check_crc(resp);
312 if (rc) {
313 PROTOCOL_ERR("14.4.1.6");
314 goto exit;
317 rc = digital_skb_pull_dep_sod(ddev, resp);
318 if (rc) {
319 PROTOCOL_ERR("14.4.1.2");
320 goto exit;
323 psl_res = (struct digital_psl_res *)resp->data;
325 if ((resp->len != sizeof(*psl_res)) ||
326 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
327 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
328 rc = -EIO;
329 goto exit;
332 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
333 NFC_DIGITAL_RF_TECH_424F);
334 if (rc)
335 goto exit;
337 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
338 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
339 if (rc)
340 goto exit;
342 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
343 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
344 ddev->skb_add_crc = digital_skb_add_crc_f;
345 ddev->skb_check_crc = digital_skb_check_crc_f;
348 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
350 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
351 NFC_RF_INITIATOR);
353 ddev->curr_nfc_dep_pni = 0;
355 exit:
356 dev_kfree_skb(resp);
358 if (rc)
359 ddev->curr_protocol = 0;
362 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
363 struct nfc_target *target)
365 struct sk_buff *skb;
366 struct digital_psl_req *psl_req;
367 int rc;
368 u8 payload_size, payload_bits;
370 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
371 if (!skb)
372 return -ENOMEM;
374 skb_put(skb, sizeof(*psl_req));
376 psl_req = (struct digital_psl_req *)skb->data;
378 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
379 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
380 psl_req->did = 0;
381 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
383 payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
384 payload_bits = digital_payload_size_to_bits(payload_size);
385 psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
387 ddev->local_payload_max = payload_size;
388 ddev->remote_payload_max = payload_size;
390 digital_skb_push_dep_sod(ddev, skb);
392 ddev->skb_add_crc(skb);
394 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
395 digital_in_recv_psl_res, target);
396 if (rc)
397 kfree_skb(skb);
399 return rc;
402 static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
403 struct sk_buff *resp)
405 struct nfc_target *target = arg;
406 struct digital_atr_res *atr_res;
407 u8 gb_len, payload_bits;
408 u8 wt;
409 int rc;
411 if (IS_ERR(resp)) {
412 rc = PTR_ERR(resp);
413 resp = NULL;
414 goto exit;
417 rc = ddev->skb_check_crc(resp);
418 if (rc) {
419 PROTOCOL_ERR("14.4.1.6");
420 goto exit;
423 rc = digital_skb_pull_dep_sod(ddev, resp);
424 if (rc) {
425 PROTOCOL_ERR("14.4.1.2");
426 goto exit;
429 if (resp->len < sizeof(struct digital_atr_res)) {
430 rc = -EIO;
431 goto exit;
434 gb_len = resp->len - sizeof(struct digital_atr_res);
436 atr_res = (struct digital_atr_res *)resp->data;
438 wt = DIGITAL_ATR_RES_TO_WT(atr_res->to);
439 if (wt > DIGITAL_NFC_DEP_IN_MAX_WT)
440 wt = DIGITAL_NFC_DEP_IN_MAX_WT;
441 ddev->dep_rwt = digital_rwt_map[wt];
443 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
444 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
446 if (!ddev->remote_payload_max) {
447 rc = -EINVAL;
448 goto exit;
451 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
452 if (rc)
453 goto exit;
455 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
456 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
457 rc = digital_in_send_psl_req(ddev, target);
458 if (!rc)
459 goto exit;
462 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
463 NFC_RF_INITIATOR);
465 ddev->curr_nfc_dep_pni = 0;
467 exit:
468 dev_kfree_skb(resp);
470 if (rc)
471 ddev->curr_protocol = 0;
474 int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
475 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
476 size_t gb_len)
478 struct sk_buff *skb;
479 struct digital_atr_req *atr_req;
480 uint size;
481 int rc;
482 u8 payload_bits;
484 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
486 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
487 PROTOCOL_ERR("14.6.1.1");
488 return -EINVAL;
491 skb = digital_skb_alloc(ddev, size);
492 if (!skb)
493 return -ENOMEM;
495 skb_put(skb, sizeof(struct digital_atr_req));
497 atr_req = (struct digital_atr_req *)skb->data;
498 memset(atr_req, 0, sizeof(struct digital_atr_req));
500 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
501 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
502 if (target->nfcid2_len)
503 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
504 else
505 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
507 atr_req->did = 0;
508 atr_req->bs = 0;
509 atr_req->br = 0;
511 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
512 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
513 atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
515 if (gb_len) {
516 atr_req->pp |= DIGITAL_GB_BIT;
517 skb_put_data(skb, gb, gb_len);
520 digital_skb_push_dep_sod(ddev, skb);
522 ddev->skb_add_crc(skb);
524 rc = digital_in_send_cmd(ddev, skb, DIGITAL_ATR_RES_RWT,
525 digital_in_recv_atr_res, target);
526 if (rc)
527 kfree_skb(skb);
529 return rc;
532 static int digital_in_send_ack(struct nfc_digital_dev *ddev,
533 struct digital_data_exch *data_exch)
535 struct digital_dep_req_res *dep_req;
536 struct sk_buff *skb;
537 int rc;
539 skb = digital_skb_alloc(ddev, 1);
540 if (!skb)
541 return -ENOMEM;
543 skb_push(skb, sizeof(struct digital_dep_req_res));
545 dep_req = (struct digital_dep_req_res *)skb->data;
547 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
548 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
549 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
550 ddev->curr_nfc_dep_pni;
552 digital_skb_push_dep_sod(ddev, skb);
554 ddev->skb_add_crc(skb);
556 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
558 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
559 digital_in_recv_dep_res, data_exch);
560 if (rc) {
561 kfree_skb(skb);
562 kfree_skb(ddev->saved_skb);
563 ddev->saved_skb = NULL;
566 return rc;
569 static int digital_in_send_nack(struct nfc_digital_dev *ddev,
570 struct digital_data_exch *data_exch)
572 struct digital_dep_req_res *dep_req;
573 struct sk_buff *skb;
574 int rc;
576 skb = digital_skb_alloc(ddev, 1);
577 if (!skb)
578 return -ENOMEM;
580 skb_push(skb, sizeof(struct digital_dep_req_res));
582 dep_req = (struct digital_dep_req_res *)skb->data;
584 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
585 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
586 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
587 DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
589 digital_skb_push_dep_sod(ddev, skb);
591 ddev->skb_add_crc(skb);
593 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
594 digital_in_recv_dep_res, data_exch);
595 if (rc)
596 kfree_skb(skb);
598 return rc;
601 static int digital_in_send_atn(struct nfc_digital_dev *ddev,
602 struct digital_data_exch *data_exch)
604 struct digital_dep_req_res *dep_req;
605 struct sk_buff *skb;
606 int rc;
608 skb = digital_skb_alloc(ddev, 1);
609 if (!skb)
610 return -ENOMEM;
612 skb_push(skb, sizeof(struct digital_dep_req_res));
614 dep_req = (struct digital_dep_req_res *)skb->data;
616 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
617 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
618 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
620 digital_skb_push_dep_sod(ddev, skb);
622 ddev->skb_add_crc(skb);
624 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
625 digital_in_recv_dep_res, data_exch);
626 if (rc)
627 kfree_skb(skb);
629 return rc;
632 static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
633 struct digital_data_exch *data_exch, u8 rtox)
635 struct digital_dep_req_res *dep_req;
636 struct sk_buff *skb;
637 int rc;
638 u16 rwt_int;
640 rwt_int = ddev->dep_rwt * rtox;
641 if (rwt_int > digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT])
642 rwt_int = digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT];
644 skb = digital_skb_alloc(ddev, 1);
645 if (!skb)
646 return -ENOMEM;
648 skb_put_u8(skb, rtox);
650 skb_push(skb, sizeof(struct digital_dep_req_res));
652 dep_req = (struct digital_dep_req_res *)skb->data;
654 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
655 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
656 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
657 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
659 digital_skb_push_dep_sod(ddev, skb);
661 ddev->skb_add_crc(skb);
663 rc = digital_in_send_cmd(ddev, skb, rwt_int,
664 digital_in_recv_dep_res, data_exch);
665 if (rc)
666 kfree_skb(skb);
668 return rc;
671 static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
672 struct digital_data_exch *data_exch)
674 int rc;
676 if (!ddev->saved_skb)
677 return -EINVAL;
679 skb_get(ddev->saved_skb);
681 rc = digital_in_send_cmd(ddev, ddev->saved_skb, ddev->dep_rwt,
682 digital_in_recv_dep_res, data_exch);
683 if (rc)
684 kfree_skb(ddev->saved_skb);
686 return rc;
689 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
690 struct sk_buff *resp)
692 struct digital_data_exch *data_exch = arg;
693 struct digital_dep_req_res *dep_res;
694 u8 pfb;
695 uint size;
696 int rc;
697 u8 rtox;
699 if (IS_ERR(resp)) {
700 rc = PTR_ERR(resp);
701 resp = NULL;
703 if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
704 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
705 ddev->atn_count = 0;
707 rc = digital_in_send_nack(ddev, data_exch);
708 if (rc)
709 goto error;
711 return;
712 } else if ((rc == -ETIMEDOUT) &&
713 (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
714 ddev->nack_count = 0;
716 rc = digital_in_send_atn(ddev, data_exch);
717 if (rc)
718 goto error;
720 return;
723 goto exit;
726 rc = digital_skb_pull_dep_sod(ddev, resp);
727 if (rc) {
728 PROTOCOL_ERR("14.4.1.2");
729 goto exit;
732 rc = ddev->skb_check_crc(resp);
733 if (rc) {
734 if ((resp->len >= 4) &&
735 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
736 ddev->atn_count = 0;
738 rc = digital_in_send_nack(ddev, data_exch);
739 if (rc)
740 goto error;
742 kfree_skb(resp);
744 return;
747 PROTOCOL_ERR("14.4.1.6");
748 goto error;
751 ddev->atn_count = 0;
752 ddev->nack_count = 0;
754 if (resp->len > ddev->local_payload_max) {
755 rc = -EMSGSIZE;
756 goto exit;
759 size = sizeof(struct digital_dep_req_res);
760 dep_res = (struct digital_dep_req_res *)resp->data;
762 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
763 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
764 rc = -EIO;
765 goto error;
768 pfb = dep_res->pfb;
770 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
771 PROTOCOL_ERR("14.8.2.1");
772 rc = -EIO;
773 goto error;
776 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
777 rc = -EIO;
778 goto exit;
781 if (size > resp->len) {
782 rc = -EIO;
783 goto error;
786 skb_pull(resp, size);
788 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
789 case DIGITAL_NFC_DEP_PFB_I_PDU:
790 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
791 PROTOCOL_ERR("14.12.3.3");
792 rc = -EIO;
793 goto error;
796 ddev->curr_nfc_dep_pni =
797 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
799 kfree_skb(ddev->saved_skb);
800 ddev->saved_skb = NULL;
802 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
803 digital_in_send_ack,
804 data_exch);
805 if (IS_ERR(resp)) {
806 rc = PTR_ERR(resp);
807 resp = NULL;
808 goto error;
811 /* If resp is NULL then we're still chaining so return and
812 * wait for the next part of the PDU. Else, the PDU is
813 * complete so pass it up.
815 if (!resp)
816 return;
818 rc = 0;
819 break;
821 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
822 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
823 PROTOCOL_ERR("14.12.4.5");
824 rc = -EIO;
825 goto exit;
828 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
829 PROTOCOL_ERR("14.12.3.3");
830 rc = -EIO;
831 goto exit;
834 ddev->curr_nfc_dep_pni =
835 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
837 if (!ddev->chaining_skb) {
838 PROTOCOL_ERR("14.12.4.3");
839 rc = -EIO;
840 goto exit;
843 /* The initiator has received a valid ACK. Free the last sent
844 * PDU and keep on sending chained skb.
846 kfree_skb(ddev->saved_skb);
847 ddev->saved_skb = NULL;
849 rc = digital_in_send_dep_req(ddev, NULL,
850 ddev->chaining_skb,
851 ddev->data_exch);
852 if (rc)
853 goto error;
855 goto free_resp;
857 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
858 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
859 rc = digital_in_send_saved_skb(ddev, data_exch);
860 if (rc)
861 goto error;
863 goto free_resp;
866 if (ddev->atn_count || ddev->nack_count) {
867 PROTOCOL_ERR("14.12.4.4");
868 rc = -EIO;
869 goto error;
872 rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
873 if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
874 PROTOCOL_ERR("14.8.4.1");
875 rc = -EIO;
876 goto error;
879 rc = digital_in_send_rtox(ddev, data_exch, rtox);
880 if (rc)
881 goto error;
883 goto free_resp;
886 exit:
887 data_exch->cb(data_exch->cb_context, resp, rc);
889 error:
890 kfree(data_exch);
892 kfree_skb(ddev->chaining_skb);
893 ddev->chaining_skb = NULL;
895 kfree_skb(ddev->saved_skb);
896 ddev->saved_skb = NULL;
898 if (rc)
899 kfree_skb(resp);
901 return;
903 free_resp:
904 dev_kfree_skb(resp);
907 int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
908 struct nfc_target *target, struct sk_buff *skb,
909 struct digital_data_exch *data_exch)
911 struct digital_dep_req_res *dep_req;
912 struct sk_buff *chaining_skb, *tmp_skb;
913 int rc;
915 skb_push(skb, sizeof(struct digital_dep_req_res));
917 dep_req = (struct digital_dep_req_res *)skb->data;
919 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
920 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
921 dep_req->pfb = ddev->curr_nfc_dep_pni;
923 ddev->atn_count = 0;
924 ddev->nack_count = 0;
926 chaining_skb = ddev->chaining_skb;
928 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
929 if (IS_ERR(tmp_skb))
930 return PTR_ERR(tmp_skb);
932 digital_skb_push_dep_sod(ddev, tmp_skb);
934 ddev->skb_add_crc(tmp_skb);
936 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
938 rc = digital_in_send_cmd(ddev, tmp_skb, ddev->dep_rwt,
939 digital_in_recv_dep_res, data_exch);
940 if (rc) {
941 if (tmp_skb != skb)
942 kfree_skb(tmp_skb);
944 kfree_skb(chaining_skb);
945 ddev->chaining_skb = NULL;
947 kfree_skb(ddev->saved_skb);
948 ddev->saved_skb = NULL;
951 return rc;
954 static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
956 ddev->curr_rf_tech = rf_tech;
958 ddev->skb_add_crc = digital_skb_add_crc_none;
959 ddev->skb_check_crc = digital_skb_check_crc_none;
961 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
962 return;
964 switch (ddev->curr_rf_tech) {
965 case NFC_DIGITAL_RF_TECH_106A:
966 ddev->skb_add_crc = digital_skb_add_crc_a;
967 ddev->skb_check_crc = digital_skb_check_crc_a;
968 break;
970 case NFC_DIGITAL_RF_TECH_212F:
971 case NFC_DIGITAL_RF_TECH_424F:
972 ddev->skb_add_crc = digital_skb_add_crc_f;
973 ddev->skb_check_crc = digital_skb_check_crc_f;
974 break;
976 default:
977 break;
981 static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
982 struct digital_data_exch *data_exch)
984 struct digital_dep_req_res *dep_res;
985 struct sk_buff *skb;
986 int rc;
988 skb = digital_skb_alloc(ddev, 1);
989 if (!skb)
990 return -ENOMEM;
992 skb_push(skb, sizeof(struct digital_dep_req_res));
994 dep_res = (struct digital_dep_req_res *)skb->data;
996 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
997 dep_res->cmd = DIGITAL_CMD_DEP_RES;
998 dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
999 ddev->curr_nfc_dep_pni;
1001 if (ddev->did) {
1002 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1004 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1007 ddev->curr_nfc_dep_pni =
1008 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1010 digital_skb_push_dep_sod(ddev, skb);
1012 ddev->skb_add_crc(skb);
1014 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
1016 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1017 data_exch);
1018 if (rc) {
1019 kfree_skb(skb);
1020 kfree_skb(ddev->saved_skb);
1021 ddev->saved_skb = NULL;
1024 return rc;
1027 static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
1029 struct digital_dep_req_res *dep_res;
1030 struct sk_buff *skb;
1031 int rc;
1033 skb = digital_skb_alloc(ddev, 1);
1034 if (!skb)
1035 return -ENOMEM;
1037 skb_push(skb, sizeof(struct digital_dep_req_res));
1039 dep_res = (struct digital_dep_req_res *)skb->data;
1041 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1042 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1043 dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
1045 if (ddev->did) {
1046 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1048 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1051 digital_skb_push_dep_sod(ddev, skb);
1053 ddev->skb_add_crc(skb);
1055 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1056 NULL);
1057 if (rc)
1058 kfree_skb(skb);
1060 return rc;
1063 static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1065 int rc;
1067 if (!ddev->saved_skb)
1068 return -EINVAL;
1070 skb_get(ddev->saved_skb);
1072 rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1073 digital_tg_recv_dep_req, NULL);
1074 if (rc)
1075 kfree_skb(ddev->saved_skb);
1077 return rc;
1080 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1081 struct sk_buff *resp)
1083 int rc;
1084 struct digital_dep_req_res *dep_req;
1085 u8 pfb;
1086 size_t size;
1088 if (IS_ERR(resp)) {
1089 rc = PTR_ERR(resp);
1090 resp = NULL;
1091 goto exit;
1094 rc = ddev->skb_check_crc(resp);
1095 if (rc) {
1096 PROTOCOL_ERR("14.4.1.6");
1097 goto exit;
1100 rc = digital_skb_pull_dep_sod(ddev, resp);
1101 if (rc) {
1102 PROTOCOL_ERR("14.4.1.2");
1103 goto exit;
1106 if (resp->len > ddev->local_payload_max) {
1107 rc = -EMSGSIZE;
1108 goto exit;
1111 size = sizeof(struct digital_dep_req_res);
1112 dep_req = (struct digital_dep_req_res *)resp->data;
1114 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1115 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1116 rc = -EIO;
1117 goto exit;
1120 pfb = dep_req->pfb;
1122 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1123 if (ddev->did && (ddev->did == resp->data[3])) {
1124 size++;
1125 } else {
1126 rc = -EIO;
1127 goto exit;
1129 } else if (ddev->did) {
1130 rc = -EIO;
1131 goto exit;
1134 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1135 rc = -EIO;
1136 goto exit;
1139 if (size > resp->len) {
1140 rc = -EIO;
1141 goto exit;
1144 skb_pull(resp, size);
1146 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
1147 case DIGITAL_NFC_DEP_PFB_I_PDU:
1148 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1150 if (ddev->atn_count) {
1151 /* The target has received (and replied to) at least one
1152 * ATN DEP_REQ.
1154 ddev->atn_count = 0;
1156 /* pni of resp PDU equal to the target current pni - 1
1157 * means resp is the previous DEP_REQ PDU received from
1158 * the initiator so the target replies with saved_skb
1159 * which is the previous DEP_RES saved in
1160 * digital_tg_send_dep_res().
1162 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) ==
1163 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni - 1)) {
1164 rc = digital_tg_send_saved_skb(ddev);
1165 if (rc)
1166 goto exit;
1168 goto free_resp;
1171 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1172 * means the target probably did not received the last
1173 * DEP_REQ PDU sent by the initiator. The target
1174 * fallbacks to normal processing then.
1178 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
1179 PROTOCOL_ERR("14.12.3.4");
1180 rc = -EIO;
1181 goto exit;
1184 kfree_skb(ddev->saved_skb);
1185 ddev->saved_skb = NULL;
1187 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1188 digital_tg_send_ack, NULL);
1189 if (IS_ERR(resp)) {
1190 rc = PTR_ERR(resp);
1191 resp = NULL;
1192 goto exit;
1195 /* If resp is NULL then we're still chaining so return and
1196 * wait for the next part of the PDU. Else, the PDU is
1197 * complete so pass it up.
1199 if (!resp)
1200 return;
1202 rc = 0;
1203 break;
1204 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
1205 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* NACK */
1206 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1207 ddev->curr_nfc_dep_pni) {
1208 rc = -EIO;
1209 goto exit;
1212 ddev->atn_count = 0;
1214 rc = digital_tg_send_saved_skb(ddev);
1215 if (rc)
1216 goto exit;
1218 goto free_resp;
1221 /* ACK */
1222 if (ddev->atn_count) {
1223 /* The target has previously recevied one or more ATN
1224 * PDUs.
1226 ddev->atn_count = 0;
1228 /* If the ACK PNI is equal to the target PNI - 1 means
1229 * that the initiator did not receive the previous PDU
1230 * sent by the target so re-send it.
1232 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) ==
1233 ddev->curr_nfc_dep_pni) {
1234 rc = digital_tg_send_saved_skb(ddev);
1235 if (rc)
1236 goto exit;
1238 goto free_resp;
1241 /* Otherwise, the target did not receive the previous
1242 * ACK PDU from the initiator. Fallback to normal
1243 * processing of chained PDU then.
1247 /* Keep on sending chained PDU */
1248 if (!ddev->chaining_skb ||
1249 DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1250 ddev->curr_nfc_dep_pni) {
1251 rc = -EIO;
1252 goto exit;
1255 kfree_skb(ddev->saved_skb);
1256 ddev->saved_skb = NULL;
1258 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1259 if (rc)
1260 goto exit;
1262 goto free_resp;
1263 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1264 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1265 rc = -EINVAL;
1266 goto exit;
1269 rc = digital_tg_send_atn(ddev);
1270 if (rc)
1271 goto exit;
1273 ddev->atn_count++;
1275 goto free_resp;
1278 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1280 exit:
1281 kfree_skb(ddev->chaining_skb);
1282 ddev->chaining_skb = NULL;
1284 ddev->atn_count = 0;
1286 kfree_skb(ddev->saved_skb);
1287 ddev->saved_skb = NULL;
1289 if (rc)
1290 kfree_skb(resp);
1292 return;
1294 free_resp:
1295 dev_kfree_skb(resp);
1298 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1300 struct digital_dep_req_res *dep_res;
1301 struct sk_buff *chaining_skb, *tmp_skb;
1302 int rc;
1304 skb_push(skb, sizeof(struct digital_dep_req_res));
1306 dep_res = (struct digital_dep_req_res *)skb->data;
1308 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1309 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1310 dep_res->pfb = ddev->curr_nfc_dep_pni;
1312 if (ddev->did) {
1313 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1315 skb_put_data(skb, &ddev->did, sizeof(ddev->did));
1318 ddev->curr_nfc_dep_pni =
1319 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1321 chaining_skb = ddev->chaining_skb;
1323 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1324 if (IS_ERR(tmp_skb))
1325 return PTR_ERR(tmp_skb);
1327 digital_skb_push_dep_sod(ddev, tmp_skb);
1329 ddev->skb_add_crc(tmp_skb);
1331 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
1333 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1334 NULL);
1335 if (rc) {
1336 if (tmp_skb != skb)
1337 kfree_skb(tmp_skb);
1339 kfree_skb(chaining_skb);
1340 ddev->chaining_skb = NULL;
1342 kfree_skb(ddev->saved_skb);
1343 ddev->saved_skb = NULL;
1346 return rc;
1349 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1350 void *arg, struct sk_buff *resp)
1352 u8 rf_tech = (unsigned long)arg;
1354 if (IS_ERR(resp))
1355 return;
1357 digital_tg_set_rf_tech(ddev, rf_tech);
1359 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1361 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1363 dev_kfree_skb(resp);
1366 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1367 u8 rf_tech)
1369 struct digital_psl_res *psl_res;
1370 struct sk_buff *skb;
1371 int rc;
1373 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1374 if (!skb)
1375 return -ENOMEM;
1377 skb_put(skb, sizeof(struct digital_psl_res));
1379 psl_res = (struct digital_psl_res *)skb->data;
1381 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1382 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1383 psl_res->did = did;
1385 digital_skb_push_dep_sod(ddev, skb);
1387 ddev->skb_add_crc(skb);
1389 ddev->curr_nfc_dep_pni = 0;
1391 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1392 (void *)(unsigned long)rf_tech);
1393 if (rc)
1394 kfree_skb(skb);
1396 return rc;
1399 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1400 struct sk_buff *resp)
1402 int rc;
1403 struct digital_psl_req *psl_req;
1404 u8 rf_tech;
1405 u8 dsi, payload_size, payload_bits;
1407 if (IS_ERR(resp)) {
1408 rc = PTR_ERR(resp);
1409 resp = NULL;
1410 goto exit;
1413 rc = ddev->skb_check_crc(resp);
1414 if (rc) {
1415 PROTOCOL_ERR("14.4.1.6");
1416 goto exit;
1419 rc = digital_skb_pull_dep_sod(ddev, resp);
1420 if (rc) {
1421 PROTOCOL_ERR("14.4.1.2");
1422 goto exit;
1425 psl_req = (struct digital_psl_req *)resp->data;
1427 if (resp->len != sizeof(struct digital_psl_req) ||
1428 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1429 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1430 rc = -EIO;
1431 goto exit;
1434 dsi = (psl_req->brs >> 3) & 0x07;
1435 switch (dsi) {
1436 case 0:
1437 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1438 break;
1439 case 1:
1440 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1441 break;
1442 case 2:
1443 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1444 break;
1445 default:
1446 pr_err("Unsupported dsi value %d\n", dsi);
1447 goto exit;
1450 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1451 payload_size = digital_payload_bits_to_size(payload_bits);
1453 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1454 ddev->remote_payload_max))) {
1455 rc = -EINVAL;
1456 goto exit;
1459 ddev->local_payload_max = payload_size;
1460 ddev->remote_payload_max = payload_size;
1462 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1464 exit:
1465 kfree_skb(resp);
1468 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1469 void *arg, struct sk_buff *resp)
1471 int offset;
1473 if (IS_ERR(resp)) {
1474 digital_poll_next_tech(ddev);
1475 return;
1478 offset = 2;
1479 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1480 offset++;
1482 ddev->atn_count = 0;
1484 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1485 digital_tg_recv_psl_req(ddev, arg, resp);
1486 else
1487 digital_tg_recv_dep_req(ddev, arg, resp);
1490 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1491 struct digital_atr_req *atr_req)
1493 struct digital_atr_res *atr_res;
1494 struct sk_buff *skb;
1495 u8 *gb, payload_bits;
1496 size_t gb_len;
1497 int rc;
1499 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1500 if (!gb)
1501 gb_len = 0;
1503 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1504 if (!skb)
1505 return -ENOMEM;
1507 skb_put(skb, sizeof(struct digital_atr_res));
1508 atr_res = (struct digital_atr_res *)skb->data;
1510 memset(atr_res, 0, sizeof(struct digital_atr_res));
1512 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1513 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1514 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1515 atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
1517 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1518 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1519 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1521 if (gb_len) {
1522 skb_put(skb, gb_len);
1524 atr_res->pp |= DIGITAL_GB_BIT;
1525 memcpy(atr_res->gb, gb, gb_len);
1528 digital_skb_push_dep_sod(ddev, skb);
1530 ddev->skb_add_crc(skb);
1532 ddev->curr_nfc_dep_pni = 0;
1534 rc = digital_tg_send_cmd(ddev, skb, 999,
1535 digital_tg_send_atr_res_complete, NULL);
1536 if (rc)
1537 kfree_skb(skb);
1539 return rc;
1542 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1543 struct sk_buff *resp)
1545 int rc;
1546 struct digital_atr_req *atr_req;
1547 size_t gb_len, min_size;
1548 u8 poll_tech_count, payload_bits;
1550 if (IS_ERR(resp)) {
1551 rc = PTR_ERR(resp);
1552 resp = NULL;
1553 goto exit;
1556 if (!resp->len) {
1557 rc = -EIO;
1558 goto exit;
1561 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1562 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1563 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1564 } else {
1565 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1566 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1569 if (resp->len < min_size) {
1570 rc = -EIO;
1571 goto exit;
1574 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1576 rc = ddev->skb_check_crc(resp);
1577 if (rc) {
1578 PROTOCOL_ERR("14.4.1.6");
1579 goto exit;
1582 rc = digital_skb_pull_dep_sod(ddev, resp);
1583 if (rc) {
1584 PROTOCOL_ERR("14.4.1.2");
1585 goto exit;
1588 atr_req = (struct digital_atr_req *)resp->data;
1590 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1591 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1592 atr_req->did > DIGITAL_DID_MAX) {
1593 rc = -EINVAL;
1594 goto exit;
1597 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1598 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1600 if (!ddev->remote_payload_max) {
1601 rc = -EINVAL;
1602 goto exit;
1605 ddev->did = atr_req->did;
1607 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1608 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1609 if (rc)
1610 goto exit;
1612 rc = digital_tg_send_atr_res(ddev, atr_req);
1613 if (rc)
1614 goto exit;
1616 gb_len = resp->len - sizeof(struct digital_atr_req);
1618 poll_tech_count = ddev->poll_tech_count;
1619 ddev->poll_tech_count = 0;
1621 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1622 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1623 if (rc) {
1624 ddev->poll_tech_count = poll_tech_count;
1625 goto exit;
1628 rc = 0;
1629 exit:
1630 if (rc)
1631 digital_poll_next_tech(ddev);
1633 dev_kfree_skb(resp);