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
30 #define DIGITAL_SEL_RES_LEN 1
32 #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
33 #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
34 #define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20)
35 #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
37 #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
38 #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
39 ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
40 (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
42 #define DIGITAL_MIFARE_READ_RES_LEN 16
43 #define DIGITAL_MIFARE_ACK_RES 0x0A
45 #define DIGITAL_CMD_SENSB_REQ 0x05
46 #define DIGITAL_SENSB_ADVANCED BIT(5)
47 #define DIGITAL_SENSB_EXTENDED BIT(4)
48 #define DIGITAL_SENSB_ALLB_REQ BIT(3)
49 #define DIGITAL_SENSB_N(n) ((n) & 0x7)
51 #define DIGITAL_CMD_SENSB_RES 0x50
53 #define DIGITAL_CMD_ATTRIB_REQ 0x1D
54 #define DIGITAL_ATTRIB_P1_TR0_DEFAULT (0x0 << 6)
55 #define DIGITAL_ATTRIB_P1_TR1_DEFAULT (0x0 << 4)
56 #define DIGITAL_ATTRIB_P1_SUPRESS_EOS BIT(3)
57 #define DIGITAL_ATTRIB_P1_SUPRESS_SOS BIT(2)
58 #define DIGITAL_ATTRIB_P2_LISTEN_POLL_1 (0x0 << 6)
59 #define DIGITAL_ATTRIB_P2_POLL_LISTEN_1 (0x0 << 4)
60 #define DIGITAL_ATTRIB_P2_MAX_FRAME_256 0x8
61 #define DIGITAL_ATTRIB_P4_DID(n) ((n) & 0xf)
63 #define DIGITAL_CMD_SENSF_REQ 0x00
64 #define DIGITAL_CMD_SENSF_RES 0x01
66 #define DIGITAL_SENSF_RES_MIN_LENGTH 17
67 #define DIGITAL_SENSF_RES_RD_AP_B1 0x00
68 #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F
70 #define DIGITAL_SENSF_REQ_RC_NONE 0
71 #define DIGITAL_SENSF_REQ_RC_SC 1
72 #define DIGITAL_SENSF_REQ_RC_AP 2
74 #define DIGITAL_CMD_ISO15693_INVENTORY_REQ 0x01
76 #define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE BIT(1)
77 #define DIGITAL_ISO15693_REQ_FLAG_INVENTORY BIT(2)
78 #define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS BIT(5)
79 #define DIGITAL_ISO15693_RES_FLAG_ERROR BIT(0)
80 #define DIGITAL_ISO15693_RES_IS_VALID(flags) \
81 (!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR))
83 #define DIGITAL_ISO_DEP_I_PCB 0x02
84 #define DIGITAL_ISO_DEP_PNI(pni) ((pni) & 0x01)
86 #define DIGITAL_ISO_DEP_PCB_TYPE(pcb) ((pcb) & 0xC0)
88 #define DIGITAL_ISO_DEP_I_BLOCK 0x00
90 #define DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb) ((pcb) & 0x08)
92 static const u8 digital_ats_fsc[] = {
93 16, 24, 32, 40, 48, 64, 96, 128,
96 #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
97 #define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
98 #define DIGITAL_ATS_MAX_FSC 256
100 #define DIGITAL_RATS_BYTE1 0xE0
101 #define DIGITAL_RATS_PARAM 0x80
103 struct digital_sdd_res {
108 struct digital_sel_req {
115 struct digital_sensb_req {
121 struct digital_sensb_res {
128 struct digital_attrib_req {
137 struct digital_attrib_res {
141 struct digital_sensf_req {
149 struct digital_sensf_res {
160 struct digital_iso15693_inv_req {
167 struct digital_iso15693_inv_res {
173 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
174 struct nfc_target *target);
176 int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
186 block_type = DIGITAL_ISO_DEP_PCB_TYPE(pcb);
188 /* No support fo R-block nor S-block */
189 if (block_type != DIGITAL_ISO_DEP_I_BLOCK) {
190 pr_err("ISO_DEP R-block and S-block not supported\n");
194 if (DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb)) {
195 pr_err("DID field in ISO_DEP PCB not supported\n");
204 int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
208 * Chaining not supported so skb->len + 1 PCB byte + 2 CRC bytes must
209 * not be greater than remote FSC
211 if (skb->len + 3 > ddev->target_fsc)
216 *skb->data = DIGITAL_ISO_DEP_I_PCB | ddev->curr_nfc_dep_pni;
218 ddev->curr_nfc_dep_pni =
219 DIGITAL_ISO_DEP_PNI(ddev->curr_nfc_dep_pni + 1);
224 static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg,
225 struct sk_buff *resp)
227 struct nfc_target *target = arg;
242 fsdi = DIGITAL_ATS_FSCI(resp->data[1]);
244 ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
246 ddev->target_fsc = digital_ats_fsc[fsdi];
248 ddev->curr_nfc_dep_pni = 0;
250 rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443);
257 digital_poll_next_tech(ddev);
260 static int digital_in_send_rats(struct nfc_digital_dev *ddev,
261 struct nfc_target *target)
266 skb = digital_skb_alloc(ddev, 2);
270 skb_put_u8(skb, DIGITAL_RATS_BYTE1);
271 skb_put_u8(skb, DIGITAL_RATS_PARAM);
273 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats,
281 static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
282 struct sk_buff *resp)
284 struct nfc_target *target = arg;
295 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
296 rc = digital_skb_check_crc_a(resp);
298 PROTOCOL_ERR("4.4.1.3");
303 if (resp->len != DIGITAL_SEL_RES_LEN) {
308 sel_res = resp->data[0];
310 if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
311 rc = digital_in_send_sdd_req(ddev, target);
318 target->sel_res = sel_res;
320 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
321 nfc_proto = NFC_PROTO_MIFARE;
322 } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
323 nfc_proto = NFC_PROTO_NFC_DEP;
324 } else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) {
325 rc = digital_in_send_rats(ddev, target);
329 * Skip target_found and don't free it for now. This will be
330 * done when receiving the ATS
338 rc = digital_target_found(ddev, target, nfc_proto);
347 digital_poll_next_tech(ddev);
350 static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
351 struct nfc_target *target,
352 struct digital_sdd_res *sdd_res)
355 struct digital_sel_req *sel_req;
359 skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
363 skb_put(skb, sizeof(struct digital_sel_req));
364 sel_req = (struct digital_sel_req *)skb->data;
366 if (target->nfcid1_len <= 4)
367 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
368 else if (target->nfcid1_len < 10)
369 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
371 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
373 sel_req->sel_cmd = sel_cmd;
375 memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
376 sel_req->bcc = sdd_res->bcc;
378 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
379 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
380 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
384 digital_skb_add_crc_a(skb);
387 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
396 static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
397 struct sk_buff *resp)
399 struct nfc_target *target = arg;
400 struct digital_sdd_res *sdd_res;
411 if (resp->len < DIGITAL_SDD_RES_LEN) {
412 PROTOCOL_ERR("4.7.2.8");
417 sdd_res = (struct digital_sdd_res *)resp->data;
419 for (i = 0, bcc = 0; i < 4; i++)
420 bcc ^= sdd_res->nfcid1[i];
422 if (bcc != sdd_res->bcc) {
423 PROTOCOL_ERR("4.7.2.6");
428 if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
436 memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
438 target->nfcid1_len += size;
440 rc = digital_in_send_sel_req(ddev, target, sdd_res);
447 digital_poll_next_tech(ddev);
451 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
452 struct nfc_target *target)
458 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
459 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
463 skb = digital_skb_alloc(ddev, 2);
467 if (target->nfcid1_len == 0)
468 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
469 else if (target->nfcid1_len == 3)
470 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
472 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
474 skb_put_u8(skb, sel_cmd);
475 skb_put_u8(skb, DIGITAL_SDD_REQ_SEL_PAR);
477 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
485 static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
486 struct sk_buff *resp)
488 struct nfc_target *target = NULL;
497 if (resp->len < sizeof(u16)) {
502 target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
508 target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
510 if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
511 PROTOCOL_ERR("4.6.3.3");
516 if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
517 rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
519 rc = digital_in_send_sdd_req(ddev, target);
526 digital_poll_next_tech(ddev);
530 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
535 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
536 NFC_DIGITAL_RF_TECH_106A);
540 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
541 NFC_DIGITAL_FRAMING_NFCA_SHORT);
545 skb = digital_skb_alloc(ddev, 1);
549 skb_put_u8(skb, DIGITAL_CMD_SENS_REQ);
551 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
558 int digital_in_recv_mifare_res(struct sk_buff *resp)
560 /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
561 * Since the driver can't differentiate a ACK/NACK response from a valid
562 * READ response, the CRC calculation must be handled at digital level
563 * even if the driver supports it for this technology.
565 if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
566 if (digital_skb_check_crc_a(resp)) {
567 PROTOCOL_ERR("9.4.1.2");
574 /* ACK response (i.e. successful WRITE). */
575 if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
580 /* NACK and any other responses are treated as error. */
584 static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
585 struct sk_buff *resp)
587 struct nfc_target *target = arg;
588 struct digital_attrib_res *attrib_res;
597 if (resp->len < sizeof(*attrib_res)) {
598 PROTOCOL_ERR("12.6.2");
603 attrib_res = (struct digital_attrib_res *)resp->data;
605 if (attrib_res->mbli_did & 0x0f) {
606 PROTOCOL_ERR("12.6.2.1");
611 rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
618 digital_poll_next_tech(ddev);
621 static int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
622 struct nfc_target *target,
623 struct digital_sensb_res *sensb_res)
625 struct digital_attrib_req *attrib_req;
629 skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
633 attrib_req = skb_put(skb, 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 = skb_put(skb, sizeof(*sensb_req));
739 sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
740 sensb_req->afi = 0x00; /* All families and sub-families */
741 sensb_req->param = DIGITAL_SENSB_N(0);
743 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
751 static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
752 struct sk_buff *resp)
756 struct nfc_target target;
757 struct digital_sensf_res *sensf_res;
765 if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
770 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
771 rc = digital_skb_check_crc_f(resp);
773 PROTOCOL_ERR("6.4.1.8");
780 memset(&target, 0, sizeof(struct nfc_target));
782 sensf_res = (struct digital_sensf_res *)resp->data;
784 memcpy(target.sensf_res, sensf_res, resp->len);
785 target.sensf_res_len = resp->len;
787 memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
788 target.nfcid2_len = NFC_NFCID2_MAXSIZE;
790 if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
791 target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
792 proto = NFC_PROTO_NFC_DEP;
794 proto = NFC_PROTO_FELICA;
796 rc = digital_target_found(ddev, &target, proto);
802 digital_poll_next_tech(ddev);
805 int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
807 struct digital_sensf_req *sensf_req;
812 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
816 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
817 NFC_DIGITAL_FRAMING_NFCF);
821 size = sizeof(struct digital_sensf_req);
823 skb = digital_skb_alloc(ddev, size);
829 sensf_req = (struct digital_sensf_req *)skb->data;
830 sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
831 sensf_req->sc1 = 0xFF;
832 sensf_req->sc2 = 0xFF;
836 *(u8 *)skb_push(skb, 1) = size + 1;
838 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
839 digital_skb_add_crc_f(skb);
841 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
849 static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev,
850 void *arg, struct sk_buff *resp)
852 struct digital_iso15693_inv_res *res;
853 struct nfc_target *target = NULL;
862 if (resp->len != sizeof(*res)) {
867 res = (struct digital_iso15693_inv_res *)resp->data;
869 if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) {
870 PROTOCOL_ERR("ISO15693 - 10.3.1");
875 target = kzalloc(sizeof(*target), GFP_KERNEL);
881 target->is_iso15693 = 1;
882 target->iso15693_dsfid = res->dsfid;
883 memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid));
885 rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693);
893 digital_poll_next_tech(ddev);
896 int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech)
898 struct digital_iso15693_inv_req *req;
902 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
903 NFC_DIGITAL_RF_TECH_ISO15693);
907 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
908 NFC_DIGITAL_FRAMING_ISO15693_INVENTORY);
912 skb = digital_skb_alloc(ddev, sizeof(*req));
916 skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */
917 req = (struct digital_iso15693_inv_req *)skb->data;
919 /* Single sub-carrier, high data rate, no AFI, single slot
922 req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE |
923 DIGITAL_ISO15693_REQ_FLAG_INVENTORY |
924 DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS;
925 req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ;
928 rc = digital_in_send_cmd(ddev, skb, 30,
929 digital_in_recv_iso15693_inv_res, NULL);
936 static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
941 skb = digital_skb_alloc(ddev, 1);
945 skb_put_u8(skb, DIGITAL_SEL_RES_NFC_DEP);
947 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
948 digital_skb_add_crc_a(skb);
950 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
951 NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE);
957 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
965 static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
966 struct sk_buff *resp)
976 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
977 rc = digital_skb_check_crc_a(resp);
979 PROTOCOL_ERR("4.4.1.3");
984 /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
986 rc = digital_tg_send_sel_res(ddev);
990 digital_poll_next_tech(ddev);
995 static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
998 struct digital_sdd_res *sdd_res;
1001 skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
1005 skb_put(skb, sizeof(struct digital_sdd_res));
1006 sdd_res = (struct digital_sdd_res *)skb->data;
1008 sdd_res->nfcid1[0] = 0x08;
1009 get_random_bytes(sdd_res->nfcid1 + 1, 3);
1012 for (i = 0; i < 4; i++)
1013 sdd_res->bcc ^= sdd_res->nfcid1[i];
1015 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1016 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
1022 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
1030 static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
1031 struct sk_buff *resp)
1042 sdd_req = resp->data;
1044 if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
1045 sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
1050 rc = digital_tg_send_sdd_res(ddev);
1054 digital_poll_next_tech(ddev);
1056 dev_kfree_skb(resp);
1059 static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
1061 struct sk_buff *skb;
1065 skb = digital_skb_alloc(ddev, 2);
1069 sens_res = skb_put(skb, 2);
1071 sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
1072 sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
1074 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1075 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
1081 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
1089 void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
1090 struct sk_buff *resp)
1101 sens_req = resp->data[0];
1103 if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
1104 sens_req != DIGITAL_CMD_ALL_REQ)) {
1109 rc = digital_tg_send_sens_res(ddev);
1113 digital_poll_next_tech(ddev);
1115 dev_kfree_skb(resp);
1118 static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
1119 void *arg, struct sk_buff *resp)
1121 if (!IS_ERR(resp) && (resp->len >= 2) &&
1122 (resp->data[1] == DIGITAL_CMD_SENSF_REQ))
1123 digital_tg_recv_sensf_req(ddev, arg, resp);
1125 digital_tg_recv_atr_req(ddev, arg, resp);
1130 static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
1131 struct digital_sensf_req *sensf_req)
1133 struct sk_buff *skb;
1136 struct digital_sensf_res *sensf_res;
1138 size = sizeof(struct digital_sensf_res);
1140 if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
1141 size -= sizeof(sensf_res->rd);
1143 skb = digital_skb_alloc(ddev, size);
1149 sensf_res = (struct digital_sensf_res *)skb->data;
1151 memset(sensf_res, 0, size);
1153 sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
1154 sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
1155 sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
1156 get_random_bytes(&sensf_res->nfcid2[2], 6);
1158 switch (sensf_req->rc) {
1159 case DIGITAL_SENSF_REQ_RC_SC:
1160 sensf_res->rd[0] = sensf_req->sc1;
1161 sensf_res->rd[1] = sensf_req->sc2;
1163 case DIGITAL_SENSF_REQ_RC_AP:
1164 sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
1165 sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
1169 *(u8 *)skb_push(skb, sizeof(u8)) = size + 1;
1171 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
1172 digital_skb_add_crc_f(skb);
1174 rc = digital_tg_send_cmd(ddev, skb, 300,
1175 digital_tg_recv_atr_or_sensf_req, NULL);
1182 void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
1183 struct sk_buff *resp)
1185 struct digital_sensf_req *sensf_req;
1194 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
1195 rc = digital_skb_check_crc_f(resp);
1197 PROTOCOL_ERR("6.4.1.8");
1202 if (resp->len != sizeof(struct digital_sensf_req) + 1) {
1208 sensf_req = (struct digital_sensf_req *)resp->data;
1210 if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
1215 rc = digital_tg_send_sensf_res(ddev, sensf_req);
1219 digital_poll_next_tech(ddev);
1221 dev_kfree_skb(resp);
1224 static int digital_tg_config_nfca(struct nfc_digital_dev *ddev)
1228 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1229 NFC_DIGITAL_RF_TECH_106A);
1233 return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1234 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1237 int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
1241 rc = digital_tg_config_nfca(ddev);
1245 return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
1248 static int digital_tg_config_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1252 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1256 return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1257 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
1260 int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1264 rc = digital_tg_config_nfcf(ddev, rf_tech);
1268 return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, NULL);
1271 void digital_tg_recv_md_req(struct nfc_digital_dev *ddev, void *arg,
1272 struct sk_buff *resp)
1282 rc = ddev->ops->tg_get_rf_tech(ddev, &rf_tech);
1287 case NFC_DIGITAL_RF_TECH_106A:
1288 rc = digital_tg_config_nfca(ddev);
1291 digital_tg_recv_sens_req(ddev, arg, resp);
1293 case NFC_DIGITAL_RF_TECH_212F:
1294 case NFC_DIGITAL_RF_TECH_424F:
1295 rc = digital_tg_config_nfcf(ddev, rf_tech);
1298 digital_tg_recv_sensf_req(ddev, arg, resp);
1307 digital_poll_next_tech(ddev);
1308 dev_kfree_skb(resp);