crypto: aesni - make non-AVX AES-GCM work with any aadlen
[linux/fpc-iii.git] / net / nfc / digital_dep.c
blobf864ce19e13dbc5d7734ca2ff8308047089c2d70
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 8
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 *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 memcpy(skb_put(new_skb, ddev->remote_payload_max), skb->data,
230 ddev->remote_payload_max);
231 skb_pull(skb, ddev->remote_payload_max);
233 ddev->chaining_skb = skb;
234 ddev->data_exch = data_exch;
235 } else {
236 ddev->chaining_skb = NULL;
237 new_skb = skb;
240 return new_skb;
243 static struct sk_buff *
244 digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
245 struct sk_buff *resp,
246 int (*send_ack)(struct nfc_digital_dev *ddev,
247 struct digital_data_exch
248 *data_exch),
249 struct digital_data_exch *data_exch)
251 struct sk_buff *new_skb;
252 int rc;
254 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
255 ddev->chaining_skb =
256 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
257 GFP_KERNEL);
258 if (!ddev->chaining_skb) {
259 rc = -ENOMEM;
260 goto error;
264 if (ddev->chaining_skb) {
265 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
266 new_skb = skb_copy_expand(ddev->chaining_skb,
267 skb_headroom(
268 ddev->chaining_skb),
269 8 * ddev->local_payload_max,
270 GFP_KERNEL);
271 if (!new_skb) {
272 rc = -ENOMEM;
273 goto error;
276 kfree_skb(ddev->chaining_skb);
277 ddev->chaining_skb = new_skb;
280 memcpy(skb_put(ddev->chaining_skb, resp->len), resp->data,
281 resp->len);
283 kfree_skb(resp);
284 resp = NULL;
286 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
287 rc = send_ack(ddev, data_exch);
288 if (rc)
289 goto error;
291 return NULL;
294 resp = ddev->chaining_skb;
295 ddev->chaining_skb = NULL;
298 return resp;
300 error:
301 kfree_skb(resp);
303 kfree_skb(ddev->chaining_skb);
304 ddev->chaining_skb = NULL;
306 return ERR_PTR(rc);
309 static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
310 struct sk_buff *resp)
312 struct nfc_target *target = arg;
313 struct digital_psl_res *psl_res;
314 int rc;
316 if (IS_ERR(resp)) {
317 rc = PTR_ERR(resp);
318 resp = NULL;
319 goto exit;
322 rc = ddev->skb_check_crc(resp);
323 if (rc) {
324 PROTOCOL_ERR("14.4.1.6");
325 goto exit;
328 rc = digital_skb_pull_dep_sod(ddev, resp);
329 if (rc) {
330 PROTOCOL_ERR("14.4.1.2");
331 goto exit;
334 psl_res = (struct digital_psl_res *)resp->data;
336 if ((resp->len != sizeof(*psl_res)) ||
337 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
338 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
339 rc = -EIO;
340 goto exit;
343 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
344 NFC_DIGITAL_RF_TECH_424F);
345 if (rc)
346 goto exit;
348 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
349 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
350 if (rc)
351 goto exit;
353 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
354 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
355 ddev->skb_add_crc = digital_skb_add_crc_f;
356 ddev->skb_check_crc = digital_skb_check_crc_f;
359 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
361 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
362 NFC_RF_INITIATOR);
364 ddev->curr_nfc_dep_pni = 0;
366 exit:
367 dev_kfree_skb(resp);
369 if (rc)
370 ddev->curr_protocol = 0;
373 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
374 struct nfc_target *target)
376 struct sk_buff *skb;
377 struct digital_psl_req *psl_req;
378 int rc;
379 u8 payload_size, payload_bits;
381 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
382 if (!skb)
383 return -ENOMEM;
385 skb_put(skb, sizeof(*psl_req));
387 psl_req = (struct digital_psl_req *)skb->data;
389 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
390 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
391 psl_req->did = 0;
392 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
394 payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
395 payload_bits = digital_payload_size_to_bits(payload_size);
396 psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
398 ddev->local_payload_max = payload_size;
399 ddev->remote_payload_max = payload_size;
401 digital_skb_push_dep_sod(ddev, skb);
403 ddev->skb_add_crc(skb);
405 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
406 digital_in_recv_psl_res, target);
407 if (rc)
408 kfree_skb(skb);
410 return rc;
413 static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
414 struct sk_buff *resp)
416 struct nfc_target *target = arg;
417 struct digital_atr_res *atr_res;
418 u8 gb_len, payload_bits;
419 u8 wt;
420 int rc;
422 if (IS_ERR(resp)) {
423 rc = PTR_ERR(resp);
424 resp = NULL;
425 goto exit;
428 rc = ddev->skb_check_crc(resp);
429 if (rc) {
430 PROTOCOL_ERR("14.4.1.6");
431 goto exit;
434 rc = digital_skb_pull_dep_sod(ddev, resp);
435 if (rc) {
436 PROTOCOL_ERR("14.4.1.2");
437 goto exit;
440 if (resp->len < sizeof(struct digital_atr_res)) {
441 rc = -EIO;
442 goto exit;
445 gb_len = resp->len - sizeof(struct digital_atr_res);
447 atr_res = (struct digital_atr_res *)resp->data;
449 wt = DIGITAL_ATR_RES_TO_WT(atr_res->to);
450 if (wt > DIGITAL_NFC_DEP_IN_MAX_WT)
451 wt = DIGITAL_NFC_DEP_IN_MAX_WT;
452 ddev->dep_rwt = digital_rwt_map[wt];
454 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
455 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
457 if (!ddev->remote_payload_max) {
458 rc = -EINVAL;
459 goto exit;
462 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
463 if (rc)
464 goto exit;
466 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
467 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
468 rc = digital_in_send_psl_req(ddev, target);
469 if (!rc)
470 goto exit;
473 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
474 NFC_RF_INITIATOR);
476 ddev->curr_nfc_dep_pni = 0;
478 exit:
479 dev_kfree_skb(resp);
481 if (rc)
482 ddev->curr_protocol = 0;
485 int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
486 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
487 size_t gb_len)
489 struct sk_buff *skb;
490 struct digital_atr_req *atr_req;
491 uint size;
492 int rc;
493 u8 payload_bits;
495 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
497 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
498 PROTOCOL_ERR("14.6.1.1");
499 return -EINVAL;
502 skb = digital_skb_alloc(ddev, size);
503 if (!skb)
504 return -ENOMEM;
506 skb_put(skb, sizeof(struct digital_atr_req));
508 atr_req = (struct digital_atr_req *)skb->data;
509 memset(atr_req, 0, sizeof(struct digital_atr_req));
511 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
512 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
513 if (target->nfcid2_len)
514 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
515 else
516 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
518 atr_req->did = 0;
519 atr_req->bs = 0;
520 atr_req->br = 0;
522 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
523 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
524 atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
526 if (gb_len) {
527 atr_req->pp |= DIGITAL_GB_BIT;
528 memcpy(skb_put(skb, gb_len), gb, gb_len);
531 digital_skb_push_dep_sod(ddev, skb);
533 ddev->skb_add_crc(skb);
535 rc = digital_in_send_cmd(ddev, skb, DIGITAL_ATR_RES_RWT,
536 digital_in_recv_atr_res, target);
537 if (rc)
538 kfree_skb(skb);
540 return rc;
543 static int digital_in_send_ack(struct nfc_digital_dev *ddev,
544 struct digital_data_exch *data_exch)
546 struct digital_dep_req_res *dep_req;
547 struct sk_buff *skb;
548 int rc;
550 skb = digital_skb_alloc(ddev, 1);
551 if (!skb)
552 return -ENOMEM;
554 skb_push(skb, sizeof(struct digital_dep_req_res));
556 dep_req = (struct digital_dep_req_res *)skb->data;
558 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
559 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
560 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
561 ddev->curr_nfc_dep_pni;
563 digital_skb_push_dep_sod(ddev, skb);
565 ddev->skb_add_crc(skb);
567 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
569 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
570 digital_in_recv_dep_res, data_exch);
571 if (rc) {
572 kfree_skb(skb);
573 kfree_skb(ddev->saved_skb);
574 ddev->saved_skb = NULL;
577 return rc;
580 static int digital_in_send_nack(struct nfc_digital_dev *ddev,
581 struct digital_data_exch *data_exch)
583 struct digital_dep_req_res *dep_req;
584 struct sk_buff *skb;
585 int rc;
587 skb = digital_skb_alloc(ddev, 1);
588 if (!skb)
589 return -ENOMEM;
591 skb_push(skb, sizeof(struct digital_dep_req_res));
593 dep_req = (struct digital_dep_req_res *)skb->data;
595 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
596 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
597 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
598 DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
600 digital_skb_push_dep_sod(ddev, skb);
602 ddev->skb_add_crc(skb);
604 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
605 digital_in_recv_dep_res, data_exch);
606 if (rc)
607 kfree_skb(skb);
609 return rc;
612 static int digital_in_send_atn(struct nfc_digital_dev *ddev,
613 struct digital_data_exch *data_exch)
615 struct digital_dep_req_res *dep_req;
616 struct sk_buff *skb;
617 int rc;
619 skb = digital_skb_alloc(ddev, 1);
620 if (!skb)
621 return -ENOMEM;
623 skb_push(skb, sizeof(struct digital_dep_req_res));
625 dep_req = (struct digital_dep_req_res *)skb->data;
627 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
628 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
629 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
631 digital_skb_push_dep_sod(ddev, skb);
633 ddev->skb_add_crc(skb);
635 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
636 digital_in_recv_dep_res, data_exch);
637 if (rc)
638 kfree_skb(skb);
640 return rc;
643 static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
644 struct digital_data_exch *data_exch, u8 rtox)
646 struct digital_dep_req_res *dep_req;
647 struct sk_buff *skb;
648 int rc;
649 u16 rwt_int;
651 rwt_int = ddev->dep_rwt * rtox;
652 if (rwt_int > digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT])
653 rwt_int = digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT];
655 skb = digital_skb_alloc(ddev, 1);
656 if (!skb)
657 return -ENOMEM;
659 *skb_put(skb, 1) = rtox;
661 skb_push(skb, sizeof(struct digital_dep_req_res));
663 dep_req = (struct digital_dep_req_res *)skb->data;
665 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
666 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
667 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
668 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
670 digital_skb_push_dep_sod(ddev, skb);
672 ddev->skb_add_crc(skb);
674 rc = digital_in_send_cmd(ddev, skb, rwt_int,
675 digital_in_recv_dep_res, data_exch);
676 if (rc)
677 kfree_skb(skb);
679 return rc;
682 static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
683 struct digital_data_exch *data_exch)
685 int rc;
687 if (!ddev->saved_skb)
688 return -EINVAL;
690 skb_get(ddev->saved_skb);
692 rc = digital_in_send_cmd(ddev, ddev->saved_skb, ddev->dep_rwt,
693 digital_in_recv_dep_res, data_exch);
694 if (rc)
695 kfree_skb(ddev->saved_skb);
697 return rc;
700 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
701 struct sk_buff *resp)
703 struct digital_data_exch *data_exch = arg;
704 struct digital_dep_req_res *dep_res;
705 u8 pfb;
706 uint size;
707 int rc;
708 u8 rtox;
710 if (IS_ERR(resp)) {
711 rc = PTR_ERR(resp);
712 resp = NULL;
714 if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
715 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
716 ddev->atn_count = 0;
718 rc = digital_in_send_nack(ddev, data_exch);
719 if (rc)
720 goto error;
722 return;
723 } else if ((rc == -ETIMEDOUT) &&
724 (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
725 ddev->nack_count = 0;
727 rc = digital_in_send_atn(ddev, data_exch);
728 if (rc)
729 goto error;
731 return;
734 goto exit;
737 rc = digital_skb_pull_dep_sod(ddev, resp);
738 if (rc) {
739 PROTOCOL_ERR("14.4.1.2");
740 goto exit;
743 rc = ddev->skb_check_crc(resp);
744 if (rc) {
745 if ((resp->len >= 4) &&
746 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
747 ddev->atn_count = 0;
749 rc = digital_in_send_nack(ddev, data_exch);
750 if (rc)
751 goto error;
753 kfree_skb(resp);
755 return;
758 PROTOCOL_ERR("14.4.1.6");
759 goto error;
762 ddev->atn_count = 0;
763 ddev->nack_count = 0;
765 if (resp->len > ddev->local_payload_max) {
766 rc = -EMSGSIZE;
767 goto exit;
770 size = sizeof(struct digital_dep_req_res);
771 dep_res = (struct digital_dep_req_res *)resp->data;
773 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
774 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
775 rc = -EIO;
776 goto error;
779 pfb = dep_res->pfb;
781 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
782 PROTOCOL_ERR("14.8.2.1");
783 rc = -EIO;
784 goto error;
787 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
788 rc = -EIO;
789 goto exit;
792 if (size > resp->len) {
793 rc = -EIO;
794 goto error;
797 skb_pull(resp, size);
799 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
800 case DIGITAL_NFC_DEP_PFB_I_PDU:
801 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
802 PROTOCOL_ERR("14.12.3.3");
803 rc = -EIO;
804 goto error;
807 ddev->curr_nfc_dep_pni =
808 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
810 kfree_skb(ddev->saved_skb);
811 ddev->saved_skb = NULL;
813 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
814 digital_in_send_ack,
815 data_exch);
816 if (IS_ERR(resp)) {
817 rc = PTR_ERR(resp);
818 resp = NULL;
819 goto error;
822 /* If resp is NULL then we're still chaining so return and
823 * wait for the next part of the PDU. Else, the PDU is
824 * complete so pass it up.
826 if (!resp)
827 return;
829 rc = 0;
830 break;
832 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
833 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
834 PROTOCOL_ERR("14.12.4.5");
835 rc = -EIO;
836 goto exit;
839 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
840 PROTOCOL_ERR("14.12.3.3");
841 rc = -EIO;
842 goto exit;
845 ddev->curr_nfc_dep_pni =
846 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
848 if (!ddev->chaining_skb) {
849 PROTOCOL_ERR("14.12.4.3");
850 rc = -EIO;
851 goto exit;
854 /* The initiator has received a valid ACK. Free the last sent
855 * PDU and keep on sending chained skb.
857 kfree_skb(ddev->saved_skb);
858 ddev->saved_skb = NULL;
860 rc = digital_in_send_dep_req(ddev, NULL,
861 ddev->chaining_skb,
862 ddev->data_exch);
863 if (rc)
864 goto error;
866 goto free_resp;
868 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
869 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
870 rc = digital_in_send_saved_skb(ddev, data_exch);
871 if (rc)
872 goto error;
874 goto free_resp;
877 if (ddev->atn_count || ddev->nack_count) {
878 PROTOCOL_ERR("14.12.4.4");
879 rc = -EIO;
880 goto error;
883 rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
884 if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
885 PROTOCOL_ERR("14.8.4.1");
886 rc = -EIO;
887 goto error;
890 rc = digital_in_send_rtox(ddev, data_exch, rtox);
891 if (rc)
892 goto error;
894 goto free_resp;
897 exit:
898 data_exch->cb(data_exch->cb_context, resp, rc);
900 error:
901 kfree(data_exch);
903 kfree_skb(ddev->chaining_skb);
904 ddev->chaining_skb = NULL;
906 kfree_skb(ddev->saved_skb);
907 ddev->saved_skb = NULL;
909 if (rc)
910 kfree_skb(resp);
912 return;
914 free_resp:
915 dev_kfree_skb(resp);
918 int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
919 struct nfc_target *target, struct sk_buff *skb,
920 struct digital_data_exch *data_exch)
922 struct digital_dep_req_res *dep_req;
923 struct sk_buff *chaining_skb, *tmp_skb;
924 int rc;
926 skb_push(skb, sizeof(struct digital_dep_req_res));
928 dep_req = (struct digital_dep_req_res *)skb->data;
930 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
931 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
932 dep_req->pfb = ddev->curr_nfc_dep_pni;
934 ddev->atn_count = 0;
935 ddev->nack_count = 0;
937 chaining_skb = ddev->chaining_skb;
939 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
940 if (IS_ERR(tmp_skb))
941 return PTR_ERR(tmp_skb);
943 digital_skb_push_dep_sod(ddev, tmp_skb);
945 ddev->skb_add_crc(tmp_skb);
947 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
949 rc = digital_in_send_cmd(ddev, tmp_skb, ddev->dep_rwt,
950 digital_in_recv_dep_res, data_exch);
951 if (rc) {
952 if (tmp_skb != skb)
953 kfree_skb(tmp_skb);
955 kfree_skb(chaining_skb);
956 ddev->chaining_skb = NULL;
958 kfree_skb(ddev->saved_skb);
959 ddev->saved_skb = NULL;
962 return rc;
965 static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
967 ddev->curr_rf_tech = rf_tech;
969 ddev->skb_add_crc = digital_skb_add_crc_none;
970 ddev->skb_check_crc = digital_skb_check_crc_none;
972 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
973 return;
975 switch (ddev->curr_rf_tech) {
976 case NFC_DIGITAL_RF_TECH_106A:
977 ddev->skb_add_crc = digital_skb_add_crc_a;
978 ddev->skb_check_crc = digital_skb_check_crc_a;
979 break;
981 case NFC_DIGITAL_RF_TECH_212F:
982 case NFC_DIGITAL_RF_TECH_424F:
983 ddev->skb_add_crc = digital_skb_add_crc_f;
984 ddev->skb_check_crc = digital_skb_check_crc_f;
985 break;
987 default:
988 break;
992 static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
993 struct digital_data_exch *data_exch)
995 struct digital_dep_req_res *dep_res;
996 struct sk_buff *skb;
997 int rc;
999 skb = digital_skb_alloc(ddev, 1);
1000 if (!skb)
1001 return -ENOMEM;
1003 skb_push(skb, sizeof(struct digital_dep_req_res));
1005 dep_res = (struct digital_dep_req_res *)skb->data;
1007 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1008 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1009 dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
1010 ddev->curr_nfc_dep_pni;
1012 if (ddev->did) {
1013 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1015 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1016 sizeof(ddev->did));
1019 ddev->curr_nfc_dep_pni =
1020 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1022 digital_skb_push_dep_sod(ddev, skb);
1024 ddev->skb_add_crc(skb);
1026 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
1028 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1029 data_exch);
1030 if (rc) {
1031 kfree_skb(skb);
1032 kfree_skb(ddev->saved_skb);
1033 ddev->saved_skb = NULL;
1036 return rc;
1039 static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
1041 struct digital_dep_req_res *dep_res;
1042 struct sk_buff *skb;
1043 int rc;
1045 skb = digital_skb_alloc(ddev, 1);
1046 if (!skb)
1047 return -ENOMEM;
1049 skb_push(skb, sizeof(struct digital_dep_req_res));
1051 dep_res = (struct digital_dep_req_res *)skb->data;
1053 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1054 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1055 dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
1057 if (ddev->did) {
1058 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1060 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1061 sizeof(ddev->did));
1064 digital_skb_push_dep_sod(ddev, skb);
1066 ddev->skb_add_crc(skb);
1068 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1069 NULL);
1070 if (rc)
1071 kfree_skb(skb);
1073 return rc;
1076 static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1078 int rc;
1080 if (!ddev->saved_skb)
1081 return -EINVAL;
1083 skb_get(ddev->saved_skb);
1085 rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1086 digital_tg_recv_dep_req, NULL);
1087 if (rc)
1088 kfree_skb(ddev->saved_skb);
1090 return rc;
1093 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1094 struct sk_buff *resp)
1096 int rc;
1097 struct digital_dep_req_res *dep_req;
1098 u8 pfb;
1099 size_t size;
1101 if (IS_ERR(resp)) {
1102 rc = PTR_ERR(resp);
1103 resp = NULL;
1104 goto exit;
1107 rc = ddev->skb_check_crc(resp);
1108 if (rc) {
1109 PROTOCOL_ERR("14.4.1.6");
1110 goto exit;
1113 rc = digital_skb_pull_dep_sod(ddev, resp);
1114 if (rc) {
1115 PROTOCOL_ERR("14.4.1.2");
1116 goto exit;
1119 if (resp->len > ddev->local_payload_max) {
1120 rc = -EMSGSIZE;
1121 goto exit;
1124 size = sizeof(struct digital_dep_req_res);
1125 dep_req = (struct digital_dep_req_res *)resp->data;
1127 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1128 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1129 rc = -EIO;
1130 goto exit;
1133 pfb = dep_req->pfb;
1135 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1136 if (ddev->did && (ddev->did == resp->data[3])) {
1137 size++;
1138 } else {
1139 rc = -EIO;
1140 goto exit;
1142 } else if (ddev->did) {
1143 rc = -EIO;
1144 goto exit;
1147 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1148 rc = -EIO;
1149 goto exit;
1152 if (size > resp->len) {
1153 rc = -EIO;
1154 goto exit;
1157 skb_pull(resp, size);
1159 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
1160 case DIGITAL_NFC_DEP_PFB_I_PDU:
1161 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1163 if (ddev->atn_count) {
1164 /* The target has received (and replied to) at least one
1165 * ATN DEP_REQ.
1167 ddev->atn_count = 0;
1169 /* pni of resp PDU equal to the target current pni - 1
1170 * means resp is the previous DEP_REQ PDU received from
1171 * the initiator so the target replies with saved_skb
1172 * which is the previous DEP_RES saved in
1173 * digital_tg_send_dep_res().
1175 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) ==
1176 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni - 1)) {
1177 rc = digital_tg_send_saved_skb(ddev);
1178 if (rc)
1179 goto exit;
1181 goto free_resp;
1184 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1185 * means the target probably did not received the last
1186 * DEP_REQ PDU sent by the initiator. The target
1187 * fallbacks to normal processing then.
1191 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
1192 PROTOCOL_ERR("14.12.3.4");
1193 rc = -EIO;
1194 goto exit;
1197 kfree_skb(ddev->saved_skb);
1198 ddev->saved_skb = NULL;
1200 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1201 digital_tg_send_ack, NULL);
1202 if (IS_ERR(resp)) {
1203 rc = PTR_ERR(resp);
1204 resp = NULL;
1205 goto exit;
1208 /* If resp is NULL then we're still chaining so return and
1209 * wait for the next part of the PDU. Else, the PDU is
1210 * complete so pass it up.
1212 if (!resp)
1213 return;
1215 rc = 0;
1216 break;
1217 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
1218 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* NACK */
1219 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1220 ddev->curr_nfc_dep_pni) {
1221 rc = -EIO;
1222 goto exit;
1225 ddev->atn_count = 0;
1227 rc = digital_tg_send_saved_skb(ddev);
1228 if (rc)
1229 goto exit;
1231 goto free_resp;
1234 /* ACK */
1235 if (ddev->atn_count) {
1236 /* The target has previously recevied one or more ATN
1237 * PDUs.
1239 ddev->atn_count = 0;
1241 /* If the ACK PNI is equal to the target PNI - 1 means
1242 * that the initiator did not receive the previous PDU
1243 * sent by the target so re-send it.
1245 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) ==
1246 ddev->curr_nfc_dep_pni) {
1247 rc = digital_tg_send_saved_skb(ddev);
1248 if (rc)
1249 goto exit;
1251 goto free_resp;
1254 /* Otherwise, the target did not receive the previous
1255 * ACK PDU from the initiator. Fallback to normal
1256 * processing of chained PDU then.
1260 /* Keep on sending chained PDU */
1261 if (!ddev->chaining_skb ||
1262 DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1263 ddev->curr_nfc_dep_pni) {
1264 rc = -EIO;
1265 goto exit;
1268 kfree_skb(ddev->saved_skb);
1269 ddev->saved_skb = NULL;
1271 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1272 if (rc)
1273 goto exit;
1275 goto free_resp;
1276 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1277 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1278 rc = -EINVAL;
1279 goto exit;
1282 rc = digital_tg_send_atn(ddev);
1283 if (rc)
1284 goto exit;
1286 ddev->atn_count++;
1288 goto free_resp;
1291 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1293 exit:
1294 kfree_skb(ddev->chaining_skb);
1295 ddev->chaining_skb = NULL;
1297 ddev->atn_count = 0;
1299 kfree_skb(ddev->saved_skb);
1300 ddev->saved_skb = NULL;
1302 if (rc)
1303 kfree_skb(resp);
1305 return;
1307 free_resp:
1308 dev_kfree_skb(resp);
1311 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1313 struct digital_dep_req_res *dep_res;
1314 struct sk_buff *chaining_skb, *tmp_skb;
1315 int rc;
1317 skb_push(skb, sizeof(struct digital_dep_req_res));
1319 dep_res = (struct digital_dep_req_res *)skb->data;
1321 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1322 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1323 dep_res->pfb = ddev->curr_nfc_dep_pni;
1325 if (ddev->did) {
1326 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1328 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1329 sizeof(ddev->did));
1332 ddev->curr_nfc_dep_pni =
1333 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1335 chaining_skb = ddev->chaining_skb;
1337 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1338 if (IS_ERR(tmp_skb))
1339 return PTR_ERR(tmp_skb);
1341 digital_skb_push_dep_sod(ddev, tmp_skb);
1343 ddev->skb_add_crc(tmp_skb);
1345 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
1347 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1348 NULL);
1349 if (rc) {
1350 if (tmp_skb != skb)
1351 kfree_skb(tmp_skb);
1353 kfree_skb(chaining_skb);
1354 ddev->chaining_skb = NULL;
1356 kfree_skb(ddev->saved_skb);
1357 ddev->saved_skb = NULL;
1360 return rc;
1363 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1364 void *arg, struct sk_buff *resp)
1366 u8 rf_tech = (unsigned long)arg;
1368 if (IS_ERR(resp))
1369 return;
1371 digital_tg_set_rf_tech(ddev, rf_tech);
1373 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1375 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1377 dev_kfree_skb(resp);
1380 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1381 u8 rf_tech)
1383 struct digital_psl_res *psl_res;
1384 struct sk_buff *skb;
1385 int rc;
1387 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1388 if (!skb)
1389 return -ENOMEM;
1391 skb_put(skb, sizeof(struct digital_psl_res));
1393 psl_res = (struct digital_psl_res *)skb->data;
1395 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1396 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1397 psl_res->did = did;
1399 digital_skb_push_dep_sod(ddev, skb);
1401 ddev->skb_add_crc(skb);
1403 ddev->curr_nfc_dep_pni = 0;
1405 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1406 (void *)(unsigned long)rf_tech);
1407 if (rc)
1408 kfree_skb(skb);
1410 return rc;
1413 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1414 struct sk_buff *resp)
1416 int rc;
1417 struct digital_psl_req *psl_req;
1418 u8 rf_tech;
1419 u8 dsi, payload_size, payload_bits;
1421 if (IS_ERR(resp)) {
1422 rc = PTR_ERR(resp);
1423 resp = NULL;
1424 goto exit;
1427 rc = ddev->skb_check_crc(resp);
1428 if (rc) {
1429 PROTOCOL_ERR("14.4.1.6");
1430 goto exit;
1433 rc = digital_skb_pull_dep_sod(ddev, resp);
1434 if (rc) {
1435 PROTOCOL_ERR("14.4.1.2");
1436 goto exit;
1439 psl_req = (struct digital_psl_req *)resp->data;
1441 if (resp->len != sizeof(struct digital_psl_req) ||
1442 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1443 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1444 rc = -EIO;
1445 goto exit;
1448 dsi = (psl_req->brs >> 3) & 0x07;
1449 switch (dsi) {
1450 case 0:
1451 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1452 break;
1453 case 1:
1454 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1455 break;
1456 case 2:
1457 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1458 break;
1459 default:
1460 pr_err("Unsupported dsi value %d\n", dsi);
1461 goto exit;
1464 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1465 payload_size = digital_payload_bits_to_size(payload_bits);
1467 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1468 ddev->remote_payload_max))) {
1469 rc = -EINVAL;
1470 goto exit;
1473 ddev->local_payload_max = payload_size;
1474 ddev->remote_payload_max = payload_size;
1476 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1478 exit:
1479 kfree_skb(resp);
1482 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1483 void *arg, struct sk_buff *resp)
1485 int offset;
1487 if (IS_ERR(resp)) {
1488 digital_poll_next_tech(ddev);
1489 return;
1492 offset = 2;
1493 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1494 offset++;
1496 ddev->atn_count = 0;
1498 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1499 digital_tg_recv_psl_req(ddev, arg, resp);
1500 else
1501 digital_tg_recv_dep_req(ddev, arg, resp);
1504 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1505 struct digital_atr_req *atr_req)
1507 struct digital_atr_res *atr_res;
1508 struct sk_buff *skb;
1509 u8 *gb, payload_bits;
1510 size_t gb_len;
1511 int rc;
1513 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1514 if (!gb)
1515 gb_len = 0;
1517 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1518 if (!skb)
1519 return -ENOMEM;
1521 skb_put(skb, sizeof(struct digital_atr_res));
1522 atr_res = (struct digital_atr_res *)skb->data;
1524 memset(atr_res, 0, sizeof(struct digital_atr_res));
1526 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1527 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1528 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1529 atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
1531 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1532 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1533 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1535 if (gb_len) {
1536 skb_put(skb, gb_len);
1538 atr_res->pp |= DIGITAL_GB_BIT;
1539 memcpy(atr_res->gb, gb, gb_len);
1542 digital_skb_push_dep_sod(ddev, skb);
1544 ddev->skb_add_crc(skb);
1546 ddev->curr_nfc_dep_pni = 0;
1548 rc = digital_tg_send_cmd(ddev, skb, 999,
1549 digital_tg_send_atr_res_complete, NULL);
1550 if (rc)
1551 kfree_skb(skb);
1553 return rc;
1556 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1557 struct sk_buff *resp)
1559 int rc;
1560 struct digital_atr_req *atr_req;
1561 size_t gb_len, min_size;
1562 u8 poll_tech_count, payload_bits;
1564 if (IS_ERR(resp)) {
1565 rc = PTR_ERR(resp);
1566 resp = NULL;
1567 goto exit;
1570 if (!resp->len) {
1571 rc = -EIO;
1572 goto exit;
1575 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1576 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1577 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1578 } else {
1579 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1580 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1583 if (resp->len < min_size) {
1584 rc = -EIO;
1585 goto exit;
1588 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1590 rc = ddev->skb_check_crc(resp);
1591 if (rc) {
1592 PROTOCOL_ERR("14.4.1.6");
1593 goto exit;
1596 rc = digital_skb_pull_dep_sod(ddev, resp);
1597 if (rc) {
1598 PROTOCOL_ERR("14.4.1.2");
1599 goto exit;
1602 atr_req = (struct digital_atr_req *)resp->data;
1604 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1605 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1606 atr_req->did > DIGITAL_DID_MAX) {
1607 rc = -EINVAL;
1608 goto exit;
1611 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1612 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1614 if (!ddev->remote_payload_max) {
1615 rc = -EINVAL;
1616 goto exit;
1619 ddev->did = atr_req->did;
1621 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1622 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1623 if (rc)
1624 goto exit;
1626 rc = digital_tg_send_atr_res(ddev, atr_req);
1627 if (rc)
1628 goto exit;
1630 gb_len = resp->len - sizeof(struct digital_atr_req);
1632 poll_tech_count = ddev->poll_tech_count;
1633 ddev->poll_tech_count = 0;
1635 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1636 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1637 if (rc) {
1638 ddev->poll_tech_count = poll_tech_count;
1639 goto exit;
1642 rc = 0;
1643 exit:
1644 if (rc)
1645 digital_poll_next_tech(ddev);
1647 dev_kfree_skb(resp);