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");
461 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
462 pr_err("Received a SUPERVISOR PDU\n");
468 skb_pull(resp
, size
);
470 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
477 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
479 struct digital_dep_req_res
*dep_res
;
481 skb_push(skb
, sizeof(struct digital_dep_req_res
));
482 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
484 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
485 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
486 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
488 digital_skb_push_dep_sod(ddev
, skb
);
490 ddev
->skb_add_crc(skb
);
492 return digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
496 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
497 void *arg
, struct sk_buff
*resp
)
499 u8 rf_tech
= (unsigned long)arg
;
504 digital_tg_set_rf_tech(ddev
, rf_tech
);
506 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
508 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
513 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
516 struct digital_psl_res
*psl_res
;
520 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
524 skb_put(skb
, sizeof(struct digital_psl_res
));
526 psl_res
= (struct digital_psl_res
*)skb
->data
;
528 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
529 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
532 digital_skb_push_dep_sod(ddev
, skb
);
534 ddev
->skb_add_crc(skb
);
536 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
537 (void *)(unsigned long)rf_tech
);
545 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
546 struct sk_buff
*resp
)
549 struct digital_psl_req
*psl_req
;
559 rc
= ddev
->skb_check_crc(resp
);
561 PROTOCOL_ERR("14.4.1.6");
565 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
567 PROTOCOL_ERR("14.4.1.2");
571 psl_req
= (struct digital_psl_req
*)resp
->data
;
573 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
574 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
575 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
580 dsi
= (psl_req
->brs
>> 3) & 0x07;
583 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
586 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
589 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
592 pr_err("Unsupported dsi value %d\n", dsi
);
596 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
602 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
603 void *arg
, struct sk_buff
*resp
)
608 digital_poll_next_tech(ddev
);
613 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
616 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
617 digital_tg_recv_psl_req(ddev
, arg
, resp
);
619 digital_tg_recv_dep_req(ddev
, arg
, resp
);
622 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
623 struct digital_atr_req
*atr_req
)
625 struct digital_atr_res
*atr_res
;
631 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
635 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
639 skb_put(skb
, sizeof(struct digital_atr_res
));
640 atr_res
= (struct digital_atr_res
*)skb
->data
;
642 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
644 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
645 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
646 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
648 atr_res
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
650 skb_put(skb
, gb_len
);
652 atr_res
->pp
|= DIGITAL_GB_BIT
;
653 memcpy(atr_res
->gb
, gb
, gb_len
);
656 digital_skb_push_dep_sod(ddev
, skb
);
658 ddev
->skb_add_crc(skb
);
660 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
661 digital_tg_send_atr_res_complete
, NULL
);
670 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
671 struct sk_buff
*resp
)
674 struct digital_atr_req
*atr_req
;
675 size_t gb_len
, min_size
;
688 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
689 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
690 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
692 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
693 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
696 if (resp
->len
< min_size
) {
701 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
703 rc
= ddev
->skb_check_crc(resp
);
705 PROTOCOL_ERR("14.4.1.6");
709 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
711 PROTOCOL_ERR("14.4.1.2");
715 atr_req
= (struct digital_atr_req
*)resp
->data
;
717 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
718 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
) {
723 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
724 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
728 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
732 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
733 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
734 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
738 ddev
->poll_tech_count
= 0;
743 digital_poll_next_tech(ddev
);