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
18 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
19 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
21 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
23 #define DIGITAL_CMD_ATR_REQ 0x00
24 #define DIGITAL_CMD_ATR_RES 0x01
25 #define DIGITAL_CMD_PSL_REQ 0x04
26 #define DIGITAL_CMD_PSL_RES 0x05
27 #define DIGITAL_CMD_DEP_REQ 0x06
28 #define DIGITAL_CMD_DEP_RES 0x07
30 #define DIGITAL_ATR_REQ_MIN_SIZE 16
31 #define DIGITAL_ATR_REQ_MAX_SIZE 64
33 #define DIGITAL_NFCID3_LEN ((u8)8)
34 #define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
35 #define DIGITAL_GB_BIT 0x02
37 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
39 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
41 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
42 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
43 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
44 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
45 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
46 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
48 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
49 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
50 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
52 struct digital_atr_req
{
63 struct digital_atr_res
{
75 struct digital_psl_req
{
83 struct digital_psl_res
{
89 struct digital_dep_req_res
{
95 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
96 struct sk_buff
*resp
);
98 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
101 skb_push(skb
, sizeof(u8
));
103 skb
->data
[0] = skb
->len
;
105 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
106 *skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
109 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
117 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
118 skb_pull(skb
, sizeof(u8
));
121 if (size
!= skb
->len
)
124 skb_pull(skb
, sizeof(u8
));
129 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
130 struct sk_buff
*resp
)
132 struct nfc_target
*target
= arg
;
133 struct digital_atr_res
*atr_res
;
143 rc
= ddev
->skb_check_crc(resp
);
145 PROTOCOL_ERR("14.4.1.6");
149 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
151 PROTOCOL_ERR("14.4.1.2");
155 if (resp
->len
< sizeof(struct digital_atr_res
)) {
160 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
162 atr_res
= (struct digital_atr_res
*)resp
->data
;
164 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
168 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
171 ddev
->curr_nfc_dep_pni
= 0;
177 ddev
->curr_protocol
= 0;
180 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
181 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
185 struct digital_atr_req
*atr_req
;
188 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
190 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
191 PROTOCOL_ERR("14.6.1.1");
195 skb
= digital_skb_alloc(ddev
, size
);
199 skb_put(skb
, sizeof(struct digital_atr_req
));
201 atr_req
= (struct digital_atr_req
*)skb
->data
;
202 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
204 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
205 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
206 if (target
->nfcid2_len
)
207 memcpy(atr_req
->nfcid3
, target
->nfcid2
,
208 max(target
->nfcid2_len
, DIGITAL_NFCID3_LEN
));
210 get_random_bytes(atr_req
->nfcid3
, DIGITAL_NFCID3_LEN
);
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");
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.");
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_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
384 struct sk_buff
*resp
)
387 struct digital_dep_req_res
*dep_req
;
396 rc
= ddev
->skb_check_crc(resp
);
398 PROTOCOL_ERR("14.4.1.6");
402 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
404 PROTOCOL_ERR("14.4.1.2");
408 size
= sizeof(struct digital_dep_req_res
);
409 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
411 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
412 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
417 if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req
->pfb
))
420 if (resp
->len
< size
) {
425 switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req
->pfb
)) {
426 case DIGITAL_NFC_DEP_PFB_I_PDU
:
427 PR_DBG("DIGITAL_NFC_DEP_PFB_I_PDU");
428 ddev
->curr_nfc_dep_pni
= DIGITAL_NFC_DEP_PFB_PNI(dep_req
->pfb
);
430 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
431 PR_ERR("Received a ACK/NACK PDU");
435 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
436 PR_ERR("Received a SUPERVISOR PDU");
442 skb_pull(resp
, size
);
444 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
451 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
453 struct digital_dep_req_res
*dep_res
;
455 skb_push(skb
, sizeof(struct digital_dep_req_res
));
456 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
458 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
459 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
460 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
462 digital_skb_push_dep_sod(ddev
, skb
);
464 ddev
->skb_add_crc(skb
);
466 return digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
470 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
471 void *arg
, struct sk_buff
*resp
)
473 u8 rf_tech
= PTR_ERR(arg
);
478 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
480 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
485 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
488 struct digital_psl_res
*psl_res
;
492 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
496 skb_put(skb
, sizeof(struct digital_psl_res
));
498 psl_res
= (struct digital_psl_res
*)skb
->data
;
500 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
501 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
504 digital_skb_push_dep_sod(ddev
, skb
);
506 ddev
->skb_add_crc(skb
);
508 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
517 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
518 struct sk_buff
*resp
)
521 struct digital_psl_req
*psl_req
;
531 rc
= ddev
->skb_check_crc(resp
);
533 PROTOCOL_ERR("14.4.1.6");
537 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
539 PROTOCOL_ERR("14.4.1.2");
543 psl_req
= (struct digital_psl_req
*)resp
->data
;
545 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
546 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
547 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
552 dsi
= (psl_req
->brs
>> 3) & 0x07;
555 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
558 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
561 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
564 PR_ERR("Unsuported dsi value %d", dsi
);
568 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
574 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
575 void *arg
, struct sk_buff
*resp
)
580 digital_poll_next_tech(ddev
);
585 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
588 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
589 digital_tg_recv_psl_req(ddev
, arg
, resp
);
591 digital_tg_recv_dep_req(ddev
, arg
, resp
);
594 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
595 struct digital_atr_req
*atr_req
)
597 struct digital_atr_res
*atr_res
;
603 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
607 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
611 skb_put(skb
, sizeof(struct digital_atr_res
));
612 atr_res
= (struct digital_atr_res
*)skb
->data
;
614 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
616 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
617 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
618 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
620 atr_res
->pp
= DIGITAL_LR_BITS_PAYLOAD_SIZE_254B
;
622 skb_put(skb
, gb_len
);
624 atr_res
->pp
|= DIGITAL_GB_BIT
;
625 memcpy(atr_res
->gb
, gb
, gb_len
);
628 digital_skb_push_dep_sod(ddev
, skb
);
630 ddev
->skb_add_crc(skb
);
632 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
633 digital_tg_send_atr_res_complete
, NULL
);
642 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
643 struct sk_buff
*resp
)
646 struct digital_atr_req
*atr_req
;
647 size_t gb_len
, min_size
;
660 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
661 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
663 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
664 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
665 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
667 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
669 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
670 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
671 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
674 if (resp
->len
< min_size
) {
679 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
)) {
680 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
681 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
684 rc
= ddev
->skb_check_crc(resp
);
686 PROTOCOL_ERR("14.4.1.6");
690 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
692 PROTOCOL_ERR("14.4.1.2");
696 atr_req
= (struct digital_atr_req
*)resp
->data
;
698 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
699 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
) {
704 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
705 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
709 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
713 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
714 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
715 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
719 ddev
->poll_tech_count
= 0;
724 digital_poll_next_tech(ddev
);