]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/nfc/digital_dep.c
NFC: digital: Add Target-mode NFC-DEP DID Support
[mirror_ubuntu-bionic-kernel.git] / net / nfc / digital_dep.c
1 /*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
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.
8 *
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.
13 *
14 */
15
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18 #include "digital.h"
19
20 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
21 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
22
23 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
24
25 #define DIGITAL_CMD_ATR_REQ 0x00
26 #define DIGITAL_CMD_ATR_RES 0x01
27 #define DIGITAL_CMD_PSL_REQ 0x04
28 #define DIGITAL_CMD_PSL_RES 0x05
29 #define DIGITAL_CMD_DEP_REQ 0x06
30 #define DIGITAL_CMD_DEP_RES 0x07
31
32 #define DIGITAL_ATR_REQ_MIN_SIZE 16
33 #define DIGITAL_ATR_REQ_MAX_SIZE 64
34
35 #define DIGITAL_DID_MAX 14
36
37 #define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
38 #define DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B \
39 (DIGITAL_LR_BITS_PAYLOAD_SIZE_254B >> 4)
40 #define DIGITAL_GB_BIT 0x02
41
42 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
43
44 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
45 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
46
47 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
48 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
49 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
50 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
51 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
52 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
53
54 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
55 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
56 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
57
58 struct digital_atr_req {
59 u8 dir;
60 u8 cmd;
61 u8 nfcid3[10];
62 u8 did;
63 u8 bs;
64 u8 br;
65 u8 pp;
66 u8 gb[0];
67 } __packed;
68
69 struct digital_atr_res {
70 u8 dir;
71 u8 cmd;
72 u8 nfcid3[10];
73 u8 did;
74 u8 bs;
75 u8 br;
76 u8 to;
77 u8 pp;
78 u8 gb[0];
79 } __packed;
80
81 struct digital_psl_req {
82 u8 dir;
83 u8 cmd;
84 u8 did;
85 u8 brs;
86 u8 fsl;
87 } __packed;
88
89 struct digital_psl_res {
90 u8 dir;
91 u8 cmd;
92 u8 did;
93 } __packed;
94
95 struct digital_dep_req_res {
96 u8 dir;
97 u8 cmd;
98 u8 pfb;
99 } __packed;
100
101 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
102 struct sk_buff *resp);
103
104 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
105 struct sk_buff *skb)
106 {
107 skb_push(skb, sizeof(u8));
108
109 skb->data[0] = skb->len;
110
111 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
112 *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
113 }
114
115 static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
116 struct sk_buff *skb)
117 {
118 u8 size;
119
120 if (skb->len < 2)
121 return -EIO;
122
123 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
124 skb_pull(skb, sizeof(u8));
125
126 size = skb->data[0];
127 if (size != skb->len)
128 return -EIO;
129
130 skb_pull(skb, sizeof(u8));
131
132 return 0;
133 }
134
135 static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
136 struct sk_buff *resp)
137 {
138 struct nfc_target *target = arg;
139 struct digital_psl_res *psl_res;
140 int rc;
141
142 if (IS_ERR(resp)) {
143 rc = PTR_ERR(resp);
144 resp = NULL;
145 goto exit;
146 }
147
148 rc = ddev->skb_check_crc(resp);
149 if (rc) {
150 PROTOCOL_ERR("14.4.1.6");
151 goto exit;
152 }
153
154 rc = digital_skb_pull_dep_sod(ddev, resp);
155 if (rc) {
156 PROTOCOL_ERR("14.4.1.2");
157 goto exit;
158 }
159
160 psl_res = (struct digital_psl_res *)resp->data;
161
162 if ((resp->len != sizeof(*psl_res)) ||
163 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
164 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
165 rc = -EIO;
166 goto exit;
167 }
168
169 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
170 NFC_DIGITAL_RF_TECH_424F);
171 if (rc)
172 goto exit;
173
174 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
175 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
176 if (rc)
177 goto exit;
178
179 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
180 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
181 ddev->skb_add_crc = digital_skb_add_crc_f;
182 ddev->skb_check_crc = digital_skb_check_crc_f;
183 }
184
185 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
186
187 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
188 NFC_RF_INITIATOR);
189
190 ddev->curr_nfc_dep_pni = 0;
191
192 exit:
193 dev_kfree_skb(resp);
194
195 if (rc)
196 ddev->curr_protocol = 0;
197 }
198
199 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
200 struct nfc_target *target)
201 {
202 struct sk_buff *skb;
203 struct digital_psl_req *psl_req;
204 int rc;
205
206 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
207 if (!skb)
208 return -ENOMEM;
209
210 skb_put(skb, sizeof(*psl_req));
211
212 psl_req = (struct digital_psl_req *)skb->data;
213
214 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
215 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
216 psl_req->did = 0;
217 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
218 psl_req->fsl = DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B;
219
220 digital_skb_push_dep_sod(ddev, skb);
221
222 ddev->skb_add_crc(skb);
223
224 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_psl_res,
225 target);
226 if (rc)
227 kfree_skb(skb);
228
229 return rc;
230 }
231
232 static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
233 struct sk_buff *resp)
234 {
235 struct nfc_target *target = arg;
236 struct digital_atr_res *atr_res;
237 u8 gb_len;
238 int rc;
239
240 if (IS_ERR(resp)) {
241 rc = PTR_ERR(resp);
242 resp = NULL;
243 goto exit;
244 }
245
246 rc = ddev->skb_check_crc(resp);
247 if (rc) {
248 PROTOCOL_ERR("14.4.1.6");
249 goto exit;
250 }
251
252 rc = digital_skb_pull_dep_sod(ddev, resp);
253 if (rc) {
254 PROTOCOL_ERR("14.4.1.2");
255 goto exit;
256 }
257
258 if (resp->len < sizeof(struct digital_atr_res)) {
259 rc = -EIO;
260 goto exit;
261 }
262
263 gb_len = resp->len - sizeof(struct digital_atr_res);
264
265 atr_res = (struct digital_atr_res *)resp->data;
266
267 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
268 if (rc)
269 goto exit;
270
271 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
272 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
273 rc = digital_in_send_psl_req(ddev, target);
274 if (!rc)
275 goto exit;
276 }
277
278 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
279 NFC_RF_INITIATOR);
280
281 ddev->curr_nfc_dep_pni = 0;
282
283 exit:
284 dev_kfree_skb(resp);
285
286 if (rc)
287 ddev->curr_protocol = 0;
288 }
289
290 int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
291 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
292 size_t gb_len)
293 {
294 struct sk_buff *skb;
295 struct digital_atr_req *atr_req;
296 uint size;
297 int rc;
298
299 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
300
301 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
302 PROTOCOL_ERR("14.6.1.1");
303 return -EINVAL;
304 }
305
306 skb = digital_skb_alloc(ddev, size);
307 if (!skb)
308 return -ENOMEM;
309
310 skb_put(skb, sizeof(struct digital_atr_req));
311
312 atr_req = (struct digital_atr_req *)skb->data;
313 memset(atr_req, 0, sizeof(struct digital_atr_req));
314
315 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
316 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
317 if (target->nfcid2_len)
318 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
319 else
320 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
321
322 atr_req->did = 0;
323 atr_req->bs = 0;
324 atr_req->br = 0;
325
326 atr_req->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
327
328 if (gb_len) {
329 atr_req->pp |= DIGITAL_GB_BIT;
330 memcpy(skb_put(skb, gb_len), gb, gb_len);
331 }
332
333 digital_skb_push_dep_sod(ddev, skb);
334
335 ddev->skb_add_crc(skb);
336
337 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res,
338 target);
339 if (rc)
340 kfree_skb(skb);
341
342 return rc;
343 }
344
345 static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
346 struct digital_data_exch *data_exch, u8 rtox)
347 {
348 struct digital_dep_req_res *dep_req;
349 struct sk_buff *skb;
350 int rc;
351
352 skb = digital_skb_alloc(ddev, 1);
353 if (!skb)
354 return -ENOMEM;
355
356 *skb_put(skb, 1) = rtox;
357
358 skb_push(skb, sizeof(struct digital_dep_req_res));
359
360 dep_req = (struct digital_dep_req_res *)skb->data;
361
362 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
363 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
364 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
365 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
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, 1500, digital_in_recv_dep_res,
372 data_exch);
373 if (rc)
374 kfree_skb(skb);
375
376 return rc;
377 }
378
379 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
380 struct sk_buff *resp)
381 {
382 struct digital_data_exch *data_exch = arg;
383 struct digital_dep_req_res *dep_res;
384 u8 pfb;
385 uint size;
386 int rc;
387
388 if (IS_ERR(resp)) {
389 rc = PTR_ERR(resp);
390 resp = NULL;
391 goto exit;
392 }
393
394 rc = ddev->skb_check_crc(resp);
395 if (rc) {
396 PROTOCOL_ERR("14.4.1.6");
397 goto error;
398 }
399
400 rc = digital_skb_pull_dep_sod(ddev, resp);
401 if (rc) {
402 PROTOCOL_ERR("14.4.1.2");
403 goto exit;
404 }
405
406 size = sizeof(struct digital_dep_req_res);
407 dep_res = (struct digital_dep_req_res *)resp->data;
408
409 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
410 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
411 rc = -EIO;
412 goto error;
413 }
414
415 pfb = dep_res->pfb;
416
417 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
418 PROTOCOL_ERR("14.8.2.1");
419 rc = -EIO;
420 goto error;
421 }
422
423 if (size > resp->len) {
424 rc = -EIO;
425 goto error;
426 }
427
428 skb_pull(resp, size);
429
430 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
431 case DIGITAL_NFC_DEP_PFB_I_PDU:
432 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
433 PROTOCOL_ERR("14.12.3.3");
434 rc = -EIO;
435 goto error;
436 }
437
438 ddev->curr_nfc_dep_pni =
439 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
440 rc = 0;
441 break;
442
443 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
444 pr_err("Received a ACK/NACK PDU\n");
445 rc = -EIO;
446 goto error;
447
448 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
449 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
450 rc = -EINVAL;
451 goto error;
452 }
453
454 rc = digital_in_send_rtox(ddev, data_exch, resp->data[0]);
455 if (rc)
456 goto error;
457
458 kfree_skb(resp);
459 return;
460 }
461
462 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
463 pr_err("MI bit set. Chained PDU not supported\n");
464 rc = -EIO;
465 goto error;
466 }
467
468 exit:
469 data_exch->cb(data_exch->cb_context, resp, rc);
470
471 error:
472 kfree(data_exch);
473
474 if (rc)
475 kfree_skb(resp);
476 }
477
478 int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
479 struct nfc_target *target, struct sk_buff *skb,
480 struct digital_data_exch *data_exch)
481 {
482 struct digital_dep_req_res *dep_req;
483
484 skb_push(skb, sizeof(struct digital_dep_req_res));
485
486 dep_req = (struct digital_dep_req_res *)skb->data;
487 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
488 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
489 dep_req->pfb = ddev->curr_nfc_dep_pni;
490
491 digital_skb_push_dep_sod(ddev, skb);
492
493 ddev->skb_add_crc(skb);
494
495 return digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
496 data_exch);
497 }
498
499 static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
500 {
501 ddev->curr_rf_tech = rf_tech;
502
503 ddev->skb_add_crc = digital_skb_add_crc_none;
504 ddev->skb_check_crc = digital_skb_check_crc_none;
505
506 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
507 return;
508
509 switch (ddev->curr_rf_tech) {
510 case NFC_DIGITAL_RF_TECH_106A:
511 ddev->skb_add_crc = digital_skb_add_crc_a;
512 ddev->skb_check_crc = digital_skb_check_crc_a;
513 break;
514
515 case NFC_DIGITAL_RF_TECH_212F:
516 case NFC_DIGITAL_RF_TECH_424F:
517 ddev->skb_add_crc = digital_skb_add_crc_f;
518 ddev->skb_check_crc = digital_skb_check_crc_f;
519 break;
520
521 default:
522 break;
523 }
524 }
525
526 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
527 struct sk_buff *resp)
528 {
529 int rc;
530 struct digital_dep_req_res *dep_req;
531 u8 pfb;
532 size_t size;
533
534 if (IS_ERR(resp)) {
535 rc = PTR_ERR(resp);
536 resp = NULL;
537 goto exit;
538 }
539
540 rc = ddev->skb_check_crc(resp);
541 if (rc) {
542 PROTOCOL_ERR("14.4.1.6");
543 goto exit;
544 }
545
546 rc = digital_skb_pull_dep_sod(ddev, resp);
547 if (rc) {
548 PROTOCOL_ERR("14.4.1.2");
549 goto exit;
550 }
551
552 size = sizeof(struct digital_dep_req_res);
553 dep_req = (struct digital_dep_req_res *)resp->data;
554
555 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
556 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
557 rc = -EIO;
558 goto exit;
559 }
560
561 pfb = dep_req->pfb;
562
563 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
564 if (ddev->did && (ddev->did == resp->data[3])) {
565 size++;
566 } else {
567 rc = -EIO;
568 goto exit;
569 }
570 } else if (ddev->did) {
571 rc = -EIO;
572 goto exit;
573 }
574
575 if (size > resp->len) {
576 rc = -EIO;
577 goto exit;
578 }
579
580 skb_pull(resp, size);
581
582 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
583 case DIGITAL_NFC_DEP_PFB_I_PDU:
584 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
585 ddev->curr_nfc_dep_pni = DIGITAL_NFC_DEP_PFB_PNI(pfb);
586 break;
587 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
588 pr_err("Received a ACK/NACK PDU\n");
589 rc = -EINVAL;
590 goto exit;
591 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
592 pr_err("Received a SUPERVISOR PDU\n");
593 rc = -EINVAL;
594 goto exit;
595 }
596
597 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
598
599 exit:
600 if (rc)
601 kfree_skb(resp);
602 }
603
604 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
605 {
606 struct digital_dep_req_res *dep_res;
607
608 skb_push(skb, sizeof(struct digital_dep_req_res));
609 dep_res = (struct digital_dep_req_res *)skb->data;
610
611 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
612 dep_res->cmd = DIGITAL_CMD_DEP_RES;
613 dep_res->pfb = ddev->curr_nfc_dep_pni;
614
615 if (ddev->did) {
616 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
617
618 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
619 sizeof(ddev->did));
620 }
621
622 digital_skb_push_dep_sod(ddev, skb);
623
624 ddev->skb_add_crc(skb);
625
626 return digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
627 NULL);
628 }
629
630 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
631 void *arg, struct sk_buff *resp)
632 {
633 u8 rf_tech = (unsigned long)arg;
634
635 if (IS_ERR(resp))
636 return;
637
638 digital_tg_set_rf_tech(ddev, rf_tech);
639
640 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
641
642 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
643
644 dev_kfree_skb(resp);
645 }
646
647 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
648 u8 rf_tech)
649 {
650 struct digital_psl_res *psl_res;
651 struct sk_buff *skb;
652 int rc;
653
654 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
655 if (!skb)
656 return -ENOMEM;
657
658 skb_put(skb, sizeof(struct digital_psl_res));
659
660 psl_res = (struct digital_psl_res *)skb->data;
661
662 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
663 psl_res->cmd = DIGITAL_CMD_PSL_RES;
664 psl_res->did = did;
665
666 digital_skb_push_dep_sod(ddev, skb);
667
668 ddev->skb_add_crc(skb);
669
670 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
671 (void *)(unsigned long)rf_tech);
672 if (rc)
673 kfree_skb(skb);
674
675 return rc;
676 }
677
678 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
679 struct sk_buff *resp)
680 {
681 int rc;
682 struct digital_psl_req *psl_req;
683 u8 rf_tech;
684 u8 dsi;
685
686 if (IS_ERR(resp)) {
687 rc = PTR_ERR(resp);
688 resp = NULL;
689 goto exit;
690 }
691
692 rc = ddev->skb_check_crc(resp);
693 if (rc) {
694 PROTOCOL_ERR("14.4.1.6");
695 goto exit;
696 }
697
698 rc = digital_skb_pull_dep_sod(ddev, resp);
699 if (rc) {
700 PROTOCOL_ERR("14.4.1.2");
701 goto exit;
702 }
703
704 psl_req = (struct digital_psl_req *)resp->data;
705
706 if (resp->len != sizeof(struct digital_psl_req) ||
707 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
708 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
709 rc = -EIO;
710 goto exit;
711 }
712
713 dsi = (psl_req->brs >> 3) & 0x07;
714 switch (dsi) {
715 case 0:
716 rf_tech = NFC_DIGITAL_RF_TECH_106A;
717 break;
718 case 1:
719 rf_tech = NFC_DIGITAL_RF_TECH_212F;
720 break;
721 case 2:
722 rf_tech = NFC_DIGITAL_RF_TECH_424F;
723 break;
724 default:
725 pr_err("Unsupported dsi value %d\n", dsi);
726 goto exit;
727 }
728
729 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
730
731 exit:
732 kfree_skb(resp);
733 }
734
735 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
736 void *arg, struct sk_buff *resp)
737 {
738 int offset;
739
740 if (IS_ERR(resp)) {
741 digital_poll_next_tech(ddev);
742 return;
743 }
744
745 offset = 2;
746 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
747 offset++;
748
749 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
750 digital_tg_recv_psl_req(ddev, arg, resp);
751 else
752 digital_tg_recv_dep_req(ddev, arg, resp);
753 }
754
755 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
756 struct digital_atr_req *atr_req)
757 {
758 struct digital_atr_res *atr_res;
759 struct sk_buff *skb;
760 u8 *gb;
761 size_t gb_len;
762 int rc;
763
764 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
765 if (!gb)
766 gb_len = 0;
767
768 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
769 if (!skb)
770 return -ENOMEM;
771
772 skb_put(skb, sizeof(struct digital_atr_res));
773 atr_res = (struct digital_atr_res *)skb->data;
774
775 memset(atr_res, 0, sizeof(struct digital_atr_res));
776
777 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
778 atr_res->cmd = DIGITAL_CMD_ATR_RES;
779 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
780 atr_res->to = 8;
781 atr_res->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
782 if (gb_len) {
783 skb_put(skb, gb_len);
784
785 atr_res->pp |= DIGITAL_GB_BIT;
786 memcpy(atr_res->gb, gb, gb_len);
787 }
788
789 digital_skb_push_dep_sod(ddev, skb);
790
791 ddev->skb_add_crc(skb);
792
793 rc = digital_tg_send_cmd(ddev, skb, 999,
794 digital_tg_send_atr_res_complete, NULL);
795 if (rc)
796 kfree_skb(skb);
797
798 return rc;
799 }
800
801 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
802 struct sk_buff *resp)
803 {
804 int rc;
805 struct digital_atr_req *atr_req;
806 size_t gb_len, min_size;
807 u8 poll_tech_count;
808
809 if (IS_ERR(resp)) {
810 rc = PTR_ERR(resp);
811 resp = NULL;
812 goto exit;
813 }
814
815 if (!resp->len) {
816 rc = -EIO;
817 goto exit;
818 }
819
820 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
821 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
822 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
823 } else {
824 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
825 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
826 }
827
828 if (resp->len < min_size) {
829 rc = -EIO;
830 goto exit;
831 }
832
833 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
834
835 rc = ddev->skb_check_crc(resp);
836 if (rc) {
837 PROTOCOL_ERR("14.4.1.6");
838 goto exit;
839 }
840
841 rc = digital_skb_pull_dep_sod(ddev, resp);
842 if (rc) {
843 PROTOCOL_ERR("14.4.1.2");
844 goto exit;
845 }
846
847 atr_req = (struct digital_atr_req *)resp->data;
848
849 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
850 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
851 atr_req->did > DIGITAL_DID_MAX) {
852 rc = -EINVAL;
853 goto exit;
854 }
855
856 ddev->did = atr_req->did;
857
858 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
859 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
860 if (rc)
861 goto exit;
862
863 rc = digital_tg_send_atr_res(ddev, atr_req);
864 if (rc)
865 goto exit;
866
867 gb_len = resp->len - sizeof(struct digital_atr_req);
868
869 poll_tech_count = ddev->poll_tech_count;
870 ddev->poll_tech_count = 0;
871
872 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
873 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
874 if (rc) {
875 ddev->poll_tech_count = poll_tech_count;
876 goto exit;
877 }
878
879 rc = 0;
880 exit:
881 if (rc)
882 digital_poll_next_tech(ddev);
883
884 dev_kfree_skb(resp);
885 }