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
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
20 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
21 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
23 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
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
32 #define DIGITAL_ATR_REQ_MIN_SIZE 16
33 #define DIGITAL_ATR_REQ_MAX_SIZE 64
35 #define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
36 #define DIGITAL_GB_BIT 0x02
38 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
40 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
42 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
43 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
44 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
45 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
46 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
47 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
49 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
50 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
51 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
53 struct digital_atr_req
{
64 struct digital_atr_res
{
76 struct digital_psl_req
{
84 struct digital_psl_res
{
90 struct digital_dep_req_res
{
96 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
97 struct sk_buff
*resp
);
99 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
102 skb_push(skb
, sizeof(u8
));
104 skb
->data
[0] = skb
->len
;
106 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
107 *skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
110 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
118 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
119 skb_pull(skb
, sizeof(u8
));
122 if (size
!= skb
->len
)
125 skb_pull(skb
, sizeof(u8
));
130 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
131 struct sk_buff
*resp
)
133 struct nfc_target
*target
= arg
;
134 struct digital_atr_res
*atr_res
;
144 rc
= ddev
->skb_check_crc(resp
);
146 PROTOCOL_ERR("14.4.1.6");
150 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
152 PROTOCOL_ERR("14.4.1.2");
156 if (resp
->len
< sizeof(struct digital_atr_res
)) {
161 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
163 atr_res
= (struct digital_atr_res
*)resp
->data
;
165 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
169 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
172 ddev
->curr_nfc_dep_pni
= 0;
178 ddev
->curr_protocol
= 0;
181 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
182 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
186 struct digital_atr_req
*atr_req
;
189 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
191 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
192 PROTOCOL_ERR("14.6.1.1");
196 skb
= digital_skb_alloc(ddev
, size
);
200 skb_put(skb
, sizeof(struct digital_atr_req
));
202 atr_req
= (struct digital_atr_req
*)skb
->data
;
203 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
205 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
206 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
207 if (target
->nfcid2_len
)
208 memcpy(atr_req
->nfcid3
, target
->nfcid2
, NFC_NFCID2_MAXSIZE
);
210 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
216 atr_req
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
219 atr_req
->pp
|= DIGITAL_GB_BIT
;
220 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
223 digital_skb_push_dep_sod(ddev
, skb
);
225 ddev
->skb_add_crc(skb
);
227 digital_in_send_cmd(ddev
, skb
, 500, digital_in_recv_atr_res
, target
);
232 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
233 struct digital_data_exch
*data_exch
, u8 rtox
)
235 struct digital_dep_req_res
*dep_req
;
239 skb
= digital_skb_alloc(ddev
, 1);
243 *skb_put(skb
, 1) = rtox
;
245 skb_push(skb
, sizeof(struct digital_dep_req_res
));
247 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
249 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
250 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
251 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
252 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
254 digital_skb_push_dep_sod(ddev
, skb
);
256 ddev
->skb_add_crc(skb
);
258 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
264 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
265 struct sk_buff
*resp
)
267 struct digital_data_exch
*data_exch
= arg
;
268 struct digital_dep_req_res
*dep_res
;
279 rc
= ddev
->skb_check_crc(resp
);
281 PROTOCOL_ERR("14.4.1.6");
285 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
287 PROTOCOL_ERR("14.4.1.2");
291 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
293 if (resp
->len
< sizeof(struct digital_dep_req_res
) ||
294 dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
295 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
302 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
303 case DIGITAL_NFC_DEP_PFB_I_PDU
:
304 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
305 PROTOCOL_ERR("14.12.3.3");
310 ddev
->curr_nfc_dep_pni
=
311 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
315 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
316 pr_err("Received a ACK/NACK PDU\n");
320 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
321 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
326 rc
= digital_in_send_rtox(ddev
, data_exch
, resp
->data
[3]);
334 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
335 pr_err("MI bit set. Chained PDU not supported\n");
340 size
= sizeof(struct digital_dep_req_res
);
342 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
))
345 if (size
> resp
->len
) {
350 skb_pull(resp
, size
);
353 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
362 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
363 struct nfc_target
*target
, struct sk_buff
*skb
,
364 struct digital_data_exch
*data_exch
)
366 struct digital_dep_req_res
*dep_req
;
368 skb_push(skb
, sizeof(struct digital_dep_req_res
));
370 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
371 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
372 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
373 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
375 digital_skb_push_dep_sod(ddev
, skb
);
377 ddev
->skb_add_crc(skb
);
379 return digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
383 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
385 ddev
->curr_rf_tech
= rf_tech
;
387 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
388 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
390 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
393 switch (ddev
->curr_rf_tech
) {
394 case NFC_DIGITAL_RF_TECH_106A
:
395 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
396 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
399 case NFC_DIGITAL_RF_TECH_212F
:
400 case NFC_DIGITAL_RF_TECH_424F
:
401 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
402 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
410 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
411 struct sk_buff
*resp
)
414 struct digital_dep_req_res
*dep_req
;
423 rc
= ddev
->skb_check_crc(resp
);
425 PROTOCOL_ERR("14.4.1.6");
429 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
431 PROTOCOL_ERR("14.4.1.2");
435 size
= sizeof(struct digital_dep_req_res
);
436 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
438 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
439 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
444 if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req
->pfb
))
447 if (resp
->len
< size
) {
452 switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req
->pfb
)) {
453 case DIGITAL_NFC_DEP_PFB_I_PDU
:
454 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
455 ddev
->curr_nfc_dep_pni
= DIGITAL_NFC_DEP_PFB_PNI(dep_req
->pfb
);
457 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
458 pr_err("Received a ACK/NACK PDU\n");
462 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
463 pr_err("Received a SUPERVISOR PDU\n");
469 skb_pull(resp
, size
);
471 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
478 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
480 struct digital_dep_req_res
*dep_res
;
482 skb_push(skb
, sizeof(struct digital_dep_req_res
));
483 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
485 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
486 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
487 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
489 digital_skb_push_dep_sod(ddev
, skb
);
491 ddev
->skb_add_crc(skb
);
493 return digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
497 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
498 void *arg
, struct sk_buff
*resp
)
500 u8 rf_tech
= (unsigned long)arg
;
505 digital_tg_set_rf_tech(ddev
, rf_tech
);
507 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
509 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
514 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
517 struct digital_psl_res
*psl_res
;
521 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
525 skb_put(skb
, sizeof(struct digital_psl_res
));
527 psl_res
= (struct digital_psl_res
*)skb
->data
;
529 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
530 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
533 digital_skb_push_dep_sod(ddev
, skb
);
535 ddev
->skb_add_crc(skb
);
537 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
538 (void *)(unsigned long)rf_tech
);
546 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
547 struct sk_buff
*resp
)
550 struct digital_psl_req
*psl_req
;
560 rc
= ddev
->skb_check_crc(resp
);
562 PROTOCOL_ERR("14.4.1.6");
566 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
568 PROTOCOL_ERR("14.4.1.2");
572 psl_req
= (struct digital_psl_req
*)resp
->data
;
574 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
575 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
576 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
581 dsi
= (psl_req
->brs
>> 3) & 0x07;
584 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
587 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
590 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
593 pr_err("Unsupported dsi value %d\n", dsi
);
597 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
603 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
604 void *arg
, struct sk_buff
*resp
)
609 digital_poll_next_tech(ddev
);
614 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
617 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
618 digital_tg_recv_psl_req(ddev
, arg
, resp
);
620 digital_tg_recv_dep_req(ddev
, arg
, resp
);
623 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
624 struct digital_atr_req
*atr_req
)
626 struct digital_atr_res
*atr_res
;
632 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
636 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
640 skb_put(skb
, sizeof(struct digital_atr_res
));
641 atr_res
= (struct digital_atr_res
*)skb
->data
;
643 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
645 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
646 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
647 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
649 atr_res
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
651 skb_put(skb
, gb_len
);
653 atr_res
->pp
|= DIGITAL_GB_BIT
;
654 memcpy(atr_res
->gb
, gb
, gb_len
);
657 digital_skb_push_dep_sod(ddev
, skb
);
659 ddev
->skb_add_crc(skb
);
661 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
662 digital_tg_send_atr_res_complete
, NULL
);
671 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
672 struct sk_buff
*resp
)
675 struct digital_atr_req
*atr_req
;
676 size_t gb_len
, min_size
;
689 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
690 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
691 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
693 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
694 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
697 if (resp
->len
< min_size
) {
702 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
704 rc
= ddev
->skb_check_crc(resp
);
706 PROTOCOL_ERR("14.4.1.6");
710 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
712 PROTOCOL_ERR("14.4.1.2");
716 atr_req
= (struct digital_atr_req
*)resp
->data
;
718 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
719 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
) {
724 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
725 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
729 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
733 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
734 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
735 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
739 ddev
->poll_tech_count
= 0;
744 digital_poll_next_tech(ddev
);