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_N_RETRY_NACK 2
21 #define DIGITAL_NFC_DEP_N_RETRY_ATN 2
23 #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
24 #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
26 #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
28 #define DIGITAL_CMD_ATR_REQ 0x00
29 #define DIGITAL_CMD_ATR_RES 0x01
30 #define DIGITAL_CMD_PSL_REQ 0x04
31 #define DIGITAL_CMD_PSL_RES 0x05
32 #define DIGITAL_CMD_DEP_REQ 0x06
33 #define DIGITAL_CMD_DEP_RES 0x07
35 #define DIGITAL_ATR_REQ_MIN_SIZE 16
36 #define DIGITAL_ATR_REQ_MAX_SIZE 64
38 #define DIGITAL_ATR_RES_TO_WT(s) ((s) & 0xF)
40 #define DIGITAL_DID_MAX 14
42 #define DIGITAL_PAYLOAD_SIZE_MAX 254
43 #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
44 #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
45 #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
46 #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
48 #define DIGITAL_GB_BIT 0x02
50 #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
51 #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
53 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
55 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
56 #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
57 #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
58 #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
60 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
61 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
62 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
63 #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
64 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
65 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
66 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
68 #define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
69 #define DIGITAL_NFC_DEP_RTOX_MAX 59
71 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
72 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
73 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
75 struct digital_atr_req
{
86 struct digital_atr_res
{
98 struct digital_psl_req
{
106 struct digital_psl_res
{
112 struct digital_dep_req_res
{
118 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
119 struct sk_buff
*resp
);
120 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
121 struct sk_buff
*resp
);
123 static const u8 digital_payload_bits_map
[4] = {
130 /* Response Waiting Time for ATR_RES PDU in ms
132 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
135 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
136 * dRWT(nfcdep) = 16 / f(c) s
137 * dT(nfcdep,initiator) = 100 ms
140 #define DIGITAL_ATR_RES_RWT 1337
142 /* Response Waiting Time for other DEP PDUs in ms
144 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
147 * rwt = (256 * 16 / f(c)) * 2^wt s
148 * dRWT(nfcdep) = 16 / f(c) s
149 * dT(nfcdep,initiator) = 100 ms
151 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
153 #define DIGITAL_NFC_DEP_IN_MAX_WT 14
154 #define DIGITAL_NFC_DEP_TG_MAX_WT 8
155 static const u16 digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
+ 1] = {
156 100, 101, 101, 102, 105,
157 110, 119, 139, 177, 255,
158 409, 719, 1337, 2575, 5049,
161 static u8
digital_payload_bits_to_size(u8 payload_bits
)
163 if (payload_bits
>= ARRAY_SIZE(digital_payload_bits_map
))
166 return digital_payload_bits_map
[payload_bits
];
169 static u8
digital_payload_size_to_bits(u8 payload_size
)
173 for (i
= 0; i
< ARRAY_SIZE(digital_payload_bits_map
); i
++)
174 if (digital_payload_bits_map
[i
] == payload_size
)
180 static void digital_skb_push_dep_sod(struct nfc_digital_dev
*ddev
,
183 skb_push(skb
, sizeof(u8
));
185 skb
->data
[0] = skb
->len
;
187 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
188 *skb_push(skb
, sizeof(u8
)) = DIGITAL_NFC_DEP_NFCA_SOD_SB
;
191 static int digital_skb_pull_dep_sod(struct nfc_digital_dev
*ddev
,
199 if (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)
200 skb_pull(skb
, sizeof(u8
));
203 if (size
!= skb
->len
)
206 skb_pull(skb
, sizeof(u8
));
211 static struct sk_buff
*
212 digital_send_dep_data_prep(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
,
213 struct digital_dep_req_res
*dep_req_res
,
214 struct digital_data_exch
*data_exch
)
216 struct sk_buff
*new_skb
;
218 if (skb
->len
> ddev
->remote_payload_max
) {
219 dep_req_res
->pfb
|= DIGITAL_NFC_DEP_PFB_MI_BIT
;
221 new_skb
= digital_skb_alloc(ddev
, ddev
->remote_payload_max
);
223 kfree_skb(ddev
->chaining_skb
);
224 ddev
->chaining_skb
= NULL
;
226 return ERR_PTR(-ENOMEM
);
229 memcpy(skb_put(new_skb
, ddev
->remote_payload_max
), skb
->data
,
230 ddev
->remote_payload_max
);
231 skb_pull(skb
, ddev
->remote_payload_max
);
233 ddev
->chaining_skb
= skb
;
234 ddev
->data_exch
= data_exch
;
236 ddev
->chaining_skb
= NULL
;
243 static struct sk_buff
*
244 digital_recv_dep_data_gather(struct nfc_digital_dev
*ddev
, u8 pfb
,
245 struct sk_buff
*resp
,
246 int (*send_ack
)(struct nfc_digital_dev
*ddev
,
247 struct digital_data_exch
249 struct digital_data_exch
*data_exch
)
251 struct sk_buff
*new_skb
;
254 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
) && (!ddev
->chaining_skb
)) {
256 nfc_alloc_recv_skb(8 * ddev
->local_payload_max
,
258 if (!ddev
->chaining_skb
) {
264 if (ddev
->chaining_skb
) {
265 if (resp
->len
> skb_tailroom(ddev
->chaining_skb
)) {
266 new_skb
= skb_copy_expand(ddev
->chaining_skb
,
269 8 * ddev
->local_payload_max
,
276 kfree_skb(ddev
->chaining_skb
);
277 ddev
->chaining_skb
= new_skb
;
280 memcpy(skb_put(ddev
->chaining_skb
, resp
->len
), resp
->data
,
286 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb
)) {
287 rc
= send_ack(ddev
, data_exch
);
294 resp
= ddev
->chaining_skb
;
295 ddev
->chaining_skb
= NULL
;
303 kfree_skb(ddev
->chaining_skb
);
304 ddev
->chaining_skb
= NULL
;
309 static void digital_in_recv_psl_res(struct nfc_digital_dev
*ddev
, void *arg
,
310 struct sk_buff
*resp
)
312 struct nfc_target
*target
= arg
;
313 struct digital_psl_res
*psl_res
;
322 rc
= ddev
->skb_check_crc(resp
);
324 PROTOCOL_ERR("14.4.1.6");
328 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
330 PROTOCOL_ERR("14.4.1.2");
334 psl_res
= (struct digital_psl_res
*)resp
->data
;
336 if ((resp
->len
!= sizeof(*psl_res
)) ||
337 (psl_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
) ||
338 (psl_res
->cmd
!= DIGITAL_CMD_PSL_RES
)) {
343 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
,
344 NFC_DIGITAL_RF_TECH_424F
);
348 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
349 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP
);
353 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
) &&
354 (ddev
->curr_rf_tech
== NFC_DIGITAL_RF_TECH_106A
)) {
355 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
356 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
359 ddev
->curr_rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
361 nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
364 ddev
->curr_nfc_dep_pni
= 0;
370 ddev
->curr_protocol
= 0;
373 static int digital_in_send_psl_req(struct nfc_digital_dev
*ddev
,
374 struct nfc_target
*target
)
377 struct digital_psl_req
*psl_req
;
379 u8 payload_size
, payload_bits
;
381 skb
= digital_skb_alloc(ddev
, sizeof(*psl_req
));
385 skb_put(skb
, sizeof(*psl_req
));
387 psl_req
= (struct digital_psl_req
*)skb
->data
;
389 psl_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
390 psl_req
->cmd
= DIGITAL_CMD_PSL_REQ
;
392 psl_req
->brs
= (0x2 << 3) | 0x2; /* 424F both directions */
394 payload_size
= min(ddev
->local_payload_max
, ddev
->remote_payload_max
);
395 payload_bits
= digital_payload_size_to_bits(payload_size
);
396 psl_req
->fsl
= DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits
);
398 ddev
->local_payload_max
= payload_size
;
399 ddev
->remote_payload_max
= payload_size
;
401 digital_skb_push_dep_sod(ddev
, skb
);
403 ddev
->skb_add_crc(skb
);
405 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
406 digital_in_recv_psl_res
, target
);
413 static void digital_in_recv_atr_res(struct nfc_digital_dev
*ddev
, void *arg
,
414 struct sk_buff
*resp
)
416 struct nfc_target
*target
= arg
;
417 struct digital_atr_res
*atr_res
;
418 u8 gb_len
, payload_bits
;
428 rc
= ddev
->skb_check_crc(resp
);
430 PROTOCOL_ERR("14.4.1.6");
434 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
436 PROTOCOL_ERR("14.4.1.2");
440 if (resp
->len
< sizeof(struct digital_atr_res
)) {
445 gb_len
= resp
->len
- sizeof(struct digital_atr_res
);
447 atr_res
= (struct digital_atr_res
*)resp
->data
;
449 wt
= DIGITAL_ATR_RES_TO_WT(atr_res
->to
);
450 if (wt
> DIGITAL_NFC_DEP_IN_MAX_WT
)
451 wt
= DIGITAL_NFC_DEP_IN_MAX_WT
;
452 ddev
->dep_rwt
= digital_rwt_map
[wt
];
454 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_res
->pp
);
455 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
457 if (!ddev
->remote_payload_max
) {
462 rc
= nfc_set_remote_general_bytes(ddev
->nfc_dev
, atr_res
->gb
, gb_len
);
466 if ((ddev
->protocols
& NFC_PROTO_FELICA_MASK
) &&
467 (ddev
->curr_rf_tech
!= NFC_DIGITAL_RF_TECH_424F
)) {
468 rc
= digital_in_send_psl_req(ddev
, target
);
473 rc
= nfc_dep_link_is_up(ddev
->nfc_dev
, target
->idx
, NFC_COMM_ACTIVE
,
476 ddev
->curr_nfc_dep_pni
= 0;
482 ddev
->curr_protocol
= 0;
485 int digital_in_send_atr_req(struct nfc_digital_dev
*ddev
,
486 struct nfc_target
*target
, __u8 comm_mode
, __u8
*gb
,
490 struct digital_atr_req
*atr_req
;
495 size
= DIGITAL_ATR_REQ_MIN_SIZE
+ gb_len
;
497 if (size
> DIGITAL_ATR_REQ_MAX_SIZE
) {
498 PROTOCOL_ERR("14.6.1.1");
502 skb
= digital_skb_alloc(ddev
, size
);
506 skb_put(skb
, sizeof(struct digital_atr_req
));
508 atr_req
= (struct digital_atr_req
*)skb
->data
;
509 memset(atr_req
, 0, sizeof(struct digital_atr_req
));
511 atr_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
512 atr_req
->cmd
= DIGITAL_CMD_ATR_REQ
;
513 if (target
->nfcid2_len
)
514 memcpy(atr_req
->nfcid3
, target
->nfcid2
, NFC_NFCID2_MAXSIZE
);
516 get_random_bytes(atr_req
->nfcid3
, NFC_NFCID3_MAXSIZE
);
522 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
523 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
524 atr_req
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
527 atr_req
->pp
|= DIGITAL_GB_BIT
;
528 memcpy(skb_put(skb
, gb_len
), gb
, gb_len
);
531 digital_skb_push_dep_sod(ddev
, skb
);
533 ddev
->skb_add_crc(skb
);
535 rc
= digital_in_send_cmd(ddev
, skb
, DIGITAL_ATR_RES_RWT
,
536 digital_in_recv_atr_res
, target
);
543 static int digital_in_send_ack(struct nfc_digital_dev
*ddev
,
544 struct digital_data_exch
*data_exch
)
546 struct digital_dep_req_res
*dep_req
;
550 skb
= digital_skb_alloc(ddev
, 1);
554 skb_push(skb
, sizeof(struct digital_dep_req_res
));
556 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
558 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
559 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
560 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
561 ddev
->curr_nfc_dep_pni
;
563 digital_skb_push_dep_sod(ddev
, skb
);
565 ddev
->skb_add_crc(skb
);
567 ddev
->saved_skb
= pskb_copy(skb
, GFP_KERNEL
);
569 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
570 digital_in_recv_dep_res
, data_exch
);
573 kfree_skb(ddev
->saved_skb
);
574 ddev
->saved_skb
= NULL
;
580 static int digital_in_send_nack(struct nfc_digital_dev
*ddev
,
581 struct digital_data_exch
*data_exch
)
583 struct digital_dep_req_res
*dep_req
;
587 skb
= digital_skb_alloc(ddev
, 1);
591 skb_push(skb
, sizeof(struct digital_dep_req_res
));
593 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
595 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
596 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
597 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
598 DIGITAL_NFC_DEP_PFB_NACK_BIT
| ddev
->curr_nfc_dep_pni
;
600 digital_skb_push_dep_sod(ddev
, skb
);
602 ddev
->skb_add_crc(skb
);
604 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
605 digital_in_recv_dep_res
, data_exch
);
612 static int digital_in_send_atn(struct nfc_digital_dev
*ddev
,
613 struct digital_data_exch
*data_exch
)
615 struct digital_dep_req_res
*dep_req
;
619 skb
= digital_skb_alloc(ddev
, 1);
623 skb_push(skb
, sizeof(struct digital_dep_req_res
));
625 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
627 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
628 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
629 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
631 digital_skb_push_dep_sod(ddev
, skb
);
633 ddev
->skb_add_crc(skb
);
635 rc
= digital_in_send_cmd(ddev
, skb
, ddev
->dep_rwt
,
636 digital_in_recv_dep_res
, data_exch
);
643 static int digital_in_send_rtox(struct nfc_digital_dev
*ddev
,
644 struct digital_data_exch
*data_exch
, u8 rtox
)
646 struct digital_dep_req_res
*dep_req
;
651 rwt_int
= ddev
->dep_rwt
* rtox
;
652 if (rwt_int
> digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
])
653 rwt_int
= digital_rwt_map
[DIGITAL_NFC_DEP_IN_MAX_WT
];
655 skb
= digital_skb_alloc(ddev
, 1);
659 *skb_put(skb
, 1) = rtox
;
661 skb_push(skb
, sizeof(struct digital_dep_req_res
));
663 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
665 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
666 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
667 dep_req
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
|
668 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT
;
670 digital_skb_push_dep_sod(ddev
, skb
);
672 ddev
->skb_add_crc(skb
);
674 rc
= digital_in_send_cmd(ddev
, skb
, rwt_int
,
675 digital_in_recv_dep_res
, data_exch
);
682 static int digital_in_send_saved_skb(struct nfc_digital_dev
*ddev
,
683 struct digital_data_exch
*data_exch
)
687 if (!ddev
->saved_skb
)
690 skb_get(ddev
->saved_skb
);
692 rc
= digital_in_send_cmd(ddev
, ddev
->saved_skb
, ddev
->dep_rwt
,
693 digital_in_recv_dep_res
, data_exch
);
695 kfree_skb(ddev
->saved_skb
);
700 static void digital_in_recv_dep_res(struct nfc_digital_dev
*ddev
, void *arg
,
701 struct sk_buff
*resp
)
703 struct digital_data_exch
*data_exch
= arg
;
704 struct digital_dep_req_res
*dep_res
;
714 if ((rc
== -EIO
|| (rc
== -ETIMEDOUT
&& ddev
->nack_count
)) &&
715 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
718 rc
= digital_in_send_nack(ddev
, data_exch
);
723 } else if ((rc
== -ETIMEDOUT
) &&
724 (ddev
->atn_count
++ < DIGITAL_NFC_DEP_N_RETRY_ATN
)) {
725 ddev
->nack_count
= 0;
727 rc
= digital_in_send_atn(ddev
, data_exch
);
737 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
739 PROTOCOL_ERR("14.4.1.2");
743 rc
= ddev
->skb_check_crc(resp
);
745 if ((resp
->len
>= 4) &&
746 (ddev
->nack_count
++ < DIGITAL_NFC_DEP_N_RETRY_NACK
)) {
749 rc
= digital_in_send_nack(ddev
, data_exch
);
758 PROTOCOL_ERR("14.4.1.6");
763 ddev
->nack_count
= 0;
765 if (resp
->len
> ddev
->local_payload_max
) {
770 size
= sizeof(struct digital_dep_req_res
);
771 dep_res
= (struct digital_dep_req_res
*)resp
->data
;
773 if (resp
->len
< size
|| dep_res
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_IN
||
774 dep_res
->cmd
!= DIGITAL_CMD_DEP_RES
) {
781 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
782 PROTOCOL_ERR("14.8.2.1");
787 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
792 if (size
> resp
->len
) {
797 skb_pull(resp
, size
);
799 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
800 case DIGITAL_NFC_DEP_PFB_I_PDU
:
801 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
802 PROTOCOL_ERR("14.12.3.3");
807 ddev
->curr_nfc_dep_pni
=
808 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
810 kfree_skb(ddev
->saved_skb
);
811 ddev
->saved_skb
= NULL
;
813 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
822 /* If resp is NULL then we're still chaining so return and
823 * wait for the next part of the PDU. Else, the PDU is
824 * complete so pass it up.
832 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
833 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) {
834 PROTOCOL_ERR("14.12.4.5");
839 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
840 PROTOCOL_ERR("14.12.3.3");
845 ddev
->curr_nfc_dep_pni
=
846 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
848 if (!ddev
->chaining_skb
) {
849 PROTOCOL_ERR("14.12.4.3");
854 /* The initiator has received a valid ACK. Free the last sent
855 * PDU and keep on sending chained skb.
857 kfree_skb(ddev
->saved_skb
);
858 ddev
->saved_skb
= NULL
;
860 rc
= digital_in_send_dep_req(ddev
, NULL
,
868 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
869 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) { /* ATN */
870 rc
= digital_in_send_saved_skb(ddev
, data_exch
);
877 if (ddev
->atn_count
|| ddev
->nack_count
) {
878 PROTOCOL_ERR("14.12.4.4");
883 rtox
= DIGITAL_NFC_DEP_RTOX_VALUE(resp
->data
[0]);
884 if (!rtox
|| rtox
> DIGITAL_NFC_DEP_RTOX_MAX
) {
885 PROTOCOL_ERR("14.8.4.1");
890 rc
= digital_in_send_rtox(ddev
, data_exch
, rtox
);
898 data_exch
->cb(data_exch
->cb_context
, resp
, rc
);
903 kfree_skb(ddev
->chaining_skb
);
904 ddev
->chaining_skb
= NULL
;
906 kfree_skb(ddev
->saved_skb
);
907 ddev
->saved_skb
= NULL
;
918 int digital_in_send_dep_req(struct nfc_digital_dev
*ddev
,
919 struct nfc_target
*target
, struct sk_buff
*skb
,
920 struct digital_data_exch
*data_exch
)
922 struct digital_dep_req_res
*dep_req
;
923 struct sk_buff
*chaining_skb
, *tmp_skb
;
926 skb_push(skb
, sizeof(struct digital_dep_req_res
));
928 dep_req
= (struct digital_dep_req_res
*)skb
->data
;
930 dep_req
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_OUT
;
931 dep_req
->cmd
= DIGITAL_CMD_DEP_REQ
;
932 dep_req
->pfb
= ddev
->curr_nfc_dep_pni
;
935 ddev
->nack_count
= 0;
937 chaining_skb
= ddev
->chaining_skb
;
939 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_req
, data_exch
);
941 return PTR_ERR(tmp_skb
);
943 digital_skb_push_dep_sod(ddev
, tmp_skb
);
945 ddev
->skb_add_crc(tmp_skb
);
947 ddev
->saved_skb
= pskb_copy(tmp_skb
, GFP_KERNEL
);
949 rc
= digital_in_send_cmd(ddev
, tmp_skb
, ddev
->dep_rwt
,
950 digital_in_recv_dep_res
, data_exch
);
955 kfree_skb(chaining_skb
);
956 ddev
->chaining_skb
= NULL
;
958 kfree_skb(ddev
->saved_skb
);
959 ddev
->saved_skb
= NULL
;
965 static void digital_tg_set_rf_tech(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
967 ddev
->curr_rf_tech
= rf_tech
;
969 ddev
->skb_add_crc
= digital_skb_add_crc_none
;
970 ddev
->skb_check_crc
= digital_skb_check_crc_none
;
972 if (DIGITAL_DRV_CAPS_TG_CRC(ddev
))
975 switch (ddev
->curr_rf_tech
) {
976 case NFC_DIGITAL_RF_TECH_106A
:
977 ddev
->skb_add_crc
= digital_skb_add_crc_a
;
978 ddev
->skb_check_crc
= digital_skb_check_crc_a
;
981 case NFC_DIGITAL_RF_TECH_212F
:
982 case NFC_DIGITAL_RF_TECH_424F
:
983 ddev
->skb_add_crc
= digital_skb_add_crc_f
;
984 ddev
->skb_check_crc
= digital_skb_check_crc_f
;
992 static int digital_tg_send_ack(struct nfc_digital_dev
*ddev
,
993 struct digital_data_exch
*data_exch
)
995 struct digital_dep_req_res
*dep_res
;
999 skb
= digital_skb_alloc(ddev
, 1);
1003 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1005 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1007 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1008 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1009 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
|
1010 ddev
->curr_nfc_dep_pni
;
1013 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1015 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
1019 ddev
->curr_nfc_dep_pni
=
1020 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1022 digital_skb_push_dep_sod(ddev
, skb
);
1024 ddev
->skb_add_crc(skb
);
1026 ddev
->saved_skb
= pskb_copy(skb
, GFP_KERNEL
);
1028 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1032 kfree_skb(ddev
->saved_skb
);
1033 ddev
->saved_skb
= NULL
;
1039 static int digital_tg_send_atn(struct nfc_digital_dev
*ddev
)
1041 struct digital_dep_req_res
*dep_res
;
1042 struct sk_buff
*skb
;
1045 skb
= digital_skb_alloc(ddev
, 1);
1049 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1051 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1053 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1054 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1055 dep_res
->pfb
= DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
;
1058 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1060 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
1064 digital_skb_push_dep_sod(ddev
, skb
);
1066 ddev
->skb_add_crc(skb
);
1068 rc
= digital_tg_send_cmd(ddev
, skb
, 1500, digital_tg_recv_dep_req
,
1076 static int digital_tg_send_saved_skb(struct nfc_digital_dev
*ddev
)
1080 if (!ddev
->saved_skb
)
1083 skb_get(ddev
->saved_skb
);
1085 rc
= digital_tg_send_cmd(ddev
, ddev
->saved_skb
, 1500,
1086 digital_tg_recv_dep_req
, NULL
);
1088 kfree_skb(ddev
->saved_skb
);
1093 static void digital_tg_recv_dep_req(struct nfc_digital_dev
*ddev
, void *arg
,
1094 struct sk_buff
*resp
)
1097 struct digital_dep_req_res
*dep_req
;
1107 rc
= ddev
->skb_check_crc(resp
);
1109 PROTOCOL_ERR("14.4.1.6");
1113 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1115 PROTOCOL_ERR("14.4.1.2");
1119 if (resp
->len
> ddev
->local_payload_max
) {
1124 size
= sizeof(struct digital_dep_req_res
);
1125 dep_req
= (struct digital_dep_req_res
*)resp
->data
;
1127 if (resp
->len
< size
|| dep_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1128 dep_req
->cmd
!= DIGITAL_CMD_DEP_REQ
) {
1135 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb
)) {
1136 if (ddev
->did
&& (ddev
->did
== resp
->data
[3])) {
1142 } else if (ddev
->did
) {
1147 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb
)) {
1152 if (size
> resp
->len
) {
1157 skb_pull(resp
, size
);
1159 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb
)) {
1160 case DIGITAL_NFC_DEP_PFB_I_PDU
:
1161 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1163 if (ddev
->atn_count
) {
1164 /* The target has received (and replied to) at least one
1167 ddev
->atn_count
= 0;
1169 /* pni of resp PDU equal to the target current pni - 1
1170 * means resp is the previous DEP_REQ PDU received from
1171 * the initiator so the target replies with saved_skb
1172 * which is the previous DEP_RES saved in
1173 * digital_tg_send_dep_res().
1175 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) ==
1176 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
- 1)) {
1177 rc
= digital_tg_send_saved_skb(ddev
);
1184 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1185 * means the target probably did not received the last
1186 * DEP_REQ PDU sent by the initiator. The target
1187 * fallbacks to normal processing then.
1191 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
) != ddev
->curr_nfc_dep_pni
) {
1192 PROTOCOL_ERR("14.12.3.4");
1197 kfree_skb(ddev
->saved_skb
);
1198 ddev
->saved_skb
= NULL
;
1200 resp
= digital_recv_dep_data_gather(ddev
, pfb
, resp
,
1201 digital_tg_send_ack
, NULL
);
1208 /* If resp is NULL then we're still chaining so return and
1209 * wait for the next part of the PDU. Else, the PDU is
1210 * complete so pass it up.
1217 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU
:
1218 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb
)) { /* NACK */
1219 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) !=
1220 ddev
->curr_nfc_dep_pni
) {
1225 ddev
->atn_count
= 0;
1227 rc
= digital_tg_send_saved_skb(ddev
);
1235 if (ddev
->atn_count
) {
1236 /* The target has previously recevied one or more ATN
1239 ddev
->atn_count
= 0;
1241 /* If the ACK PNI is equal to the target PNI - 1 means
1242 * that the initiator did not receive the previous PDU
1243 * sent by the target so re-send it.
1245 if (DIGITAL_NFC_DEP_PFB_PNI(pfb
+ 1) ==
1246 ddev
->curr_nfc_dep_pni
) {
1247 rc
= digital_tg_send_saved_skb(ddev
);
1254 /* Otherwise, the target did not receive the previous
1255 * ACK PDU from the initiator. Fallback to normal
1256 * processing of chained PDU then.
1260 /* Keep on sending chained PDU */
1261 if (!ddev
->chaining_skb
||
1262 DIGITAL_NFC_DEP_PFB_PNI(pfb
) !=
1263 ddev
->curr_nfc_dep_pni
) {
1268 kfree_skb(ddev
->saved_skb
);
1269 ddev
->saved_skb
= NULL
;
1271 rc
= digital_tg_send_dep_res(ddev
, ddev
->chaining_skb
);
1276 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU
:
1277 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb
)) {
1282 rc
= digital_tg_send_atn(ddev
);
1291 rc
= nfc_tm_data_received(ddev
->nfc_dev
, resp
);
1294 kfree_skb(ddev
->chaining_skb
);
1295 ddev
->chaining_skb
= NULL
;
1297 ddev
->atn_count
= 0;
1299 kfree_skb(ddev
->saved_skb
);
1300 ddev
->saved_skb
= NULL
;
1308 dev_kfree_skb(resp
);
1311 int digital_tg_send_dep_res(struct nfc_digital_dev
*ddev
, struct sk_buff
*skb
)
1313 struct digital_dep_req_res
*dep_res
;
1314 struct sk_buff
*chaining_skb
, *tmp_skb
;
1317 skb_push(skb
, sizeof(struct digital_dep_req_res
));
1319 dep_res
= (struct digital_dep_req_res
*)skb
->data
;
1321 dep_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1322 dep_res
->cmd
= DIGITAL_CMD_DEP_RES
;
1323 dep_res
->pfb
= ddev
->curr_nfc_dep_pni
;
1326 dep_res
->pfb
|= DIGITAL_NFC_DEP_PFB_DID_BIT
;
1328 memcpy(skb_put(skb
, sizeof(ddev
->did
)), &ddev
->did
,
1332 ddev
->curr_nfc_dep_pni
=
1333 DIGITAL_NFC_DEP_PFB_PNI(ddev
->curr_nfc_dep_pni
+ 1);
1335 chaining_skb
= ddev
->chaining_skb
;
1337 tmp_skb
= digital_send_dep_data_prep(ddev
, skb
, dep_res
, NULL
);
1338 if (IS_ERR(tmp_skb
))
1339 return PTR_ERR(tmp_skb
);
1341 digital_skb_push_dep_sod(ddev
, tmp_skb
);
1343 ddev
->skb_add_crc(tmp_skb
);
1345 ddev
->saved_skb
= pskb_copy(tmp_skb
, GFP_KERNEL
);
1347 rc
= digital_tg_send_cmd(ddev
, tmp_skb
, 1500, digital_tg_recv_dep_req
,
1353 kfree_skb(chaining_skb
);
1354 ddev
->chaining_skb
= NULL
;
1356 kfree_skb(ddev
->saved_skb
);
1357 ddev
->saved_skb
= NULL
;
1363 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev
*ddev
,
1364 void *arg
, struct sk_buff
*resp
)
1366 u8 rf_tech
= (unsigned long)arg
;
1371 digital_tg_set_rf_tech(ddev
, rf_tech
);
1373 digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
1375 digital_tg_listen(ddev
, 1500, digital_tg_recv_dep_req
, NULL
);
1377 dev_kfree_skb(resp
);
1380 static int digital_tg_send_psl_res(struct nfc_digital_dev
*ddev
, u8 did
,
1383 struct digital_psl_res
*psl_res
;
1384 struct sk_buff
*skb
;
1387 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_psl_res
));
1391 skb_put(skb
, sizeof(struct digital_psl_res
));
1393 psl_res
= (struct digital_psl_res
*)skb
->data
;
1395 psl_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1396 psl_res
->cmd
= DIGITAL_CMD_PSL_RES
;
1399 digital_skb_push_dep_sod(ddev
, skb
);
1401 ddev
->skb_add_crc(skb
);
1403 ddev
->curr_nfc_dep_pni
= 0;
1405 rc
= digital_tg_send_cmd(ddev
, skb
, 0, digital_tg_send_psl_res_complete
,
1406 (void *)(unsigned long)rf_tech
);
1413 static void digital_tg_recv_psl_req(struct nfc_digital_dev
*ddev
, void *arg
,
1414 struct sk_buff
*resp
)
1417 struct digital_psl_req
*psl_req
;
1419 u8 dsi
, payload_size
, payload_bits
;
1427 rc
= ddev
->skb_check_crc(resp
);
1429 PROTOCOL_ERR("14.4.1.6");
1433 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1435 PROTOCOL_ERR("14.4.1.2");
1439 psl_req
= (struct digital_psl_req
*)resp
->data
;
1441 if (resp
->len
!= sizeof(struct digital_psl_req
) ||
1442 psl_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1443 psl_req
->cmd
!= DIGITAL_CMD_PSL_REQ
) {
1448 dsi
= (psl_req
->brs
>> 3) & 0x07;
1451 rf_tech
= NFC_DIGITAL_RF_TECH_106A
;
1454 rf_tech
= NFC_DIGITAL_RF_TECH_212F
;
1457 rf_tech
= NFC_DIGITAL_RF_TECH_424F
;
1460 pr_err("Unsupported dsi value %d\n", dsi
);
1464 payload_bits
= DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req
->fsl
);
1465 payload_size
= digital_payload_bits_to_size(payload_bits
);
1467 if (!payload_size
|| (payload_size
> min(ddev
->local_payload_max
,
1468 ddev
->remote_payload_max
))) {
1473 ddev
->local_payload_max
= payload_size
;
1474 ddev
->remote_payload_max
= payload_size
;
1476 rc
= digital_tg_send_psl_res(ddev
, psl_req
->did
, rf_tech
);
1482 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev
*ddev
,
1483 void *arg
, struct sk_buff
*resp
)
1488 digital_poll_next_tech(ddev
);
1493 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
)
1496 ddev
->atn_count
= 0;
1498 if (resp
->data
[offset
] == DIGITAL_CMD_PSL_REQ
)
1499 digital_tg_recv_psl_req(ddev
, arg
, resp
);
1501 digital_tg_recv_dep_req(ddev
, arg
, resp
);
1504 static int digital_tg_send_atr_res(struct nfc_digital_dev
*ddev
,
1505 struct digital_atr_req
*atr_req
)
1507 struct digital_atr_res
*atr_res
;
1508 struct sk_buff
*skb
;
1509 u8
*gb
, payload_bits
;
1513 gb
= nfc_get_local_general_bytes(ddev
->nfc_dev
, &gb_len
);
1517 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_atr_res
) + gb_len
);
1521 skb_put(skb
, sizeof(struct digital_atr_res
));
1522 atr_res
= (struct digital_atr_res
*)skb
->data
;
1524 memset(atr_res
, 0, sizeof(struct digital_atr_res
));
1526 atr_res
->dir
= DIGITAL_NFC_DEP_FRAME_DIR_IN
;
1527 atr_res
->cmd
= DIGITAL_CMD_ATR_RES
;
1528 memcpy(atr_res
->nfcid3
, atr_req
->nfcid3
, sizeof(atr_req
->nfcid3
));
1529 atr_res
->to
= DIGITAL_NFC_DEP_TG_MAX_WT
;
1531 ddev
->local_payload_max
= DIGITAL_PAYLOAD_SIZE_MAX
;
1532 payload_bits
= digital_payload_size_to_bits(ddev
->local_payload_max
);
1533 atr_res
->pp
= DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits
);
1536 skb_put(skb
, gb_len
);
1538 atr_res
->pp
|= DIGITAL_GB_BIT
;
1539 memcpy(atr_res
->gb
, gb
, gb_len
);
1542 digital_skb_push_dep_sod(ddev
, skb
);
1544 ddev
->skb_add_crc(skb
);
1546 ddev
->curr_nfc_dep_pni
= 0;
1548 rc
= digital_tg_send_cmd(ddev
, skb
, 999,
1549 digital_tg_send_atr_res_complete
, NULL
);
1556 void digital_tg_recv_atr_req(struct nfc_digital_dev
*ddev
, void *arg
,
1557 struct sk_buff
*resp
)
1560 struct digital_atr_req
*atr_req
;
1561 size_t gb_len
, min_size
;
1562 u8 poll_tech_count
, payload_bits
;
1575 if (resp
->data
[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB
) {
1576 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 2;
1577 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_106A
);
1579 min_size
= DIGITAL_ATR_REQ_MIN_SIZE
+ 1;
1580 digital_tg_set_rf_tech(ddev
, NFC_DIGITAL_RF_TECH_212F
);
1583 if (resp
->len
< min_size
) {
1588 ddev
->curr_protocol
= NFC_PROTO_NFC_DEP_MASK
;
1590 rc
= ddev
->skb_check_crc(resp
);
1592 PROTOCOL_ERR("14.4.1.6");
1596 rc
= digital_skb_pull_dep_sod(ddev
, resp
);
1598 PROTOCOL_ERR("14.4.1.2");
1602 atr_req
= (struct digital_atr_req
*)resp
->data
;
1604 if (atr_req
->dir
!= DIGITAL_NFC_DEP_FRAME_DIR_OUT
||
1605 atr_req
->cmd
!= DIGITAL_CMD_ATR_REQ
||
1606 atr_req
->did
> DIGITAL_DID_MAX
) {
1611 payload_bits
= DIGITAL_PAYLOAD_PP_TO_BITS(atr_req
->pp
);
1612 ddev
->remote_payload_max
= digital_payload_bits_to_size(payload_bits
);
1614 if (!ddev
->remote_payload_max
) {
1619 ddev
->did
= atr_req
->did
;
1621 rc
= digital_tg_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
1622 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED
);
1626 rc
= digital_tg_send_atr_res(ddev
, atr_req
);
1630 gb_len
= resp
->len
- sizeof(struct digital_atr_req
);
1632 poll_tech_count
= ddev
->poll_tech_count
;
1633 ddev
->poll_tech_count
= 0;
1635 rc
= nfc_tm_activated(ddev
->nfc_dev
, NFC_PROTO_NFC_DEP_MASK
,
1636 NFC_COMM_PASSIVE
, atr_req
->gb
, gb_len
);
1638 ddev
->poll_tech_count
= poll_tech_count
;
1645 digital_poll_next_tech(ddev
);
1647 dev_kfree_skb(resp
);