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_CMD_SENS_REQ 0x26
21 #define DIGITAL_CMD_ALL_REQ 0x52
22 #define DIGITAL_CMD_SEL_REQ_CL1 0x93
23 #define DIGITAL_CMD_SEL_REQ_CL2 0x95
24 #define DIGITAL_CMD_SEL_REQ_CL3 0x97
26 #define DIGITAL_SDD_REQ_SEL_PAR 0x20
28 #define DIGITAL_SDD_RES_CT 0x88
29 #define DIGITAL_SDD_RES_LEN 5
31 #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
32 #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
33 #define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20)
34 #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
36 #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
37 #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
38 ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
39 (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
41 #define DIGITAL_MIFARE_READ_RES_LEN 16
42 #define DIGITAL_MIFARE_ACK_RES 0x0A
44 #define DIGITAL_CMD_SENSB_REQ 0x05
45 #define DIGITAL_SENSB_ADVANCED BIT(5)
46 #define DIGITAL_SENSB_EXTENDED BIT(4)
47 #define DIGITAL_SENSB_ALLB_REQ BIT(3)
48 #define DIGITAL_SENSB_N(n) ((n) & 0x7)
50 #define DIGITAL_CMD_SENSB_RES 0x50
52 #define DIGITAL_CMD_ATTRIB_REQ 0x1D
53 #define DIGITAL_ATTRIB_P1_TR0_DEFAULT (0x0 << 6)
54 #define DIGITAL_ATTRIB_P1_TR1_DEFAULT (0x0 << 4)
55 #define DIGITAL_ATTRIB_P1_SUPRESS_EOS BIT(3)
56 #define DIGITAL_ATTRIB_P1_SUPRESS_SOS BIT(2)
57 #define DIGITAL_ATTRIB_P2_LISTEN_POLL_1 (0x0 << 6)
58 #define DIGITAL_ATTRIB_P2_POLL_LISTEN_1 (0x0 << 4)
59 #define DIGITAL_ATTRIB_P2_MAX_FRAME_256 0x8
60 #define DIGITAL_ATTRIB_P4_DID(n) ((n) & 0xf)
62 #define DIGITAL_CMD_SENSF_REQ 0x00
63 #define DIGITAL_CMD_SENSF_RES 0x01
65 #define DIGITAL_SENSF_RES_MIN_LENGTH 17
66 #define DIGITAL_SENSF_RES_RD_AP_B1 0x00
67 #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F
69 #define DIGITAL_SENSF_REQ_RC_NONE 0
70 #define DIGITAL_SENSF_REQ_RC_SC 1
71 #define DIGITAL_SENSF_REQ_RC_AP 2
73 #define DIGITAL_CMD_ISO15693_INVENTORY_REQ 0x01
75 #define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE BIT(1)
76 #define DIGITAL_ISO15693_REQ_FLAG_INVENTORY BIT(2)
77 #define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
78 #define DIGITAL_ISO15693_RES_FLAG_ERROR BIT(0)
79 #define DIGITAL_ISO15693_RES_IS_VALID(flags) \
80 (!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR))
82 #define DIGITAL_ISO_DEP_I_PCB 0x02
83 #define DIGITAL_ISO_DEP_PNI(pni) ((pni) & 0x01)
85 #define DIGITAL_ISO_DEP_PCB_TYPE(pcb) ((pcb) & 0xC0)
87 #define DIGITAL_ISO_DEP_I_BLOCK 0x00
89 #define DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb) ((pcb) & 0x08)
91 static const u8 digital_ats_fsc[] = {
92 16, 24, 32, 40, 48, 64, 96, 128,
95 #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
96 #define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
97 #define DIGITAL_ATS_MAX_FSC 256
99 #define DIGITAL_RATS_BYTE1 0xE0
100 #define DIGITAL_RATS_PARAM 0x80
102 struct digital_sdd_res {
107 struct digital_sel_req {
114 struct digital_sensb_req {
120 struct digital_sensb_res {
127 struct digital_attrib_req {
136 struct digital_attrib_res {
140 struct digital_sensf_req {
148 struct digital_sensf_res {
159 struct digital_iso15693_inv_req {
166 struct digital_iso15693_inv_res {
172 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
173 struct nfc_target *target);
175 int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
185 block_type = DIGITAL_ISO_DEP_PCB_TYPE(pcb);
187 /* No support fo R-block nor S-block */
188 if (block_type != DIGITAL_ISO_DEP_I_BLOCK) {
189 pr_err("ISO_DEP R-block and S-block not supported\n");
193 if (DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb)) {
194 pr_err("DID field in ISO_DEP PCB not supported\n");
203 int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
207 * Chaining not supported so skb->len + 1 PCB byte + 2 CRC bytes must
208 * not be greater than remote FSC
210 if (skb->len + 3 > ddev->target_fsc)
215 *skb->data = DIGITAL_ISO_DEP_I_PCB | ddev->curr_nfc_dep_pni;
217 ddev->curr_nfc_dep_pni =
218 DIGITAL_ISO_DEP_PNI(ddev->curr_nfc_dep_pni + 1);
223 static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg,
224 struct sk_buff *resp)
226 struct nfc_target *target = arg;
241 fsdi = DIGITAL_ATS_FSCI(resp->data[1]);
243 ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
245 ddev->target_fsc = digital_ats_fsc[fsdi];
247 ddev->curr_nfc_dep_pni = 0;
249 rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443);
256 digital_poll_next_tech(ddev);
259 static int digital_in_send_rats(struct nfc_digital_dev *ddev,
260 struct nfc_target *target)
265 skb = digital_skb_alloc(ddev, 2);
269 *skb_put(skb, 1) = DIGITAL_RATS_BYTE1;
270 *skb_put(skb, 1) = DIGITAL_RATS_PARAM;
272 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats,
280 static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
281 struct sk_buff *resp)
283 struct nfc_target *target = arg;
294 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
295 rc = digital_skb_check_crc_a(resp);
297 PROTOCOL_ERR("4.4.1.3");
307 sel_res = resp->data[0];
309 if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
310 rc = digital_in_send_sdd_req(ddev, target);
317 target->sel_res = sel_res;
319 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
320 nfc_proto = NFC_PROTO_MIFARE;
321 } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
322 nfc_proto = NFC_PROTO_NFC_DEP;
323 } else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) {
324 rc = digital_in_send_rats(ddev, target);
328 * Skip target_found and don't free it for now. This will be
329 * done when receiving the ATS
337 rc = digital_target_found(ddev, target, nfc_proto);
346 digital_poll_next_tech(ddev);
349 static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
350 struct nfc_target *target,
351 struct digital_sdd_res *sdd_res)
354 struct digital_sel_req *sel_req;
358 skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
362 skb_put(skb, sizeof(struct digital_sel_req));
363 sel_req = (struct digital_sel_req *)skb->data;
365 if (target->nfcid1_len <= 4)
366 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
367 else if (target->nfcid1_len < 10)
368 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
370 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
372 sel_req->sel_cmd = sel_cmd;
374 memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
375 sel_req->bcc = sdd_res->bcc;
377 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
378 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
379 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
383 digital_skb_add_crc_a(skb);
386 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
395 static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
396 struct sk_buff *resp)
398 struct nfc_target *target = arg;
399 struct digital_sdd_res *sdd_res;
410 if (resp->len < DIGITAL_SDD_RES_LEN) {
411 PROTOCOL_ERR("4.7.2.8");
416 sdd_res = (struct digital_sdd_res *)resp->data;
418 for (i = 0, bcc = 0; i < 4; i++)
419 bcc ^= sdd_res->nfcid1[i];
421 if (bcc != sdd_res->bcc) {
422 PROTOCOL_ERR("4.7.2.6");
427 if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
435 memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
437 target->nfcid1_len += size;
439 rc = digital_in_send_sel_req(ddev, target, sdd_res);
446 digital_poll_next_tech(ddev);
450 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
451 struct nfc_target *target)
457 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
458 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
462 skb = digital_skb_alloc(ddev, 2);
466 if (target->nfcid1_len == 0)
467 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
468 else if (target->nfcid1_len == 3)
469 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
471 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
473 *skb_put(skb, sizeof(u8)) = sel_cmd;
474 *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR;
476 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
484 static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
485 struct sk_buff *resp)
487 struct nfc_target *target = NULL;
496 if (resp->len < sizeof(u16)) {
501 target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
507 target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
509 if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
510 PROTOCOL_ERR("4.6.3.3");
515 if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
516 rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
518 rc = digital_in_send_sdd_req(ddev, target);
525 digital_poll_next_tech(ddev);
529 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
534 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
535 NFC_DIGITAL_RF_TECH_106A);
539 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
540 NFC_DIGITAL_FRAMING_NFCA_SHORT);
544 skb = digital_skb_alloc(ddev, 1);
548 *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
550 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
557 int digital_in_recv_mifare_res(struct sk_buff *resp)
559 /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
560 * Since the driver can't differentiate a ACK/NACK response from a valid
561 * READ response, the CRC calculation must be handled at digital level
562 * even if the driver supports it for this technology.
564 if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
565 if (digital_skb_check_crc_a(resp)) {
566 PROTOCOL_ERR("9.4.1.2");
573 /* ACK response (i.e. successful WRITE). */
574 if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
579 /* NACK and any other responses are treated as error. */
583 static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
584 struct sk_buff *resp)
586 struct nfc_target *target = arg;
587 struct digital_attrib_res *attrib_res;
596 if (resp->len < sizeof(*attrib_res)) {
597 PROTOCOL_ERR("12.6.2");
602 attrib_res = (struct digital_attrib_res *)resp->data;
604 if (attrib_res->mbli_did & 0x0f) {
605 PROTOCOL_ERR("12.6.2.1");
610 rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
617 digital_poll_next_tech(ddev);
620 static int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
621 struct nfc_target *target,
622 struct digital_sensb_res *sensb_res)
624 struct digital_attrib_req *attrib_req;
628 skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
632 attrib_req = (struct digital_attrib_req *)skb_put(skb,
633 sizeof(*attrib_req));
635 attrib_req->cmd = DIGITAL_CMD_ATTRIB_REQ;
636 memcpy(attrib_req->nfcid0, sensb_res->nfcid0,
637 sizeof(attrib_req->nfcid0));
638 attrib_req->param1 = DIGITAL_ATTRIB_P1_TR0_DEFAULT |
639 DIGITAL_ATTRIB_P1_TR1_DEFAULT;
640 attrib_req->param2 = DIGITAL_ATTRIB_P2_LISTEN_POLL_1 |
641 DIGITAL_ATTRIB_P2_POLL_LISTEN_1 |
642 DIGITAL_ATTRIB_P2_MAX_FRAME_256;
643 attrib_req->param3 = sensb_res->proto_info[1] & 0x07;
644 attrib_req->param4 = DIGITAL_ATTRIB_P4_DID(0);
646 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_attrib_res,
654 static void digital_in_recv_sensb_res(struct nfc_digital_dev *ddev, void *arg,
655 struct sk_buff *resp)
657 struct nfc_target *target = NULL;
658 struct digital_sensb_res *sensb_res;
668 if (resp->len != sizeof(*sensb_res)) {
669 PROTOCOL_ERR("5.6.2.1");
674 sensb_res = (struct digital_sensb_res *)resp->data;
676 if (sensb_res->cmd != DIGITAL_CMD_SENSB_RES) {
677 PROTOCOL_ERR("5.6.2");
682 if (!(sensb_res->proto_info[1] & BIT(0))) {
683 PROTOCOL_ERR("5.6.2.12");
688 if (sensb_res->proto_info[1] & BIT(3)) {
689 PROTOCOL_ERR("5.6.2.16");
694 fsci = DIGITAL_SENSB_FSCI(sensb_res->proto_info[1]);
696 ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
698 ddev->target_fsc = digital_ats_fsc[fsci];
700 target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
706 rc = digital_in_send_attrib_req(ddev, target, sensb_res);
713 digital_poll_next_tech(ddev);
717 int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech)
719 struct digital_sensb_req *sensb_req;
723 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
724 NFC_DIGITAL_RF_TECH_106B);
728 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
729 NFC_DIGITAL_FRAMING_NFCB);
733 skb = digital_skb_alloc(ddev, sizeof(*sensb_req));
737 sensb_req = (struct digital_sensb_req *)skb_put(skb,
740 sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
741 sensb_req->afi = 0x00; /* All families and sub-families */
742 sensb_req->param = DIGITAL_SENSB_N(0);
744 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
752 static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
753 struct sk_buff *resp)
757 struct nfc_target target;
758 struct digital_sensf_res *sensf_res;
766 if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
771 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
772 rc = digital_skb_check_crc_f(resp);
774 PROTOCOL_ERR("6.4.1.8");
781 memset(&target, 0, sizeof(struct nfc_target));
783 sensf_res = (struct digital_sensf_res *)resp->data;
785 memcpy(target.sensf_res, sensf_res, resp->len);
786 target.sensf_res_len = resp->len;
788 memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
789 target.nfcid2_len = NFC_NFCID2_MAXSIZE;
791 if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
792 target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
793 proto = NFC_PROTO_NFC_DEP;
795 proto = NFC_PROTO_FELICA;
797 rc = digital_target_found(ddev, &target, proto);
803 digital_poll_next_tech(ddev);
806 int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
808 struct digital_sensf_req *sensf_req;
813 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
817 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
818 NFC_DIGITAL_FRAMING_NFCF);
822 size = sizeof(struct digital_sensf_req);
824 skb = digital_skb_alloc(ddev, size);
830 sensf_req = (struct digital_sensf_req *)skb->data;
831 sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
832 sensf_req->sc1 = 0xFF;
833 sensf_req->sc2 = 0xFF;
837 *skb_push(skb, 1) = size + 1;
839 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
840 digital_skb_add_crc_f(skb);
842 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
850 static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev,
851 void *arg, struct sk_buff *resp)
853 struct digital_iso15693_inv_res *res;
854 struct nfc_target *target = NULL;
863 if (resp->len != sizeof(*res)) {
868 res = (struct digital_iso15693_inv_res *)resp->data;
870 if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) {
871 PROTOCOL_ERR("ISO15693 - 10.3.1");
876 target = kzalloc(sizeof(*target), GFP_KERNEL);
882 target->is_iso15693 = 1;
883 target->iso15693_dsfid = res->dsfid;
884 memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid));
886 rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693);
894 digital_poll_next_tech(ddev);
897 int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech)
899 struct digital_iso15693_inv_req *req;
903 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
904 NFC_DIGITAL_RF_TECH_ISO15693);
908 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
909 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY);
913 skb = digital_skb_alloc(ddev, sizeof(*req));
917 skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */
918 req = (struct digital_iso15693_inv_req *)skb->data;
920 /* Single sub-carrier, high data rate, no AFI, single slot
923 req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE |
924 DIGITAL_ISO15693_REQ_FLAG_INVENTORY |
925 DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS;
926 req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ;
929 rc = digital_in_send_cmd(ddev, skb, 30,
930 digital_in_recv_iso15693_inv_res, NULL);
937 static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
942 skb = digital_skb_alloc(ddev, 1);
946 *skb_put(skb, 1) = DIGITAL_SEL_RES_NFC_DEP;
948 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
949 digital_skb_add_crc_a(skb);
951 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
952 NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE);
958 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
966 static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
967 struct sk_buff *resp)
977 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
978 rc = digital_skb_check_crc_a(resp);
980 PROTOCOL_ERR("4.4.1.3");
985 /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
987 rc = digital_tg_send_sel_res(ddev);
991 digital_poll_next_tech(ddev);
996 static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
999 struct digital_sdd_res *sdd_res;
1002 skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
1006 skb_put(skb, sizeof(struct digital_sdd_res));
1007 sdd_res = (struct digital_sdd_res *)skb->data;
1009 sdd_res->nfcid1[0] = 0x08;
1010 get_random_bytes(sdd_res->nfcid1 + 1, 3);
1013 for (i = 0; i < 4; i++)
1014 sdd_res->bcc ^= sdd_res->nfcid1[i];
1016 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1017 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
1023 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
1031 static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
1032 struct sk_buff *resp)
1043 sdd_req = resp->data;
1045 if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
1046 sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
1051 rc = digital_tg_send_sdd_res(ddev);
1055 digital_poll_next_tech(ddev);
1057 dev_kfree_skb(resp);
1060 static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
1062 struct sk_buff *skb;
1066 skb = digital_skb_alloc(ddev, 2);
1070 sens_res = skb_put(skb, 2);
1072 sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
1073 sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
1075 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1076 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
1082 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
1090 void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
1091 struct sk_buff *resp)
1102 sens_req = resp->data[0];
1104 if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
1105 sens_req != DIGITAL_CMD_ALL_REQ)) {
1110 rc = digital_tg_send_sens_res(ddev);
1114 digital_poll_next_tech(ddev);
1116 dev_kfree_skb(resp);
1119 static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
1120 void *arg, struct sk_buff *resp)
1122 if (!IS_ERR(resp) && (resp->len >= 2) &&
1123 (resp->data[1] == DIGITAL_CMD_SENSF_REQ))
1124 digital_tg_recv_sensf_req(ddev, arg, resp);
1126 digital_tg_recv_atr_req(ddev, arg, resp);
1131 static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
1132 struct digital_sensf_req *sensf_req)
1134 struct sk_buff *skb;
1137 struct digital_sensf_res *sensf_res;
1139 size = sizeof(struct digital_sensf_res);
1141 if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
1142 size -= sizeof(sensf_res->rd);
1144 skb = digital_skb_alloc(ddev, size);
1150 sensf_res = (struct digital_sensf_res *)skb->data;
1152 memset(sensf_res, 0, size);
1154 sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
1155 sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
1156 sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
1157 get_random_bytes(&sensf_res->nfcid2[2], 6);
1159 switch (sensf_req->rc) {
1160 case DIGITAL_SENSF_REQ_RC_SC:
1161 sensf_res->rd[0] = sensf_req->sc1;
1162 sensf_res->rd[1] = sensf_req->sc2;
1164 case DIGITAL_SENSF_REQ_RC_AP:
1165 sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
1166 sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
1170 *skb_push(skb, sizeof(u8)) = size + 1;
1172 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
1173 digital_skb_add_crc_f(skb);
1175 rc = digital_tg_send_cmd(ddev, skb, 300,
1176 digital_tg_recv_atr_or_sensf_req, NULL);
1183 void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
1184 struct sk_buff *resp)
1186 struct digital_sensf_req *sensf_req;
1195 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
1196 rc = digital_skb_check_crc_f(resp);
1198 PROTOCOL_ERR("6.4.1.8");
1203 if (resp->len != sizeof(struct digital_sensf_req) + 1) {
1209 sensf_req = (struct digital_sensf_req *)resp->data;
1211 if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
1216 rc = digital_tg_send_sensf_res(ddev, sensf_req);
1220 digital_poll_next_tech(ddev);
1222 dev_kfree_skb(resp);
1225 static int digital_tg_config_nfca(struct nfc_digital_dev *ddev)
1229 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1230 NFC_DIGITAL_RF_TECH_106A);
1234 return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1235 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1238 int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
1242 rc = digital_tg_config_nfca(ddev);
1246 return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
1249 static int digital_tg_config_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1253 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1257 return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1258 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
1261 int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1265 rc = digital_tg_config_nfcf(ddev, rf_tech);
1269 return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, NULL);
1272 void digital_tg_recv_md_req(struct nfc_digital_dev *ddev, void *arg,
1273 struct sk_buff *resp)
1283 rc = ddev->ops->tg_get_rf_tech(ddev, &rf_tech);
1288 case NFC_DIGITAL_RF_TECH_106A:
1289 rc = digital_tg_config_nfca(ddev);
1292 digital_tg_recv_sens_req(ddev, arg, resp);
1294 case NFC_DIGITAL_RF_TECH_212F:
1295 case NFC_DIGITAL_RF_TECH_424F:
1296 rc = digital_tg_config_nfcf(ddev, rf_tech);
1299 digital_tg_recv_sensf_req(ddev, arg, resp);
1308 digital_poll_next_tech(ddev);
1309 dev_kfree_skb(resp);