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 return digital_in_send_cmd(ddev
, skb
, 500, digital_in_recv_atr_res
,
231 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
232 struct digital_data_exch
*data_exch
, u8 rtox
)
234 struct digital_dep_req_res
*dep_req
;
238 skb
= digital_skb_alloc(ddev
, 1);
242 *skb_put(skb
, 1) = rtox
;
244 skb_push(skb
, sizeof(struct digital_dep_req_res
));
246 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
248 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
249 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
250 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
251 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
253 digital_skb_push_dep_sod(ddev
, skb
);
255 ddev
->skb_add_crc(skb
);
257 rc
= digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
263 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
264 struct sk_buff
*resp
)
266 struct digital_data_exch
*data_exch
= arg
;
267 struct digital_dep_req_res
*dep_res
;
278 rc
= ddev
->skb_check_crc(resp
);
280 PROTOCOL_ERR("14.4.1.6");
284 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
286 PROTOCOL_ERR("14.4.1.2");
290 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
292 if (resp
->len
< sizeof(struct digital_dep_req_res
) ||
293 dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
294 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
301 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
302 case DIGITAL_NFC_DEP_PFB_I_PDU
:
303 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
304 PROTOCOL_ERR("14.12.3.3");
309 ddev
->curr_nfc_dep_pni
=
310 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
314 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
315 pr_err("Received a ACK/NACK PDU\n");
319 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
320 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
325 rc
= digital_in_send_rtox(ddev
, data_exch
, resp
->data
[3]);
333 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
334 pr_err("MI bit set. Chained PDU not supported\n");
339 size
= sizeof(struct digital_dep_req_res
);
341 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
))
344 if (size
> resp
->len
) {
349 skb_pull(resp
, size
);
352 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
361 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
362 struct nfc_target
*target
, struct sk_buff
*skb
,
363 struct digital_data_exch
*data_exch
)
365 struct digital_dep_req_res
*dep_req
;
367 skb_push(skb
, sizeof(struct digital_dep_req_res
));
369 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
370 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
371 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
372 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
374 digital_skb_push_dep_sod(ddev
, skb
);
376 ddev
->skb_add_crc(skb
);
378 return digital_in_send_cmd(ddev
, skb
, 1500, digital_in_recv_dep_res
,
382 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
384 ddev
->curr_rf_tech
= rf_tech
;
386 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
387 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
389 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
392 switch (ddev
->curr_rf_tech
) {
393 case NFC_DIGITAL_RF_TECH_106A
:
394 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
395 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
398 case NFC_DIGITAL_RF_TECH_212F
:
399 case NFC_DIGITAL_RF_TECH_424F
:
400 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
401 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
409 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
410 struct sk_buff
*resp
)
413 struct digital_dep_req_res
*dep_req
;
422 rc
= ddev
->skb_check_crc(resp
);
424 PROTOCOL_ERR("14.4.1.6");
428 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
430 PROTOCOL_ERR("14.4.1.2");
434 size
= sizeof(struct digital_dep_req_res
);
435 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
437 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
438 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
443 if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req
->pfb
))
446 if (resp
->len
< size
) {
451 switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req
->pfb
)) {
452 case DIGITAL_NFC_DEP_PFB_I_PDU
:
453 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
454 ddev
->curr_nfc_dep_pni
= DIGITAL_NFC_DEP_PFB_PNI(dep_req
->pfb
);
456 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
457 pr_err("Received a ACK/NACK PDU\n");
460 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
461 pr_err("Received a SUPERVISOR PDU\n");
466 skb_pull(resp
, size
);
468 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
475 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
477 struct digital_dep_req_res
*dep_res
;
479 skb_push(skb
, sizeof(struct digital_dep_req_res
));
480 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
482 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
483 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
484 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
486 digital_skb_push_dep_sod(ddev
, skb
);
488 ddev
->skb_add_crc(skb
);
490 return digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
494 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
495 void *arg
, struct sk_buff
*resp
)
497 u8 rf_tech
= (unsigned long)arg
;
502 digital_tg_set_rf_tech(ddev
, rf_tech
);
504 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
506 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
511 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
514 struct digital_psl_res
*psl_res
;
518 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
522 skb_put(skb
, sizeof(struct digital_psl_res
));
524 psl_res
= (struct digital_psl_res
*)skb
->data
;
526 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
527 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
530 digital_skb_push_dep_sod(ddev
, skb
);
532 ddev
->skb_add_crc(skb
);
534 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
535 (void *)(unsigned long)rf_tech
);
543 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
544 struct sk_buff
*resp
)
547 struct digital_psl_req
*psl_req
;
557 rc
= ddev
->skb_check_crc(resp
);
559 PROTOCOL_ERR("14.4.1.6");
563 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
565 PROTOCOL_ERR("14.4.1.2");
569 psl_req
= (struct digital_psl_req
*)resp
->data
;
571 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
572 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
573 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
578 dsi
= (psl_req
->brs
>> 3) & 0x07;
581 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
584 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
587 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
590 pr_err("Unsupported dsi value %d\n", dsi
);
594 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
600 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
601 void *arg
, struct sk_buff
*resp
)
606 digital_poll_next_tech(ddev
);
611 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
614 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
615 digital_tg_recv_psl_req(ddev
, arg
, resp
);
617 digital_tg_recv_dep_req(ddev
, arg
, resp
);
620 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
621 struct digital_atr_req
*atr_req
)
623 struct digital_atr_res
*atr_res
;
629 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
633 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
637 skb_put(skb
, sizeof(struct digital_atr_res
));
638 atr_res
= (struct digital_atr_res
*)skb
->data
;
640 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
642 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
643 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
644 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
646 atr_res
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
648 skb_put(skb
, gb_len
);
650 atr_res
->pp
|= DIGITAL_GB_BIT
;
651 memcpy(atr_res
->gb
, gb
, gb_len
);
654 digital_skb_push_dep_sod(ddev
, skb
);
656 ddev
->skb_add_crc(skb
);
658 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
659 digital_tg_send_atr_res_complete
, NULL
);
668 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
669 struct sk_buff
*resp
)
672 struct digital_atr_req
*atr_req
;
673 size_t gb_len
, min_size
;
687 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
688 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
689 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
691 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
692 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
695 if (resp
->len
< min_size
) {
700 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
702 rc
= ddev
->skb_check_crc(resp
);
704 PROTOCOL_ERR("14.4.1.6");
708 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
710 PROTOCOL_ERR("14.4.1.2");
714 atr_req
= (struct digital_atr_req
*)resp
->data
;
716 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
717 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
) {
722 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
723 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
727 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
731 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
733 poll_tech_count
= ddev
->poll_tech_count
;
734 ddev
->poll_tech_count
= 0;
736 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
737 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
739 ddev
->poll_tech_count
= poll_tech_count
;
746 digital_poll_next_tech(ddev
);