1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18#include "digital.h"
19
20#define DIGITAL_NFC_DEP_N_RETRY_NACK 2
21#define DIGITAL_NFC_DEP_N_RETRY_ATN 2
22
23#define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
24#define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
25
26#define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
27
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
34
35#define DIGITAL_ATR_REQ_MIN_SIZE 16
36#define DIGITAL_ATR_REQ_MAX_SIZE 64
37
38#define DIGITAL_DID_MAX 14
39
40#define DIGITAL_PAYLOAD_SIZE_MAX 254
41#define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
42#define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
43#define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
44#define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
45
46#define DIGITAL_GB_BIT 0x02
47
48#define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2
49#define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2
50
51#define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
52
53#define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
54#define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
55#define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
56#define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
57
58#define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
59 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
60#define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
61#define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
62#define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
63#define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
64#define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
65
66#define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
67#define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
68#define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
69
70struct digital_atr_req {
71 u8 dir;
72 u8 cmd;
73 u8 nfcid3[10];
74 u8 did;
75 u8 bs;
76 u8 br;
77 u8 pp;
78 u8 gb[0];
79} __packed;
80
81struct digital_atr_res {
82 u8 dir;
83 u8 cmd;
84 u8 nfcid3[10];
85 u8 did;
86 u8 bs;
87 u8 br;
88 u8 to;
89 u8 pp;
90 u8 gb[0];
91} __packed;
92
93struct digital_psl_req {
94 u8 dir;
95 u8 cmd;
96 u8 did;
97 u8 brs;
98 u8 fsl;
99} __packed;
100
101struct digital_psl_res {
102 u8 dir;
103 u8 cmd;
104 u8 did;
105} __packed;
106
107struct digital_dep_req_res {
108 u8 dir;
109 u8 cmd;
110 u8 pfb;
111} __packed;
112
113static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
114 struct sk_buff *resp);
115static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
116 struct sk_buff *resp);
117
118static const u8 digital_payload_bits_map[4] = {
119 [0] = 64,
120 [1] = 128,
121 [2] = 192,
122 [3] = 254
123};
124
125static u8 digital_payload_bits_to_size(u8 payload_bits)
126{
127 if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
128 return 0;
129
130 return digital_payload_bits_map[payload_bits];
131}
132
133static u8 digital_payload_size_to_bits(u8 payload_size)
134{
135 int i;
136
137 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
138 if (digital_payload_bits_map[i] == payload_size)
139 return i;
140
141 return 0xff;
142}
143
144static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
145 struct sk_buff *skb)
146{
147 skb_push(skb, sizeof(u8));
148
149 skb->data[0] = skb->len;
150
151 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
152 *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
153}
154
155static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
156 struct sk_buff *skb)
157{
158 u8 size;
159
160 if (skb->len < 2)
161 return -EIO;
162
163 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
164 skb_pull(skb, sizeof(u8));
165
166 size = skb->data[0];
167 if (size != skb->len)
168 return -EIO;
169
170 skb_pull(skb, sizeof(u8));
171
172 return 0;
173}
174
175static struct sk_buff *
176digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
177 struct digital_dep_req_res *dep_req_res,
178 struct digital_data_exch *data_exch)
179{
180 struct sk_buff *new_skb;
181
182 if (skb->len > ddev->remote_payload_max) {
183 dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
184
185 new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
186 if (!new_skb) {
187 kfree_skb(ddev->chaining_skb);
188 ddev->chaining_skb = NULL;
189
190 return ERR_PTR(-ENOMEM);
191 }
192
193 skb_reserve(new_skb, ddev->tx_headroom + NFC_HEADER_SIZE +
194 DIGITAL_NFC_DEP_REQ_RES_HEADROOM);
195 memcpy(skb_put(new_skb, ddev->remote_payload_max), skb->data,
196 ddev->remote_payload_max);
197 skb_pull(skb, ddev->remote_payload_max);
198
199 ddev->chaining_skb = skb;
200 ddev->data_exch = data_exch;
201 } else {
202 ddev->chaining_skb = NULL;
203 new_skb = skb;
204 }
205
206 return new_skb;
207}
208
209static struct sk_buff *
210digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
211 struct sk_buff *resp,
212 int (*send_ack)(struct nfc_digital_dev *ddev,
213 struct digital_data_exch
214 *data_exch),
215 struct digital_data_exch *data_exch)
216{
217 struct sk_buff *new_skb;
218 int rc;
219
220 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
221 ddev->chaining_skb =
222 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
223 GFP_KERNEL);
224 if (!ddev->chaining_skb) {
225 rc = -ENOMEM;
226 goto error;
227 }
228 }
229
230 if (ddev->chaining_skb) {
231 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
232 new_skb = skb_copy_expand(ddev->chaining_skb,
233 skb_headroom(
234 ddev->chaining_skb),
235 8 * ddev->local_payload_max,
236 GFP_KERNEL);
237 if (!new_skb) {
238 rc = -ENOMEM;
239 goto error;
240 }
241
242 kfree_skb(ddev->chaining_skb);
243 ddev->chaining_skb = new_skb;
244 }
245
246 memcpy(skb_put(ddev->chaining_skb, resp->len), resp->data,
247 resp->len);
248
249 kfree_skb(resp);
250 resp = NULL;
251
252 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
253 rc = send_ack(ddev, data_exch);
254 if (rc)
255 goto error;
256
257 return NULL;
258 }
259
260 resp = ddev->chaining_skb;
261 ddev->chaining_skb = NULL;
262 }
263
264 return resp;
265
266error:
267 kfree_skb(resp);
268
269 kfree_skb(ddev->chaining_skb);
270 ddev->chaining_skb = NULL;
271
272 return ERR_PTR(rc);
273}
274
275static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
276 struct sk_buff *resp)
277{
278 struct nfc_target *target = arg;
279 struct digital_psl_res *psl_res;
280 int rc;
281
282 if (IS_ERR(resp)) {
283 rc = PTR_ERR(resp);
284 resp = NULL;
285 goto exit;
286 }
287
288 rc = ddev->skb_check_crc(resp);
289 if (rc) {
290 PROTOCOL_ERR("14.4.1.6");
291 goto exit;
292 }
293
294 rc = digital_skb_pull_dep_sod(ddev, resp);
295 if (rc) {
296 PROTOCOL_ERR("14.4.1.2");
297 goto exit;
298 }
299
300 psl_res = (struct digital_psl_res *)resp->data;
301
302 if ((resp->len != sizeof(*psl_res)) ||
303 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
304 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
305 rc = -EIO;
306 goto exit;
307 }
308
309 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
310 NFC_DIGITAL_RF_TECH_424F);
311 if (rc)
312 goto exit;
313
314 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
315 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
316 if (rc)
317 goto exit;
318
319 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
320 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
321 ddev->skb_add_crc = digital_skb_add_crc_f;
322 ddev->skb_check_crc = digital_skb_check_crc_f;
323 }
324
325 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
326
327 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
328 NFC_RF_INITIATOR);
329
330 ddev->curr_nfc_dep_pni = 0;
331
332exit:
333 dev_kfree_skb(resp);
334
335 if (rc)
336 ddev->curr_protocol = 0;
337}
338
339static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
340 struct nfc_target *target)
341{
342 struct sk_buff *skb;
343 struct digital_psl_req *psl_req;
344 int rc;
345 u8 payload_size, payload_bits;
346
347 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
348 if (!skb)
349 return -ENOMEM;
350
351 skb_put(skb, sizeof(*psl_req));
352
353 psl_req = (struct digital_psl_req *)skb->data;
354
355 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
356 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
357 psl_req->did = 0;
358 psl_req->brs = (0x2 << 3) | 0x2;
359
360 payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
361 payload_bits = digital_payload_size_to_bits(payload_size);
362 psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
363
364 ddev->local_payload_max = payload_size;
365 ddev->remote_payload_max = payload_size;
366
367 digital_skb_push_dep_sod(ddev, skb);
368
369 ddev->skb_add_crc(skb);
370
371 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_psl_res,
372 target);
373 if (rc)
374 kfree_skb(skb);
375
376 return rc;
377}
378
379static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
380 struct sk_buff *resp)
381{
382 struct nfc_target *target = arg;
383 struct digital_atr_res *atr_res;
384 u8 gb_len, payload_bits;
385 int rc;
386
387 if (IS_ERR(resp)) {
388 rc = PTR_ERR(resp);
389 resp = NULL;
390 goto exit;
391 }
392
393 rc = ddev->skb_check_crc(resp);
394 if (rc) {
395 PROTOCOL_ERR("14.4.1.6");
396 goto exit;
397 }
398
399 rc = digital_skb_pull_dep_sod(ddev, resp);
400 if (rc) {
401 PROTOCOL_ERR("14.4.1.2");
402 goto exit;
403 }
404
405 if (resp->len < sizeof(struct digital_atr_res)) {
406 rc = -EIO;
407 goto exit;
408 }
409
410 gb_len = resp->len - sizeof(struct digital_atr_res);
411
412 atr_res = (struct digital_atr_res *)resp->data;
413
414 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
415 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
416
417 if (!ddev->remote_payload_max) {
418 rc = -EINVAL;
419 goto exit;
420 }
421
422 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
423 if (rc)
424 goto exit;
425
426 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
427 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
428 rc = digital_in_send_psl_req(ddev, target);
429 if (!rc)
430 goto exit;
431 }
432
433 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
434 NFC_RF_INITIATOR);
435
436 ddev->curr_nfc_dep_pni = 0;
437
438exit:
439 dev_kfree_skb(resp);
440
441 if (rc)
442 ddev->curr_protocol = 0;
443}
444
445int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
446 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
447 size_t gb_len)
448{
449 struct sk_buff *skb;
450 struct digital_atr_req *atr_req;
451 uint size;
452 int rc;
453 u8 payload_bits;
454
455 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
456
457 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
458 PROTOCOL_ERR("14.6.1.1");
459 return -EINVAL;
460 }
461
462 skb = digital_skb_alloc(ddev, size);
463 if (!skb)
464 return -ENOMEM;
465
466 skb_put(skb, sizeof(struct digital_atr_req));
467
468 atr_req = (struct digital_atr_req *)skb->data;
469 memset(atr_req, 0, sizeof(struct digital_atr_req));
470
471 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
472 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
473 if (target->nfcid2_len)
474 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
475 else
476 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
477
478 atr_req->did = 0;
479 atr_req->bs = 0;
480 atr_req->br = 0;
481
482 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
483 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
484 atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
485
486 if (gb_len) {
487 atr_req->pp |= DIGITAL_GB_BIT;
488 memcpy(skb_put(skb, gb_len), gb, gb_len);
489 }
490
491 digital_skb_push_dep_sod(ddev, skb);
492
493 ddev->skb_add_crc(skb);
494
495 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res,
496 target);
497 if (rc)
498 kfree_skb(skb);
499
500 return rc;
501}
502
503static int digital_in_send_ack(struct nfc_digital_dev *ddev,
504 struct digital_data_exch *data_exch)
505{
506 struct digital_dep_req_res *dep_req;
507 struct sk_buff *skb;
508 int rc;
509
510 skb = digital_skb_alloc(ddev, 1);
511 if (!skb)
512 return -ENOMEM;
513
514 skb_push(skb, sizeof(struct digital_dep_req_res));
515
516 dep_req = (struct digital_dep_req_res *)skb->data;
517
518 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
519 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
520 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
521 ddev->curr_nfc_dep_pni;
522
523 digital_skb_push_dep_sod(ddev, skb);
524
525 ddev->skb_add_crc(skb);
526
527 ddev->saved_skb = skb_get(skb);
528 ddev->saved_skb_len = skb->len;
529
530 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
531 data_exch);
532 if (rc) {
533 kfree_skb(skb);
534 kfree_skb(ddev->saved_skb);
535 ddev->saved_skb = NULL;
536 }
537
538 return rc;
539}
540
541static int digital_in_send_nack(struct nfc_digital_dev *ddev,
542 struct digital_data_exch *data_exch)
543{
544 struct digital_dep_req_res *dep_req;
545 struct sk_buff *skb;
546 int rc;
547
548 skb = digital_skb_alloc(ddev, 1);
549 if (!skb)
550 return -ENOMEM;
551
552 skb_push(skb, sizeof(struct digital_dep_req_res));
553
554 dep_req = (struct digital_dep_req_res *)skb->data;
555
556 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
557 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
558 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
559 DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
560
561 digital_skb_push_dep_sod(ddev, skb);
562
563 ddev->skb_add_crc(skb);
564
565 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
566 data_exch);
567 if (rc)
568 kfree_skb(skb);
569
570 return rc;
571}
572
573static int digital_in_send_atn(struct nfc_digital_dev *ddev,
574 struct digital_data_exch *data_exch)
575{
576 struct digital_dep_req_res *dep_req;
577 struct sk_buff *skb;
578 int rc;
579
580 skb = digital_skb_alloc(ddev, 1);
581 if (!skb)
582 return -ENOMEM;
583
584 skb_push(skb, sizeof(struct digital_dep_req_res));
585
586 dep_req = (struct digital_dep_req_res *)skb->data;
587
588 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
589 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
590 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
591
592 digital_skb_push_dep_sod(ddev, skb);
593
594 ddev->skb_add_crc(skb);
595
596 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
597 data_exch);
598 if (rc)
599 kfree_skb(skb);
600
601 return rc;
602}
603
604static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
605 struct digital_data_exch *data_exch, u8 rtox)
606{
607 struct digital_dep_req_res *dep_req;
608 struct sk_buff *skb;
609 int rc;
610
611 skb = digital_skb_alloc(ddev, 1);
612 if (!skb)
613 return -ENOMEM;
614
615 *skb_put(skb, 1) = rtox;
616
617 skb_push(skb, sizeof(struct digital_dep_req_res));
618
619 dep_req = (struct digital_dep_req_res *)skb->data;
620
621 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
622 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
623 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
624 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
625
626 digital_skb_push_dep_sod(ddev, skb);
627
628 ddev->skb_add_crc(skb);
629
630 ddev->saved_skb = skb_get(skb);
631 ddev->saved_skb_len = skb->len;
632
633 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
634 data_exch);
635 if (rc) {
636 kfree_skb(skb);
637 kfree_skb(ddev->saved_skb);
638 ddev->saved_skb = NULL;
639 }
640
641 return rc;
642}
643
644static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
645 struct digital_data_exch *data_exch)
646{
647 skb_get(ddev->saved_skb);
648 skb_push(ddev->saved_skb, ddev->saved_skb_len);
649
650 return digital_in_send_cmd(ddev, ddev->saved_skb, 1500,
651 digital_in_recv_dep_res, data_exch);
652}
653
654static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
655 struct sk_buff *resp)
656{
657 struct digital_data_exch *data_exch = arg;
658 struct digital_dep_req_res *dep_res;
659 u8 pfb;
660 uint size;
661 int rc;
662
663 if (IS_ERR(resp)) {
664 rc = PTR_ERR(resp);
665 resp = NULL;
666
667 if (((rc != -ETIMEDOUT) || ddev->nack_count) &&
668 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
669 ddev->atn_count = 0;
670
671 rc = digital_in_send_nack(ddev, data_exch);
672 if (rc)
673 goto error;
674
675 return;
676 } else if ((rc == -ETIMEDOUT) &&
677 (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
678 ddev->nack_count = 0;
679
680 rc = digital_in_send_atn(ddev, data_exch);
681 if (rc)
682 goto error;
683
684 return;
685 }
686
687 goto exit;
688 }
689
690 rc = digital_skb_pull_dep_sod(ddev, resp);
691 if (rc) {
692 PROTOCOL_ERR("14.4.1.2");
693 goto exit;
694 }
695
696 rc = ddev->skb_check_crc(resp);
697 if (rc) {
698 if ((resp->len >= 4) &&
699 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
700 ddev->atn_count = 0;
701
702 rc = digital_in_send_nack(ddev, data_exch);
703 if (rc)
704 goto error;
705
706 kfree_skb(resp);
707
708 return;
709 }
710
711 PROTOCOL_ERR("14.4.1.6");
712 goto error;
713 }
714
715 ddev->atn_count = 0;
716 ddev->nack_count = 0;
717
718 if (resp->len > ddev->local_payload_max) {
719 rc = -EMSGSIZE;
720 goto exit;
721 }
722
723 size = sizeof(struct digital_dep_req_res);
724 dep_res = (struct digital_dep_req_res *)resp->data;
725
726 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
727 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
728 rc = -EIO;
729 goto error;
730 }
731
732 pfb = dep_res->pfb;
733
734 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
735 PROTOCOL_ERR("14.8.2.1");
736 rc = -EIO;
737 goto error;
738 }
739
740 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
741 rc = -EIO;
742 goto exit;
743 }
744
745 if (size > resp->len) {
746 rc = -EIO;
747 goto error;
748 }
749
750 skb_pull(resp, size);
751
752 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
753 case DIGITAL_NFC_DEP_PFB_I_PDU:
754 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
755 PROTOCOL_ERR("14.12.3.3");
756 rc = -EIO;
757 goto error;
758 }
759
760 ddev->curr_nfc_dep_pni =
761 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
762
763 kfree_skb(ddev->saved_skb);
764 ddev->saved_skb = NULL;
765
766 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
767 digital_in_send_ack,
768 data_exch);
769 if (IS_ERR(resp)) {
770 rc = PTR_ERR(resp);
771 resp = NULL;
772 goto error;
773 }
774
775
776
777
778
779 if (!resp)
780 return;
781
782 rc = 0;
783 break;
784
785 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
786 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
787 PROTOCOL_ERR("14.12.3.3");
788 rc = -EIO;
789 goto exit;
790 }
791
792 ddev->curr_nfc_dep_pni =
793 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
794
795 if (ddev->chaining_skb && !DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
796 kfree_skb(ddev->saved_skb);
797 ddev->saved_skb = NULL;
798
799 rc = digital_in_send_dep_req(ddev, NULL,
800 ddev->chaining_skb,
801 ddev->data_exch);
802 if (rc)
803 goto error;
804
805 return;
806 }
807
808 pr_err("Received a ACK/NACK PDU\n");
809 rc = -EINVAL;
810 goto exit;
811
812 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
813 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
814 rc = digital_in_send_saved_skb(ddev, data_exch);
815 if (rc) {
816 kfree_skb(ddev->saved_skb);
817 goto error;
818 }
819
820 return;
821 }
822
823 kfree_skb(ddev->saved_skb);
824 ddev->saved_skb = NULL;
825
826 rc = digital_in_send_rtox(ddev, data_exch, resp->data[0]);
827 if (rc)
828 goto error;
829
830 kfree_skb(resp);
831 return;
832 }
833
834exit:
835 data_exch->cb(data_exch->cb_context, resp, rc);
836
837error:
838 kfree(data_exch);
839
840 kfree_skb(ddev->chaining_skb);
841 ddev->chaining_skb = NULL;
842
843 kfree_skb(ddev->saved_skb);
844 ddev->saved_skb = NULL;
845
846 if (rc)
847 kfree_skb(resp);
848}
849
850int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
851 struct nfc_target *target, struct sk_buff *skb,
852 struct digital_data_exch *data_exch)
853{
854 struct digital_dep_req_res *dep_req;
855 struct sk_buff *chaining_skb, *tmp_skb;
856 int rc;
857
858 skb_push(skb, sizeof(struct digital_dep_req_res));
859
860 dep_req = (struct digital_dep_req_res *)skb->data;
861
862 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
863 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
864 dep_req->pfb = ddev->curr_nfc_dep_pni;
865
866 ddev->atn_count = 0;
867 ddev->nack_count = 0;
868
869 chaining_skb = ddev->chaining_skb;
870
871 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
872 if (IS_ERR(tmp_skb))
873 return PTR_ERR(tmp_skb);
874
875 digital_skb_push_dep_sod(ddev, tmp_skb);
876
877 ddev->skb_add_crc(tmp_skb);
878
879 ddev->saved_skb = skb_get(tmp_skb);
880 ddev->saved_skb_len = tmp_skb->len;
881
882 rc = digital_in_send_cmd(ddev, tmp_skb, 1500, digital_in_recv_dep_res,
883 data_exch);
884 if (rc) {
885 if (tmp_skb != skb)
886 kfree_skb(tmp_skb);
887
888 kfree_skb(chaining_skb);
889 ddev->chaining_skb = NULL;
890
891 kfree_skb(ddev->saved_skb);
892 ddev->saved_skb = NULL;
893 }
894
895 return rc;
896}
897
898static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
899{
900 ddev->curr_rf_tech = rf_tech;
901
902 ddev->skb_add_crc = digital_skb_add_crc_none;
903 ddev->skb_check_crc = digital_skb_check_crc_none;
904
905 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
906 return;
907
908 switch (ddev->curr_rf_tech) {
909 case NFC_DIGITAL_RF_TECH_106A:
910 ddev->skb_add_crc = digital_skb_add_crc_a;
911 ddev->skb_check_crc = digital_skb_check_crc_a;
912 break;
913
914 case NFC_DIGITAL_RF_TECH_212F:
915 case NFC_DIGITAL_RF_TECH_424F:
916 ddev->skb_add_crc = digital_skb_add_crc_f;
917 ddev->skb_check_crc = digital_skb_check_crc_f;
918 break;
919
920 default:
921 break;
922 }
923}
924
925static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
926 struct digital_data_exch *data_exch)
927{
928 struct digital_dep_req_res *dep_res;
929 struct sk_buff *skb;
930 int rc;
931
932 skb = digital_skb_alloc(ddev, 1);
933 if (!skb)
934 return -ENOMEM;
935
936 skb_push(skb, sizeof(struct digital_dep_req_res));
937
938 dep_res = (struct digital_dep_req_res *)skb->data;
939
940 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
941 dep_res->cmd = DIGITAL_CMD_DEP_RES;
942 dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
943 ddev->curr_nfc_dep_pni;
944
945 if (ddev->did) {
946 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
947
948 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
949 sizeof(ddev->did));
950 }
951
952 ddev->curr_nfc_dep_pni =
953 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
954
955 digital_skb_push_dep_sod(ddev, skb);
956
957 ddev->skb_add_crc(skb);
958
959 ddev->saved_skb = skb_get(skb);
960 ddev->saved_skb_len = skb->len;
961
962 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
963 data_exch);
964 if (rc) {
965 kfree_skb(skb);
966 kfree_skb(ddev->saved_skb);
967 ddev->saved_skb = NULL;
968 }
969
970 return rc;
971}
972
973static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
974{
975 struct digital_dep_req_res *dep_res;
976 struct sk_buff *skb;
977 int rc;
978
979 skb = digital_skb_alloc(ddev, 1);
980 if (!skb)
981 return -ENOMEM;
982
983 skb_push(skb, sizeof(struct digital_dep_req_res));
984
985 dep_res = (struct digital_dep_req_res *)skb->data;
986
987 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
988 dep_res->cmd = DIGITAL_CMD_DEP_RES;
989 dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
990
991 if (ddev->did) {
992 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
993
994 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
995 sizeof(ddev->did));
996 }
997
998 digital_skb_push_dep_sod(ddev, skb);
999
1000 ddev->skb_add_crc(skb);
1001
1002 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1003 NULL);
1004 if (rc)
1005 kfree_skb(skb);
1006
1007 return rc;
1008}
1009
1010static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1011{
1012 skb_get(ddev->saved_skb);
1013 skb_push(ddev->saved_skb, ddev->saved_skb_len);
1014
1015 return digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1016 digital_tg_recv_dep_req, NULL);
1017}
1018
1019static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1020 struct sk_buff *resp)
1021{
1022 int rc;
1023 struct digital_dep_req_res *dep_req;
1024 u8 pfb;
1025 size_t size;
1026
1027 if (IS_ERR(resp)) {
1028 rc = PTR_ERR(resp);
1029 resp = NULL;
1030 goto exit;
1031 }
1032
1033 rc = ddev->skb_check_crc(resp);
1034 if (rc) {
1035 PROTOCOL_ERR("14.4.1.6");
1036 goto exit;
1037 }
1038
1039 rc = digital_skb_pull_dep_sod(ddev, resp);
1040 if (rc) {
1041 PROTOCOL_ERR("14.4.1.2");
1042 goto exit;
1043 }
1044
1045 if (resp->len > ddev->local_payload_max) {
1046 rc = -EMSGSIZE;
1047 goto exit;
1048 }
1049
1050 size = sizeof(struct digital_dep_req_res);
1051 dep_req = (struct digital_dep_req_res *)resp->data;
1052
1053 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1054 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1055 rc = -EIO;
1056 goto exit;
1057 }
1058
1059 pfb = dep_req->pfb;
1060
1061 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1062 if (ddev->did && (ddev->did == resp->data[3])) {
1063 size++;
1064 } else {
1065 rc = -EIO;
1066 goto exit;
1067 }
1068 } else if (ddev->did) {
1069 rc = -EIO;
1070 goto exit;
1071 }
1072
1073 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1074 rc = -EIO;
1075 goto exit;
1076 }
1077
1078 if (size > resp->len) {
1079 rc = -EIO;
1080 goto exit;
1081 }
1082
1083 skb_pull(resp, size);
1084
1085 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
1086 case DIGITAL_NFC_DEP_PFB_I_PDU:
1087 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1088
1089 if ((ddev->atn_count && (DIGITAL_NFC_DEP_PFB_PNI(pfb - 1) !=
1090 ddev->curr_nfc_dep_pni)) ||
1091 (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni)) {
1092 PROTOCOL_ERR("14.12.3.4");
1093 rc = -EIO;
1094 goto exit;
1095 }
1096
1097 if (ddev->atn_count) {
1098 ddev->atn_count = 0;
1099
1100 rc = digital_tg_send_saved_skb(ddev);
1101 if (rc)
1102 goto exit;
1103
1104 return;
1105 }
1106
1107 kfree_skb(ddev->saved_skb);
1108 ddev->saved_skb = NULL;
1109
1110 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1111 digital_tg_send_ack, NULL);
1112 if (IS_ERR(resp)) {
1113 rc = PTR_ERR(resp);
1114 resp = NULL;
1115 goto exit;
1116 }
1117
1118
1119
1120
1121
1122 if (!resp)
1123 return;
1124
1125 rc = 0;
1126 break;
1127 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
1128 if (!DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
1129 if ((ddev->atn_count &&
1130 (DIGITAL_NFC_DEP_PFB_PNI(pfb - 1) !=
1131 ddev->curr_nfc_dep_pni)) ||
1132 (DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1133 ddev->curr_nfc_dep_pni) ||
1134 !ddev->chaining_skb || !ddev->saved_skb) {
1135 rc = -EIO;
1136 goto exit;
1137 }
1138
1139 if (ddev->atn_count) {
1140 ddev->atn_count = 0;
1141
1142 rc = digital_tg_send_saved_skb(ddev);
1143 if (rc)
1144 goto exit;
1145
1146 return;
1147 }
1148
1149 kfree_skb(ddev->saved_skb);
1150 ddev->saved_skb = NULL;
1151
1152 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1153 if (rc)
1154 goto exit;
1155 } else {
1156 if ((DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1157 ddev->curr_nfc_dep_pni) ||
1158 !ddev->saved_skb) {
1159 rc = -EIO;
1160 goto exit;
1161 }
1162
1163 ddev->atn_count = 0;
1164
1165 rc = digital_tg_send_saved_skb(ddev);
1166 if (rc) {
1167 kfree_skb(ddev->saved_skb);
1168 goto exit;
1169 }
1170 }
1171
1172 return;
1173 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1174 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1175 rc = -EINVAL;
1176 goto exit;
1177 }
1178
1179 rc = digital_tg_send_atn(ddev);
1180 if (rc)
1181 goto exit;
1182
1183 ddev->atn_count++;
1184
1185 kfree_skb(resp);
1186 return;
1187 }
1188
1189 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1190
1191exit:
1192 kfree_skb(ddev->chaining_skb);
1193 ddev->chaining_skb = NULL;
1194
1195 ddev->atn_count = 0;
1196
1197 kfree_skb(ddev->saved_skb);
1198 ddev->saved_skb = NULL;
1199
1200 if (rc)
1201 kfree_skb(resp);
1202}
1203
1204int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1205{
1206 struct digital_dep_req_res *dep_res;
1207 struct sk_buff *chaining_skb, *tmp_skb;
1208 int rc;
1209
1210 skb_push(skb, sizeof(struct digital_dep_req_res));
1211
1212 dep_res = (struct digital_dep_req_res *)skb->data;
1213
1214 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1215 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1216 dep_res->pfb = ddev->curr_nfc_dep_pni;
1217
1218 if (ddev->did) {
1219 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1220
1221 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1222 sizeof(ddev->did));
1223 }
1224
1225 ddev->curr_nfc_dep_pni =
1226 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1227
1228 chaining_skb = ddev->chaining_skb;
1229
1230 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1231 if (IS_ERR(tmp_skb))
1232 return PTR_ERR(tmp_skb);
1233
1234 digital_skb_push_dep_sod(ddev, tmp_skb);
1235
1236 ddev->skb_add_crc(tmp_skb);
1237
1238 ddev->saved_skb = skb_get(tmp_skb);
1239 ddev->saved_skb_len = tmp_skb->len;
1240
1241 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1242 NULL);
1243 if (rc) {
1244 if (tmp_skb != skb)
1245 kfree_skb(tmp_skb);
1246
1247 kfree_skb(chaining_skb);
1248 ddev->chaining_skb = NULL;
1249
1250 kfree_skb(ddev->saved_skb);
1251 ddev->saved_skb = NULL;
1252 }
1253
1254 return rc;
1255}
1256
1257static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1258 void *arg, struct sk_buff *resp)
1259{
1260 u8 rf_tech = (unsigned long)arg;
1261
1262 if (IS_ERR(resp))
1263 return;
1264
1265 digital_tg_set_rf_tech(ddev, rf_tech);
1266
1267 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1268
1269 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1270
1271 dev_kfree_skb(resp);
1272}
1273
1274static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1275 u8 rf_tech)
1276{
1277 struct digital_psl_res *psl_res;
1278 struct sk_buff *skb;
1279 int rc;
1280
1281 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1282 if (!skb)
1283 return -ENOMEM;
1284
1285 skb_put(skb, sizeof(struct digital_psl_res));
1286
1287 psl_res = (struct digital_psl_res *)skb->data;
1288
1289 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1290 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1291 psl_res->did = did;
1292
1293 digital_skb_push_dep_sod(ddev, skb);
1294
1295 ddev->skb_add_crc(skb);
1296
1297 ddev->curr_nfc_dep_pni = 0;
1298
1299 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1300 (void *)(unsigned long)rf_tech);
1301 if (rc)
1302 kfree_skb(skb);
1303
1304 return rc;
1305}
1306
1307static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1308 struct sk_buff *resp)
1309{
1310 int rc;
1311 struct digital_psl_req *psl_req;
1312 u8 rf_tech;
1313 u8 dsi, payload_size, payload_bits;
1314
1315 if (IS_ERR(resp)) {
1316 rc = PTR_ERR(resp);
1317 resp = NULL;
1318 goto exit;
1319 }
1320
1321 rc = ddev->skb_check_crc(resp);
1322 if (rc) {
1323 PROTOCOL_ERR("14.4.1.6");
1324 goto exit;
1325 }
1326
1327 rc = digital_skb_pull_dep_sod(ddev, resp);
1328 if (rc) {
1329 PROTOCOL_ERR("14.4.1.2");
1330 goto exit;
1331 }
1332
1333 psl_req = (struct digital_psl_req *)resp->data;
1334
1335 if (resp->len != sizeof(struct digital_psl_req) ||
1336 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1337 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1338 rc = -EIO;
1339 goto exit;
1340 }
1341
1342 dsi = (psl_req->brs >> 3) & 0x07;
1343 switch (dsi) {
1344 case 0:
1345 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1346 break;
1347 case 1:
1348 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1349 break;
1350 case 2:
1351 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1352 break;
1353 default:
1354 pr_err("Unsupported dsi value %d\n", dsi);
1355 goto exit;
1356 }
1357
1358 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1359 payload_size = digital_payload_bits_to_size(payload_bits);
1360
1361 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1362 ddev->remote_payload_max))) {
1363 rc = -EINVAL;
1364 goto exit;
1365 }
1366
1367 ddev->local_payload_max = payload_size;
1368 ddev->remote_payload_max = payload_size;
1369
1370 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1371
1372exit:
1373 kfree_skb(resp);
1374}
1375
1376static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1377 void *arg, struct sk_buff *resp)
1378{
1379 int offset;
1380
1381 if (IS_ERR(resp)) {
1382 digital_poll_next_tech(ddev);
1383 return;
1384 }
1385
1386 offset = 2;
1387 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1388 offset++;
1389
1390 ddev->atn_count = 0;
1391
1392 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1393 digital_tg_recv_psl_req(ddev, arg, resp);
1394 else
1395 digital_tg_recv_dep_req(ddev, arg, resp);
1396}
1397
1398static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1399 struct digital_atr_req *atr_req)
1400{
1401 struct digital_atr_res *atr_res;
1402 struct sk_buff *skb;
1403 u8 *gb, payload_bits;
1404 size_t gb_len;
1405 int rc;
1406
1407 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1408 if (!gb)
1409 gb_len = 0;
1410
1411 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1412 if (!skb)
1413 return -ENOMEM;
1414
1415 skb_put(skb, sizeof(struct digital_atr_res));
1416 atr_res = (struct digital_atr_res *)skb->data;
1417
1418 memset(atr_res, 0, sizeof(struct digital_atr_res));
1419
1420 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1421 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1422 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1423 atr_res->to = 8;
1424
1425 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1426 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1427 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1428
1429 if (gb_len) {
1430 skb_put(skb, gb_len);
1431
1432 atr_res->pp |= DIGITAL_GB_BIT;
1433 memcpy(atr_res->gb, gb, gb_len);
1434 }
1435
1436 digital_skb_push_dep_sod(ddev, skb);
1437
1438 ddev->skb_add_crc(skb);
1439
1440 ddev->curr_nfc_dep_pni = 0;
1441
1442 rc = digital_tg_send_cmd(ddev, skb, 999,
1443 digital_tg_send_atr_res_complete, NULL);
1444 if (rc)
1445 kfree_skb(skb);
1446
1447 return rc;
1448}
1449
1450void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1451 struct sk_buff *resp)
1452{
1453 int rc;
1454 struct digital_atr_req *atr_req;
1455 size_t gb_len, min_size;
1456 u8 poll_tech_count, payload_bits;
1457
1458 if (IS_ERR(resp)) {
1459 rc = PTR_ERR(resp);
1460 resp = NULL;
1461 goto exit;
1462 }
1463
1464 if (!resp->len) {
1465 rc = -EIO;
1466 goto exit;
1467 }
1468
1469 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1470 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1471 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1472 } else {
1473 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1474 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1475 }
1476
1477 if (resp->len < min_size) {
1478 rc = -EIO;
1479 goto exit;
1480 }
1481
1482 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1483
1484 rc = ddev->skb_check_crc(resp);
1485 if (rc) {
1486 PROTOCOL_ERR("14.4.1.6");
1487 goto exit;
1488 }
1489
1490 rc = digital_skb_pull_dep_sod(ddev, resp);
1491 if (rc) {
1492 PROTOCOL_ERR("14.4.1.2");
1493 goto exit;
1494 }
1495
1496 atr_req = (struct digital_atr_req *)resp->data;
1497
1498 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1499 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1500 atr_req->did > DIGITAL_DID_MAX) {
1501 rc = -EINVAL;
1502 goto exit;
1503 }
1504
1505 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1506 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1507
1508 if (!ddev->remote_payload_max) {
1509 rc = -EINVAL;
1510 goto exit;
1511 }
1512
1513 ddev->did = atr_req->did;
1514
1515 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1516 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1517 if (rc)
1518 goto exit;
1519
1520 rc = digital_tg_send_atr_res(ddev, atr_req);
1521 if (rc)
1522 goto exit;
1523
1524 gb_len = resp->len - sizeof(struct digital_atr_req);
1525
1526 poll_tech_count = ddev->poll_tech_count;
1527 ddev->poll_tech_count = 0;
1528
1529 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1530 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1531 if (rc) {
1532 ddev->poll_tech_count = poll_tech_count;
1533 goto exit;
1534 }
1535
1536 rc = 0;
1537exit:
1538 if (rc)
1539 digital_poll_next_tech(ddev);
1540
1541 dev_kfree_skb(resp);
1542}
1543