Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * NFC Digital Protocol stack
0004  * Copyright (c) 2013, Intel Corporation.
0005  */
0006 
0007 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
0008 
0009 #include "digital.h"
0010 
0011 #define DIGITAL_CMD_SENS_REQ    0x26
0012 #define DIGITAL_CMD_ALL_REQ     0x52
0013 #define DIGITAL_CMD_SEL_REQ_CL1 0x93
0014 #define DIGITAL_CMD_SEL_REQ_CL2 0x95
0015 #define DIGITAL_CMD_SEL_REQ_CL3 0x97
0016 
0017 #define DIGITAL_SDD_REQ_SEL_PAR 0x20
0018 
0019 #define DIGITAL_SDD_RES_CT  0x88
0020 #define DIGITAL_SDD_RES_LEN 5
0021 #define DIGITAL_SEL_RES_LEN 1
0022 
0023 #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
0024 #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
0025 #define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20)
0026 #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
0027 
0028 #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
0029 #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
0030     ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
0031     (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
0032 
0033 #define DIGITAL_MIFARE_READ_RES_LEN 16
0034 #define DIGITAL_MIFARE_ACK_RES  0x0A
0035 
0036 #define DIGITAL_CMD_SENSB_REQ           0x05
0037 #define DIGITAL_SENSB_ADVANCED          BIT(5)
0038 #define DIGITAL_SENSB_EXTENDED          BIT(4)
0039 #define DIGITAL_SENSB_ALLB_REQ          BIT(3)
0040 #define DIGITAL_SENSB_N(n)          ((n) & 0x7)
0041 
0042 #define DIGITAL_CMD_SENSB_RES           0x50
0043 
0044 #define DIGITAL_CMD_ATTRIB_REQ          0x1D
0045 #define DIGITAL_ATTRIB_P1_TR0_DEFAULT       (0x0 << 6)
0046 #define DIGITAL_ATTRIB_P1_TR1_DEFAULT       (0x0 << 4)
0047 #define DIGITAL_ATTRIB_P1_SUPRESS_EOS       BIT(3)
0048 #define DIGITAL_ATTRIB_P1_SUPRESS_SOS       BIT(2)
0049 #define DIGITAL_ATTRIB_P2_LISTEN_POLL_1     (0x0 << 6)
0050 #define DIGITAL_ATTRIB_P2_POLL_LISTEN_1     (0x0 << 4)
0051 #define DIGITAL_ATTRIB_P2_MAX_FRAME_256     0x8
0052 #define DIGITAL_ATTRIB_P4_DID(n)        ((n) & 0xf)
0053 
0054 #define DIGITAL_CMD_SENSF_REQ   0x00
0055 #define DIGITAL_CMD_SENSF_RES   0x01
0056 
0057 #define DIGITAL_SENSF_RES_MIN_LENGTH 17
0058 #define DIGITAL_SENSF_RES_RD_AP_B1   0x00
0059 #define DIGITAL_SENSF_RES_RD_AP_B2   0x8F
0060 
0061 #define DIGITAL_SENSF_REQ_RC_NONE 0
0062 #define DIGITAL_SENSF_REQ_RC_SC   1
0063 #define DIGITAL_SENSF_REQ_RC_AP   2
0064 
0065 #define DIGITAL_CMD_ISO15693_INVENTORY_REQ  0x01
0066 
0067 #define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE BIT(1)
0068 #define DIGITAL_ISO15693_REQ_FLAG_INVENTORY BIT(2)
0069 #define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS  BIT(5)
0070 #define DIGITAL_ISO15693_RES_FLAG_ERROR     BIT(0)
0071 #define DIGITAL_ISO15693_RES_IS_VALID(flags) \
0072     (!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR))
0073 
0074 #define DIGITAL_ISO_DEP_I_PCB    0x02
0075 #define DIGITAL_ISO_DEP_PNI(pni) ((pni) & 0x01)
0076 
0077 #define DIGITAL_ISO_DEP_PCB_TYPE(pcb) ((pcb) & 0xC0)
0078 
0079 #define DIGITAL_ISO_DEP_I_BLOCK 0x00
0080 
0081 #define DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb) ((pcb) & 0x08)
0082 
0083 static const u8 digital_ats_fsc[] = {
0084      16,  24,  32,  40,  48,  64,  96, 128,
0085 };
0086 
0087 #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
0088 #define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
0089 #define DIGITAL_ATS_MAX_FSC  256
0090 
0091 #define DIGITAL_RATS_BYTE1 0xE0
0092 #define DIGITAL_RATS_PARAM 0x80
0093 
0094 struct digital_sdd_res {
0095     u8 nfcid1[4];
0096     u8 bcc;
0097 } __packed;
0098 
0099 struct digital_sel_req {
0100     u8 sel_cmd;
0101     u8 b2;
0102     u8 nfcid1[4];
0103     u8 bcc;
0104 } __packed;
0105 
0106 struct digital_sensb_req {
0107     u8 cmd;
0108     u8 afi;
0109     u8 param;
0110 } __packed;
0111 
0112 struct digital_sensb_res {
0113     u8 cmd;
0114     u8 nfcid0[4];
0115     u8 app_data[4];
0116     u8 proto_info[3];
0117 } __packed;
0118 
0119 struct digital_attrib_req {
0120     u8 cmd;
0121     u8 nfcid0[4];
0122     u8 param1;
0123     u8 param2;
0124     u8 param3;
0125     u8 param4;
0126 } __packed;
0127 
0128 struct digital_attrib_res {
0129     u8 mbli_did;
0130 } __packed;
0131 
0132 struct digital_sensf_req {
0133     u8 cmd;
0134     u8 sc1;
0135     u8 sc2;
0136     u8 rc;
0137     u8 tsn;
0138 } __packed;
0139 
0140 struct digital_sensf_res {
0141     u8 cmd;
0142     u8 nfcid2[8];
0143     u8 pad0[2];
0144     u8 pad1[3];
0145     u8 mrti_check;
0146     u8 mrti_update;
0147     u8 pad2;
0148     u8 rd[2];
0149 } __packed;
0150 
0151 struct digital_iso15693_inv_req {
0152     u8 flags;
0153     u8 cmd;
0154     u8 mask_len;
0155     u64 mask;
0156 } __packed;
0157 
0158 struct digital_iso15693_inv_res {
0159     u8 flags;
0160     u8 dsfid;
0161     u64 uid;
0162 } __packed;
0163 
0164 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
0165                    struct nfc_target *target);
0166 
0167 int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
0168                 struct sk_buff *skb)
0169 {
0170     u8 pcb;
0171     u8 block_type;
0172 
0173     if (skb->len < 1)
0174         return -EIO;
0175 
0176     pcb = *skb->data;
0177     block_type = DIGITAL_ISO_DEP_PCB_TYPE(pcb);
0178 
0179     /* No support fo R-block nor S-block */
0180     if (block_type != DIGITAL_ISO_DEP_I_BLOCK) {
0181         pr_err("ISO_DEP R-block and S-block not supported\n");
0182         return -EIO;
0183     }
0184 
0185     if (DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb)) {
0186         pr_err("DID field in ISO_DEP PCB not supported\n");
0187         return -EIO;
0188     }
0189 
0190     skb_pull(skb, 1);
0191 
0192     return 0;
0193 }
0194 
0195 int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
0196                 struct sk_buff *skb)
0197 {
0198     /*
0199      * Chaining not supported so skb->len + 1 PCB byte + 2 CRC bytes must
0200      * not be greater than remote FSC
0201      */
0202     if (skb->len + 3 > ddev->target_fsc)
0203         return -EIO;
0204 
0205     skb_push(skb, 1);
0206 
0207     *skb->data = DIGITAL_ISO_DEP_I_PCB | ddev->curr_nfc_dep_pni;
0208 
0209     ddev->curr_nfc_dep_pni =
0210         DIGITAL_ISO_DEP_PNI(ddev->curr_nfc_dep_pni + 1);
0211 
0212     return 0;
0213 }
0214 
0215 static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg,
0216                 struct sk_buff *resp)
0217 {
0218     struct nfc_target *target = arg;
0219     u8 fsdi;
0220     int rc;
0221 
0222     if (IS_ERR(resp)) {
0223         rc = PTR_ERR(resp);
0224         resp = NULL;
0225         goto exit;
0226     }
0227 
0228     if (resp->len < 2) {
0229         rc = -EIO;
0230         goto exit;
0231     }
0232 
0233     fsdi = DIGITAL_ATS_FSCI(resp->data[1]);
0234     if (fsdi >= 8)
0235         ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
0236     else
0237         ddev->target_fsc = digital_ats_fsc[fsdi];
0238 
0239     ddev->curr_nfc_dep_pni = 0;
0240 
0241     rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443);
0242 
0243 exit:
0244     dev_kfree_skb(resp);
0245     kfree(target);
0246 
0247     if (rc)
0248         digital_poll_next_tech(ddev);
0249 }
0250 
0251 static int digital_in_send_rats(struct nfc_digital_dev *ddev,
0252                 struct nfc_target *target)
0253 {
0254     int rc;
0255     struct sk_buff *skb;
0256 
0257     skb = digital_skb_alloc(ddev, 2);
0258     if (!skb)
0259         return -ENOMEM;
0260 
0261     skb_put_u8(skb, DIGITAL_RATS_BYTE1);
0262     skb_put_u8(skb, DIGITAL_RATS_PARAM);
0263 
0264     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats,
0265                  target);
0266     if (rc)
0267         kfree_skb(skb);
0268 
0269     return rc;
0270 }
0271 
0272 static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
0273                     struct sk_buff *resp)
0274 {
0275     struct nfc_target *target = arg;
0276     int rc;
0277     u8 sel_res;
0278     u8 nfc_proto;
0279 
0280     if (IS_ERR(resp)) {
0281         rc = PTR_ERR(resp);
0282         resp = NULL;
0283         goto exit;
0284     }
0285 
0286     if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
0287         rc = digital_skb_check_crc_a(resp);
0288         if (rc) {
0289             PROTOCOL_ERR("4.4.1.3");
0290             goto exit;
0291         }
0292     }
0293 
0294     if (resp->len != DIGITAL_SEL_RES_LEN) {
0295         rc = -EIO;
0296         goto exit;
0297     }
0298 
0299     sel_res = resp->data[0];
0300 
0301     if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
0302         rc = digital_in_send_sdd_req(ddev, target);
0303         if (rc)
0304             goto exit;
0305 
0306         goto exit_free_skb;
0307     }
0308 
0309     target->sel_res = sel_res;
0310 
0311     if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
0312         nfc_proto = NFC_PROTO_MIFARE;
0313     } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
0314         nfc_proto = NFC_PROTO_NFC_DEP;
0315     } else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) {
0316         rc = digital_in_send_rats(ddev, target);
0317         if (rc)
0318             goto exit;
0319         /*
0320          * Skip target_found and don't free it for now. This will be
0321          * done when receiving the ATS
0322          */
0323         goto exit_free_skb;
0324     } else {
0325         rc = -EOPNOTSUPP;
0326         goto exit;
0327     }
0328 
0329     rc = digital_target_found(ddev, target, nfc_proto);
0330 
0331 exit:
0332     kfree(target);
0333 
0334 exit_free_skb:
0335     dev_kfree_skb(resp);
0336 
0337     if (rc)
0338         digital_poll_next_tech(ddev);
0339 }
0340 
0341 static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
0342                    struct nfc_target *target,
0343                    struct digital_sdd_res *sdd_res)
0344 {
0345     struct sk_buff *skb;
0346     struct digital_sel_req *sel_req;
0347     u8 sel_cmd;
0348     int rc;
0349 
0350     skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
0351     if (!skb)
0352         return -ENOMEM;
0353 
0354     skb_put(skb, sizeof(struct digital_sel_req));
0355     sel_req = (struct digital_sel_req *)skb->data;
0356 
0357     if (target->nfcid1_len <= 4)
0358         sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
0359     else if (target->nfcid1_len < 10)
0360         sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
0361     else
0362         sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
0363 
0364     sel_req->sel_cmd = sel_cmd;
0365     sel_req->b2 = 0x70;
0366     memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
0367     sel_req->bcc = sdd_res->bcc;
0368 
0369     if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
0370         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0371                 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
0372         if (rc)
0373             goto exit;
0374     } else {
0375         digital_skb_add_crc_a(skb);
0376     }
0377 
0378     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
0379                  target);
0380 exit:
0381     if (rc)
0382         kfree_skb(skb);
0383 
0384     return rc;
0385 }
0386 
0387 static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
0388                     struct sk_buff *resp)
0389 {
0390     struct nfc_target *target = arg;
0391     struct digital_sdd_res *sdd_res;
0392     int rc;
0393     u8 offset, size;
0394     u8 i, bcc;
0395 
0396     if (IS_ERR(resp)) {
0397         rc = PTR_ERR(resp);
0398         resp = NULL;
0399         goto exit;
0400     }
0401 
0402     if (resp->len < DIGITAL_SDD_RES_LEN) {
0403         PROTOCOL_ERR("4.7.2.8");
0404         rc = -EINVAL;
0405         goto exit;
0406     }
0407 
0408     sdd_res = (struct digital_sdd_res *)resp->data;
0409 
0410     for (i = 0, bcc = 0; i < 4; i++)
0411         bcc ^= sdd_res->nfcid1[i];
0412 
0413     if (bcc != sdd_res->bcc) {
0414         PROTOCOL_ERR("4.7.2.6");
0415         rc = -EINVAL;
0416         goto exit;
0417     }
0418 
0419     if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
0420         offset = 1;
0421         size = 3;
0422     } else {
0423         offset = 0;
0424         size = 4;
0425     }
0426 
0427     memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
0428            size);
0429     target->nfcid1_len += size;
0430 
0431     rc = digital_in_send_sel_req(ddev, target, sdd_res);
0432 
0433 exit:
0434     dev_kfree_skb(resp);
0435 
0436     if (rc) {
0437         kfree(target);
0438         digital_poll_next_tech(ddev);
0439     }
0440 }
0441 
0442 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
0443                    struct nfc_target *target)
0444 {
0445     int rc;
0446     struct sk_buff *skb;
0447     u8 sel_cmd;
0448 
0449     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0450                      NFC_DIGITAL_FRAMING_NFCA_STANDARD);
0451     if (rc)
0452         return rc;
0453 
0454     skb = digital_skb_alloc(ddev, 2);
0455     if (!skb)
0456         return -ENOMEM;
0457 
0458     if (target->nfcid1_len == 0)
0459         sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
0460     else if (target->nfcid1_len == 3)
0461         sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
0462     else
0463         sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
0464 
0465     skb_put_u8(skb, sel_cmd);
0466     skb_put_u8(skb, DIGITAL_SDD_REQ_SEL_PAR);
0467 
0468     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
0469                  target);
0470     if (rc)
0471         kfree_skb(skb);
0472 
0473     return rc;
0474 }
0475 
0476 static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
0477                      struct sk_buff *resp)
0478 {
0479     struct nfc_target *target = NULL;
0480     int rc;
0481 
0482     if (IS_ERR(resp)) {
0483         rc = PTR_ERR(resp);
0484         resp = NULL;
0485         goto exit;
0486     }
0487 
0488     if (resp->len < sizeof(u16)) {
0489         rc = -EIO;
0490         goto exit;
0491     }
0492 
0493     target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
0494     if (!target) {
0495         rc = -ENOMEM;
0496         goto exit;
0497     }
0498 
0499     target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
0500 
0501     if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
0502         PROTOCOL_ERR("4.6.3.3");
0503         rc = -EINVAL;
0504         goto exit;
0505     }
0506 
0507     if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
0508         rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
0509     else
0510         rc = digital_in_send_sdd_req(ddev, target);
0511 
0512 exit:
0513     dev_kfree_skb(resp);
0514 
0515     if (rc) {
0516         kfree(target);
0517         digital_poll_next_tech(ddev);
0518     }
0519 }
0520 
0521 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
0522 {
0523     struct sk_buff *skb;
0524     int rc;
0525 
0526     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
0527                      NFC_DIGITAL_RF_TECH_106A);
0528     if (rc)
0529         return rc;
0530 
0531     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0532                      NFC_DIGITAL_FRAMING_NFCA_SHORT);
0533     if (rc)
0534         return rc;
0535 
0536     skb = digital_skb_alloc(ddev, 1);
0537     if (!skb)
0538         return -ENOMEM;
0539 
0540     skb_put_u8(skb, DIGITAL_CMD_SENS_REQ);
0541 
0542     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
0543     if (rc)
0544         kfree_skb(skb);
0545 
0546     return rc;
0547 }
0548 
0549 int digital_in_recv_mifare_res(struct sk_buff *resp)
0550 {
0551     /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
0552      * Since the driver can't differentiate a ACK/NACK response from a valid
0553      * READ response, the CRC calculation must be handled at digital level
0554      * even if the driver supports it for this technology.
0555      */
0556     if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
0557         if (digital_skb_check_crc_a(resp)) {
0558             PROTOCOL_ERR("9.4.1.2");
0559             return -EIO;
0560         }
0561 
0562         return 0;
0563     }
0564 
0565     /* ACK response (i.e. successful WRITE). */
0566     if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
0567         resp->data[0] = 0;
0568         return 0;
0569     }
0570 
0571     /* NACK and any other responses are treated as error. */
0572     return -EIO;
0573 }
0574 
0575 static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
0576                        struct sk_buff *resp)
0577 {
0578     struct nfc_target *target = arg;
0579     struct digital_attrib_res *attrib_res;
0580     int rc;
0581 
0582     if (IS_ERR(resp)) {
0583         rc = PTR_ERR(resp);
0584         resp = NULL;
0585         goto exit;
0586     }
0587 
0588     if (resp->len < sizeof(*attrib_res)) {
0589         PROTOCOL_ERR("12.6.2");
0590         rc = -EIO;
0591         goto exit;
0592     }
0593 
0594     attrib_res = (struct digital_attrib_res *)resp->data;
0595 
0596     if (attrib_res->mbli_did & 0x0f) {
0597         PROTOCOL_ERR("12.6.2.1");
0598         rc = -EIO;
0599         goto exit;
0600     }
0601 
0602     rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
0603 
0604 exit:
0605     dev_kfree_skb(resp);
0606     kfree(target);
0607 
0608     if (rc)
0609         digital_poll_next_tech(ddev);
0610 }
0611 
0612 static int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
0613                    struct nfc_target *target,
0614                    struct digital_sensb_res *sensb_res)
0615 {
0616     struct digital_attrib_req *attrib_req;
0617     struct sk_buff *skb;
0618     int rc;
0619 
0620     skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
0621     if (!skb)
0622         return -ENOMEM;
0623 
0624     attrib_req = skb_put(skb, sizeof(*attrib_req));
0625 
0626     attrib_req->cmd = DIGITAL_CMD_ATTRIB_REQ;
0627     memcpy(attrib_req->nfcid0, sensb_res->nfcid0,
0628            sizeof(attrib_req->nfcid0));
0629     attrib_req->param1 = DIGITAL_ATTRIB_P1_TR0_DEFAULT |
0630                  DIGITAL_ATTRIB_P1_TR1_DEFAULT;
0631     attrib_req->param2 = DIGITAL_ATTRIB_P2_LISTEN_POLL_1 |
0632                  DIGITAL_ATTRIB_P2_POLL_LISTEN_1 |
0633                  DIGITAL_ATTRIB_P2_MAX_FRAME_256;
0634     attrib_req->param3 = sensb_res->proto_info[1] & 0x07;
0635     attrib_req->param4 = DIGITAL_ATTRIB_P4_DID(0);
0636 
0637     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_attrib_res,
0638                  target);
0639     if (rc)
0640         kfree_skb(skb);
0641 
0642     return rc;
0643 }
0644 
0645 static void digital_in_recv_sensb_res(struct nfc_digital_dev *ddev, void *arg,
0646                       struct sk_buff *resp)
0647 {
0648     struct nfc_target *target = NULL;
0649     struct digital_sensb_res *sensb_res;
0650     u8 fsci;
0651     int rc;
0652 
0653     if (IS_ERR(resp)) {
0654         rc = PTR_ERR(resp);
0655         resp = NULL;
0656         goto exit;
0657     }
0658 
0659     if (resp->len != sizeof(*sensb_res)) {
0660         PROTOCOL_ERR("5.6.2.1");
0661         rc = -EIO;
0662         goto exit;
0663     }
0664 
0665     sensb_res = (struct digital_sensb_res *)resp->data;
0666 
0667     if (sensb_res->cmd != DIGITAL_CMD_SENSB_RES) {
0668         PROTOCOL_ERR("5.6.2");
0669         rc = -EIO;
0670         goto exit;
0671     }
0672 
0673     if (!(sensb_res->proto_info[1] & BIT(0))) {
0674         PROTOCOL_ERR("5.6.2.12");
0675         rc = -EIO;
0676         goto exit;
0677     }
0678 
0679     if (sensb_res->proto_info[1] & BIT(3)) {
0680         PROTOCOL_ERR("5.6.2.16");
0681         rc = -EIO;
0682         goto exit;
0683     }
0684 
0685     fsci = DIGITAL_SENSB_FSCI(sensb_res->proto_info[1]);
0686     if (fsci >= 8)
0687         ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
0688     else
0689         ddev->target_fsc = digital_ats_fsc[fsci];
0690 
0691     target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
0692     if (!target) {
0693         rc = -ENOMEM;
0694         goto exit;
0695     }
0696 
0697     rc = digital_in_send_attrib_req(ddev, target, sensb_res);
0698 
0699 exit:
0700     dev_kfree_skb(resp);
0701 
0702     if (rc) {
0703         kfree(target);
0704         digital_poll_next_tech(ddev);
0705     }
0706 }
0707 
0708 int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech)
0709 {
0710     struct digital_sensb_req *sensb_req;
0711     struct sk_buff *skb;
0712     int rc;
0713 
0714     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
0715                      NFC_DIGITAL_RF_TECH_106B);
0716     if (rc)
0717         return rc;
0718 
0719     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0720                      NFC_DIGITAL_FRAMING_NFCB);
0721     if (rc)
0722         return rc;
0723 
0724     skb = digital_skb_alloc(ddev, sizeof(*sensb_req));
0725     if (!skb)
0726         return -ENOMEM;
0727 
0728     sensb_req = skb_put(skb, sizeof(*sensb_req));
0729 
0730     sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
0731     sensb_req->afi = 0x00; /* All families and sub-families */
0732     sensb_req->param = DIGITAL_SENSB_N(0);
0733 
0734     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
0735                  NULL);
0736     if (rc)
0737         kfree_skb(skb);
0738 
0739     return rc;
0740 }
0741 
0742 static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
0743                    struct sk_buff *resp)
0744 {
0745     int rc;
0746     u8 proto;
0747     struct nfc_target target;
0748     struct digital_sensf_res *sensf_res;
0749 
0750     if (IS_ERR(resp)) {
0751         rc = PTR_ERR(resp);
0752         resp = NULL;
0753         goto exit;
0754     }
0755 
0756     if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
0757         rc = -EIO;
0758         goto exit;
0759     }
0760 
0761     if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
0762         rc = digital_skb_check_crc_f(resp);
0763         if (rc) {
0764             PROTOCOL_ERR("6.4.1.8");
0765             goto exit;
0766         }
0767     }
0768 
0769     skb_pull(resp, 1);
0770 
0771     memset(&target, 0, sizeof(struct nfc_target));
0772 
0773     sensf_res = (struct digital_sensf_res *)resp->data;
0774 
0775     memcpy(target.sensf_res, sensf_res, resp->len);
0776     target.sensf_res_len = resp->len;
0777 
0778     memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
0779     target.nfcid2_len = NFC_NFCID2_MAXSIZE;
0780 
0781     if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
0782         target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
0783         proto = NFC_PROTO_NFC_DEP;
0784     else
0785         proto = NFC_PROTO_FELICA;
0786 
0787     rc = digital_target_found(ddev, &target, proto);
0788 
0789 exit:
0790     dev_kfree_skb(resp);
0791 
0792     if (rc)
0793         digital_poll_next_tech(ddev);
0794 }
0795 
0796 int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
0797 {
0798     struct digital_sensf_req *sensf_req;
0799     struct sk_buff *skb;
0800     int rc;
0801     u8 size;
0802 
0803     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
0804     if (rc)
0805         return rc;
0806 
0807     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0808                      NFC_DIGITAL_FRAMING_NFCF);
0809     if (rc)
0810         return rc;
0811 
0812     size = sizeof(struct digital_sensf_req);
0813 
0814     skb = digital_skb_alloc(ddev, size);
0815     if (!skb)
0816         return -ENOMEM;
0817 
0818     skb_put(skb, size);
0819 
0820     sensf_req = (struct digital_sensf_req *)skb->data;
0821     sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
0822     sensf_req->sc1 = 0xFF;
0823     sensf_req->sc2 = 0xFF;
0824     sensf_req->rc = 0;
0825     sensf_req->tsn = 0;
0826 
0827     *(u8 *)skb_push(skb, 1) = size + 1;
0828 
0829     if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
0830         digital_skb_add_crc_f(skb);
0831 
0832     rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
0833                  NULL);
0834     if (rc)
0835         kfree_skb(skb);
0836 
0837     return rc;
0838 }
0839 
0840 static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev,
0841         void *arg, struct sk_buff *resp)
0842 {
0843     struct digital_iso15693_inv_res *res;
0844     struct nfc_target *target = NULL;
0845     int rc;
0846 
0847     if (IS_ERR(resp)) {
0848         rc = PTR_ERR(resp);
0849         resp = NULL;
0850         goto out_free_skb;
0851     }
0852 
0853     if (resp->len != sizeof(*res)) {
0854         rc = -EIO;
0855         goto out_free_skb;
0856     }
0857 
0858     res = (struct digital_iso15693_inv_res *)resp->data;
0859 
0860     if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) {
0861         PROTOCOL_ERR("ISO15693 - 10.3.1");
0862         rc = -EINVAL;
0863         goto out_free_skb;
0864     }
0865 
0866     target = kzalloc(sizeof(*target), GFP_KERNEL);
0867     if (!target) {
0868         rc = -ENOMEM;
0869         goto out_free_skb;
0870     }
0871 
0872     target->is_iso15693 = 1;
0873     target->iso15693_dsfid = res->dsfid;
0874     memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid));
0875 
0876     rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693);
0877 
0878     kfree(target);
0879 
0880 out_free_skb:
0881     dev_kfree_skb(resp);
0882 
0883     if (rc)
0884         digital_poll_next_tech(ddev);
0885 }
0886 
0887 int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech)
0888 {
0889     struct digital_iso15693_inv_req *req;
0890     struct sk_buff *skb;
0891     int rc;
0892 
0893     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
0894                      NFC_DIGITAL_RF_TECH_ISO15693);
0895     if (rc)
0896         return rc;
0897 
0898     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0899                      NFC_DIGITAL_FRAMING_ISO15693_INVENTORY);
0900     if (rc)
0901         return rc;
0902 
0903     skb = digital_skb_alloc(ddev, sizeof(*req));
0904     if (!skb)
0905         return -ENOMEM;
0906 
0907     skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */
0908     req = (struct digital_iso15693_inv_req *)skb->data;
0909 
0910     /* Single sub-carrier, high data rate, no AFI, single slot
0911      * Inventory command
0912      */
0913     req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE |
0914              DIGITAL_ISO15693_REQ_FLAG_INVENTORY |
0915              DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS;
0916     req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ;
0917     req->mask_len = 0;
0918 
0919     rc = digital_in_send_cmd(ddev, skb, 30,
0920                  digital_in_recv_iso15693_inv_res, NULL);
0921     if (rc)
0922         kfree_skb(skb);
0923 
0924     return rc;
0925 }
0926 
0927 static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
0928 {
0929     struct sk_buff *skb;
0930     int rc;
0931 
0932     skb = digital_skb_alloc(ddev, 1);
0933     if (!skb)
0934         return -ENOMEM;
0935 
0936     skb_put_u8(skb, DIGITAL_SEL_RES_NFC_DEP);
0937 
0938     if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
0939         digital_skb_add_crc_a(skb);
0940 
0941     rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
0942                      NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE);
0943     if (rc) {
0944         kfree_skb(skb);
0945         return rc;
0946     }
0947 
0948     rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
0949                  NULL);
0950     if (rc)
0951         kfree_skb(skb);
0952 
0953     return rc;
0954 }
0955 
0956 static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
0957                     struct sk_buff *resp)
0958 {
0959     int rc;
0960 
0961     if (IS_ERR(resp)) {
0962         rc = PTR_ERR(resp);
0963         resp = NULL;
0964         goto exit;
0965     }
0966 
0967     if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
0968         rc = digital_skb_check_crc_a(resp);
0969         if (rc) {
0970             PROTOCOL_ERR("4.4.1.3");
0971             goto exit;
0972         }
0973     }
0974 
0975     /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
0976 
0977     rc = digital_tg_send_sel_res(ddev);
0978 
0979 exit:
0980     if (rc)
0981         digital_poll_next_tech(ddev);
0982 
0983     dev_kfree_skb(resp);
0984 }
0985 
0986 static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
0987 {
0988     struct sk_buff *skb;
0989     struct digital_sdd_res *sdd_res;
0990     int rc, i;
0991 
0992     skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
0993     if (!skb)
0994         return -ENOMEM;
0995 
0996     skb_put(skb, sizeof(struct digital_sdd_res));
0997     sdd_res = (struct digital_sdd_res *)skb->data;
0998 
0999     sdd_res->nfcid1[0] = 0x08;
1000     get_random_bytes(sdd_res->nfcid1 + 1, 3);
1001 
1002     sdd_res->bcc = 0;
1003     for (i = 0; i < 4; i++)
1004         sdd_res->bcc ^= sdd_res->nfcid1[i];
1005 
1006     rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1007                 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
1008     if (rc) {
1009         kfree_skb(skb);
1010         return rc;
1011     }
1012 
1013     rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
1014                  NULL);
1015     if (rc)
1016         kfree_skb(skb);
1017 
1018     return rc;
1019 }
1020 
1021 static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
1022                     struct sk_buff *resp)
1023 {
1024     u8 *sdd_req;
1025     int rc;
1026 
1027     if (IS_ERR(resp)) {
1028         rc = PTR_ERR(resp);
1029         resp = NULL;
1030         goto exit;
1031     }
1032 
1033     sdd_req = resp->data;
1034 
1035     if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
1036         sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
1037         rc = -EINVAL;
1038         goto exit;
1039     }
1040 
1041     rc = digital_tg_send_sdd_res(ddev);
1042 
1043 exit:
1044     if (rc)
1045         digital_poll_next_tech(ddev);
1046 
1047     dev_kfree_skb(resp);
1048 }
1049 
1050 static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
1051 {
1052     struct sk_buff *skb;
1053     u8 *sens_res;
1054     int rc;
1055 
1056     skb = digital_skb_alloc(ddev, 2);
1057     if (!skb)
1058         return -ENOMEM;
1059 
1060     sens_res = skb_put(skb, 2);
1061 
1062     sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
1063     sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
1064 
1065     rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1066                      NFC_DIGITAL_FRAMING_NFCA_STANDARD);
1067     if (rc) {
1068         kfree_skb(skb);
1069         return rc;
1070     }
1071 
1072     rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
1073                  NULL);
1074     if (rc)
1075         kfree_skb(skb);
1076 
1077     return rc;
1078 }
1079 
1080 void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
1081                   struct sk_buff *resp)
1082 {
1083     u8 sens_req;
1084     int rc;
1085 
1086     if (IS_ERR(resp)) {
1087         rc = PTR_ERR(resp);
1088         resp = NULL;
1089         goto exit;
1090     }
1091 
1092     sens_req = resp->data[0];
1093 
1094     if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
1095         sens_req != DIGITAL_CMD_ALL_REQ)) {
1096         rc = -EINVAL;
1097         goto exit;
1098     }
1099 
1100     rc = digital_tg_send_sens_res(ddev);
1101 
1102 exit:
1103     if (rc)
1104         digital_poll_next_tech(ddev);
1105 
1106     dev_kfree_skb(resp);
1107 }
1108 
1109 static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
1110         void *arg, struct sk_buff *resp)
1111 {
1112     if (!IS_ERR(resp) && (resp->len >= 2) &&
1113             (resp->data[1] == DIGITAL_CMD_SENSF_REQ))
1114         digital_tg_recv_sensf_req(ddev, arg, resp);
1115     else
1116         digital_tg_recv_atr_req(ddev, arg, resp);
1117 
1118     return;
1119 }
1120 
1121 static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
1122                   struct digital_sensf_req *sensf_req)
1123 {
1124     struct sk_buff *skb;
1125     u8 size;
1126     int rc;
1127     struct digital_sensf_res *sensf_res;
1128 
1129     size = sizeof(struct digital_sensf_res);
1130 
1131     if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
1132         size -= sizeof(sensf_res->rd);
1133 
1134     skb = digital_skb_alloc(ddev, size);
1135     if (!skb)
1136         return -ENOMEM;
1137 
1138     skb_put(skb, size);
1139 
1140     sensf_res = (struct digital_sensf_res *)skb->data;
1141 
1142     memset(sensf_res, 0, size);
1143 
1144     sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
1145     sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
1146     sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
1147     get_random_bytes(&sensf_res->nfcid2[2], 6);
1148 
1149     switch (sensf_req->rc) {
1150     case DIGITAL_SENSF_REQ_RC_SC:
1151         sensf_res->rd[0] = sensf_req->sc1;
1152         sensf_res->rd[1] = sensf_req->sc2;
1153         break;
1154     case DIGITAL_SENSF_REQ_RC_AP:
1155         sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
1156         sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
1157         break;
1158     }
1159 
1160     *(u8 *)skb_push(skb, sizeof(u8)) = size + 1;
1161 
1162     if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
1163         digital_skb_add_crc_f(skb);
1164 
1165     rc = digital_tg_send_cmd(ddev, skb, 300,
1166                  digital_tg_recv_atr_or_sensf_req, NULL);
1167     if (rc)
1168         kfree_skb(skb);
1169 
1170     return rc;
1171 }
1172 
1173 void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
1174                    struct sk_buff *resp)
1175 {
1176     struct digital_sensf_req *sensf_req;
1177     int rc;
1178 
1179     if (IS_ERR(resp)) {
1180         rc = PTR_ERR(resp);
1181         resp = NULL;
1182         goto exit;
1183     }
1184 
1185     if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
1186         rc = digital_skb_check_crc_f(resp);
1187         if (rc) {
1188             PROTOCOL_ERR("6.4.1.8");
1189             goto exit;
1190         }
1191     }
1192 
1193     if (resp->len != sizeof(struct digital_sensf_req) + 1) {
1194         rc = -EINVAL;
1195         goto exit;
1196     }
1197 
1198     skb_pull(resp, 1);
1199     sensf_req = (struct digital_sensf_req *)resp->data;
1200 
1201     if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
1202         rc = -EINVAL;
1203         goto exit;
1204     }
1205 
1206     rc = digital_tg_send_sensf_res(ddev, sensf_req);
1207 
1208 exit:
1209     if (rc)
1210         digital_poll_next_tech(ddev);
1211 
1212     dev_kfree_skb(resp);
1213 }
1214 
1215 static int digital_tg_config_nfca(struct nfc_digital_dev *ddev)
1216 {
1217     int rc;
1218 
1219     rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1220                      NFC_DIGITAL_RF_TECH_106A);
1221     if (rc)
1222         return rc;
1223 
1224     return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1225                        NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1226 }
1227 
1228 int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
1229 {
1230     int rc;
1231 
1232     rc = digital_tg_config_nfca(ddev);
1233     if (rc)
1234         return rc;
1235 
1236     return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
1237 }
1238 
1239 static int digital_tg_config_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1240 {
1241     int rc;
1242 
1243     rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1244     if (rc)
1245         return rc;
1246 
1247     return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1248                        NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
1249 }
1250 
1251 int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1252 {
1253     int rc;
1254 
1255     rc = digital_tg_config_nfcf(ddev, rf_tech);
1256     if (rc)
1257         return rc;
1258 
1259     return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, NULL);
1260 }
1261 
1262 void digital_tg_recv_md_req(struct nfc_digital_dev *ddev, void *arg,
1263                 struct sk_buff *resp)
1264 {
1265     u8 rf_tech;
1266     int rc;
1267 
1268     if (IS_ERR(resp)) {
1269         resp = NULL;
1270         goto exit_free_skb;
1271     }
1272 
1273     rc = ddev->ops->tg_get_rf_tech(ddev, &rf_tech);
1274     if (rc)
1275         goto exit_free_skb;
1276 
1277     switch (rf_tech) {
1278     case NFC_DIGITAL_RF_TECH_106A:
1279         rc = digital_tg_config_nfca(ddev);
1280         if (rc)
1281             goto exit_free_skb;
1282         digital_tg_recv_sens_req(ddev, arg, resp);
1283         break;
1284     case NFC_DIGITAL_RF_TECH_212F:
1285     case NFC_DIGITAL_RF_TECH_424F:
1286         rc = digital_tg_config_nfcf(ddev, rf_tech);
1287         if (rc)
1288             goto exit_free_skb;
1289         digital_tg_recv_sensf_req(ddev, arg, resp);
1290         break;
1291     default:
1292         goto exit_free_skb;
1293     }
1294 
1295     return;
1296 
1297 exit_free_skb:
1298     digital_poll_next_tech(ddev);
1299     dev_kfree_skb(resp);
1300 }