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_FSL_BITS_PAYLOAD_SIZE_254B \
37 (DIGITAL_LR_BITS_PAYLOAD_SIZE_254B >> 4)
38 #define DIGITAL_GB_BIT 0x02
40 #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
42 #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
44 #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
45 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
46 #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
47 #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
48 #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
49 #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
51 #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
52 #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
53 #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
55 struct digital_atr_req {
66 struct digital_atr_res {
78 struct digital_psl_req {
86 struct digital_psl_res {
92 struct digital_dep_req_res {
98 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
99 struct sk_buff *resp);
101 static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
104 skb_push(skb, sizeof(u8));
106 skb->data[0] = skb->len;
108 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
109 *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
112 static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
120 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
121 skb_pull(skb, sizeof(u8));
124 if (size != skb->len)
127 skb_pull(skb, sizeof(u8));
132 static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
133 struct sk_buff *resp)
135 struct nfc_target *target = arg;
136 struct digital_psl_res *psl_res;
145 rc = ddev->skb_check_crc(resp);
147 PROTOCOL_ERR("14.4.1.6");
151 rc = digital_skb_pull_dep_sod(ddev, resp);
153 PROTOCOL_ERR("14.4.1.2");
157 psl_res = (struct digital_psl_res *)resp->data;
159 if ((resp->len != sizeof(*psl_res)) ||
160 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
161 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
166 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
167 NFC_DIGITAL_RF_TECH_424F);
171 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
172 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
176 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
177 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
178 ddev->skb_add_crc = digital_skb_add_crc_f;
179 ddev->skb_check_crc = digital_skb_check_crc_f;
182 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
184 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
187 ddev->curr_nfc_dep_pni = 0;
193 ddev->curr_protocol = 0;
196 static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
197 struct nfc_target *target)
200 struct digital_psl_req *psl_req;
202 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
206 skb_put(skb, sizeof(*psl_req));
208 psl_req = (struct digital_psl_req *)skb->data;
210 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
211 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
213 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
214 psl_req->fsl = DIGITAL_FSL_BITS_PAYLOAD_SIZE_254B;
216 digital_skb_push_dep_sod(ddev, skb);
218 ddev->skb_add_crc(skb);
220 return digital_in_send_cmd(ddev, skb, 500, digital_in_recv_psl_res,
224 static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
225 struct sk_buff *resp)
227 struct nfc_target *target = arg;
228 struct digital_atr_res *atr_res;
238 rc = ddev->skb_check_crc(resp);
240 PROTOCOL_ERR("14.4.1.6");
244 rc = digital_skb_pull_dep_sod(ddev, resp);
246 PROTOCOL_ERR("14.4.1.2");
250 if (resp->len < sizeof(struct digital_atr_res)) {
255 gb_len = resp->len - sizeof(struct digital_atr_res);
257 atr_res = (struct digital_atr_res *)resp->data;
259 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
263 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
264 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
265 rc = digital_in_send_psl_req(ddev, target);
270 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
273 ddev->curr_nfc_dep_pni = 0;
279 ddev->curr_protocol = 0;
282 int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
283 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
287 struct digital_atr_req *atr_req;
290 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
292 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
293 PROTOCOL_ERR("14.6.1.1");
297 skb = digital_skb_alloc(ddev, size);
301 skb_put(skb, sizeof(struct digital_atr_req));
303 atr_req = (struct digital_atr_req *)skb->data;
304 memset(atr_req, 0, sizeof(struct digital_atr_req));
306 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
307 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
308 if (target->nfcid2_len)
309 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
311 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
317 atr_req->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
320 atr_req->pp |= DIGITAL_GB_BIT;
321 memcpy(skb_put(skb, gb_len), gb, gb_len);
324 digital_skb_push_dep_sod(ddev, skb);
326 ddev->skb_add_crc(skb);
328 return digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res,
332 static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
333 struct digital_data_exch *data_exch, u8 rtox)
335 struct digital_dep_req_res *dep_req;
339 skb = digital_skb_alloc(ddev, 1);
343 *skb_put(skb, 1) = rtox;
345 skb_push(skb, sizeof(struct digital_dep_req_res));
347 dep_req = (struct digital_dep_req_res *)skb->data;
349 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
350 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
351 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
352 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
354 digital_skb_push_dep_sod(ddev, skb);
356 ddev->skb_add_crc(skb);
358 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
364 static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
365 struct sk_buff *resp)
367 struct digital_data_exch *data_exch = arg;
368 struct digital_dep_req_res *dep_res;
379 rc = ddev->skb_check_crc(resp);
381 PROTOCOL_ERR("14.4.1.6");
385 rc = digital_skb_pull_dep_sod(ddev, resp);
387 PROTOCOL_ERR("14.4.1.2");
391 dep_res = (struct digital_dep_req_res *)resp->data;
393 if (resp->len < sizeof(struct digital_dep_req_res) ||
394 dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
395 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
402 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
403 case DIGITAL_NFC_DEP_PFB_I_PDU:
404 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
405 PROTOCOL_ERR("14.12.3.3");
410 ddev->curr_nfc_dep_pni =
411 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
415 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
416 pr_err("Received a ACK/NACK PDU\n");
420 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
421 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
426 rc = digital_in_send_rtox(ddev, data_exch, resp->data[3]);
434 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
435 pr_err("MI bit set. Chained PDU not supported\n");
440 size = sizeof(struct digital_dep_req_res);
442 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb))
445 if (size > resp->len) {
450 skb_pull(resp, size);
453 data_exch->cb(data_exch->cb_context, resp, rc);
462 int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
463 struct nfc_target *target, struct sk_buff *skb,
464 struct digital_data_exch *data_exch)
466 struct digital_dep_req_res *dep_req;
468 skb_push(skb, sizeof(struct digital_dep_req_res));
470 dep_req = (struct digital_dep_req_res *)skb->data;
471 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
472 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
473 dep_req->pfb = ddev->curr_nfc_dep_pni;
475 digital_skb_push_dep_sod(ddev, skb);
477 ddev->skb_add_crc(skb);
479 return digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
483 static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
485 ddev->curr_rf_tech = rf_tech;
487 ddev->skb_add_crc = digital_skb_add_crc_none;
488 ddev->skb_check_crc = digital_skb_check_crc_none;
490 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
493 switch (ddev->curr_rf_tech) {
494 case NFC_DIGITAL_RF_TECH_106A:
495 ddev->skb_add_crc = digital_skb_add_crc_a;
496 ddev->skb_check_crc = digital_skb_check_crc_a;
499 case NFC_DIGITAL_RF_TECH_212F:
500 case NFC_DIGITAL_RF_TECH_424F:
501 ddev->skb_add_crc = digital_skb_add_crc_f;
502 ddev->skb_check_crc = digital_skb_check_crc_f;
510 static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
511 struct sk_buff *resp)
514 struct digital_dep_req_res *dep_req;
523 rc = ddev->skb_check_crc(resp);
525 PROTOCOL_ERR("14.4.1.6");
529 rc = digital_skb_pull_dep_sod(ddev, resp);
531 PROTOCOL_ERR("14.4.1.2");
535 size = sizeof(struct digital_dep_req_res);
536 dep_req = (struct digital_dep_req_res *)resp->data;
538 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
539 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
544 if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req->pfb))
547 if (resp->len < size) {
552 switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
553 case DIGITAL_NFC_DEP_PFB_I_PDU:
554 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
555 ddev->curr_nfc_dep_pni = DIGITAL_NFC_DEP_PFB_PNI(dep_req->pfb);
557 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
558 pr_err("Received a ACK/NACK PDU\n");
561 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
562 pr_err("Received a SUPERVISOR PDU\n");
567 skb_pull(resp, size);
569 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
576 int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
578 struct digital_dep_req_res *dep_res;
580 skb_push(skb, sizeof(struct digital_dep_req_res));
581 dep_res = (struct digital_dep_req_res *)skb->data;
583 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
584 dep_res->cmd = DIGITAL_CMD_DEP_RES;
585 dep_res->pfb = ddev->curr_nfc_dep_pni;
587 digital_skb_push_dep_sod(ddev, skb);
589 ddev->skb_add_crc(skb);
591 return digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
595 static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
596 void *arg, struct sk_buff *resp)
598 u8 rf_tech = (unsigned long)arg;
603 digital_tg_set_rf_tech(ddev, rf_tech);
605 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
607 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
612 static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
615 struct digital_psl_res *psl_res;
619 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
623 skb_put(skb, sizeof(struct digital_psl_res));
625 psl_res = (struct digital_psl_res *)skb->data;
627 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
628 psl_res->cmd = DIGITAL_CMD_PSL_RES;
631 digital_skb_push_dep_sod(ddev, skb);
633 ddev->skb_add_crc(skb);
635 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
636 (void *)(unsigned long)rf_tech);
644 static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
645 struct sk_buff *resp)
648 struct digital_psl_req *psl_req;
658 rc = ddev->skb_check_crc(resp);
660 PROTOCOL_ERR("14.4.1.6");
664 rc = digital_skb_pull_dep_sod(ddev, resp);
666 PROTOCOL_ERR("14.4.1.2");
670 psl_req = (struct digital_psl_req *)resp->data;
672 if (resp->len != sizeof(struct digital_psl_req) ||
673 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
674 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
679 dsi = (psl_req->brs >> 3) & 0x07;
682 rf_tech = NFC_DIGITAL_RF_TECH_106A;
685 rf_tech = NFC_DIGITAL_RF_TECH_212F;
688 rf_tech = NFC_DIGITAL_RF_TECH_424F;
691 pr_err("Unsupported dsi value %d\n", dsi);
695 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
701 static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
702 void *arg, struct sk_buff *resp)
707 digital_poll_next_tech(ddev);
712 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
715 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
716 digital_tg_recv_psl_req(ddev, arg, resp);
718 digital_tg_recv_dep_req(ddev, arg, resp);
721 static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
722 struct digital_atr_req *atr_req)
724 struct digital_atr_res *atr_res;
730 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
734 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
738 skb_put(skb, sizeof(struct digital_atr_res));
739 atr_res = (struct digital_atr_res *)skb->data;
741 memset(atr_res, 0, sizeof(struct digital_atr_res));
743 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
744 atr_res->cmd = DIGITAL_CMD_ATR_RES;
745 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
747 atr_res->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
749 skb_put(skb, gb_len);
751 atr_res->pp |= DIGITAL_GB_BIT;
752 memcpy(atr_res->gb, gb, gb_len);
755 digital_skb_push_dep_sod(ddev, skb);
757 ddev->skb_add_crc(skb);
759 rc = digital_tg_send_cmd(ddev, skb, 999,
760 digital_tg_send_atr_res_complete, NULL);
769 void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
770 struct sk_buff *resp)
773 struct digital_atr_req *atr_req;
774 size_t gb_len, min_size;
788 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
789 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
790 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
792 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
793 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
796 if (resp->len < min_size) {
801 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
803 rc = ddev->skb_check_crc(resp);
805 PROTOCOL_ERR("14.4.1.6");
809 rc = digital_skb_pull_dep_sod(ddev, resp);
811 PROTOCOL_ERR("14.4.1.2");
815 atr_req = (struct digital_atr_req *)resp->data;
817 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
818 atr_req->cmd != DIGITAL_CMD_ATR_REQ) {
823 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
824 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
828 rc = digital_tg_send_atr_res(ddev, atr_req);
832 gb_len = resp->len - sizeof(struct digital_atr_req);
834 poll_tech_count = ddev->poll_tech_count;
835 ddev->poll_tech_count = 0;
837 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
838 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
840 ddev->poll_tech_count = poll_tech_count;
847 digital_poll_next_tech(ddev);