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