Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* -------------------------------------------------------------------------
0003  * Copyright (C) 2014-2016, Intel Corporation
0004  *
0005  * -------------------------------------------------------------------------
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/nfc.h>
0010 #include <linux/i2c.h>
0011 #include <linux/delay.h>
0012 #include <linux/firmware.h>
0013 #include <net/nfc/nci_core.h>
0014 
0015 #include "fdp.h"
0016 
0017 #define FDP_OTP_PATCH_NAME          "otp.bin"
0018 #define FDP_RAM_PATCH_NAME          "ram.bin"
0019 #define FDP_FW_HEADER_SIZE          576
0020 #define FDP_FW_UPDATE_SLEEP         1000
0021 
0022 #define NCI_GET_VERSION_TIMEOUT         8000
0023 #define NCI_PATCH_REQUEST_TIMEOUT       8000
0024 #define FDP_PATCH_CONN_DEST         0xC2
0025 #define FDP_PATCH_CONN_PARAM_TYPE       0xA0
0026 
0027 #define NCI_PATCH_TYPE_RAM          0x00
0028 #define NCI_PATCH_TYPE_OTP          0x01
0029 #define NCI_PATCH_TYPE_EOT          0xFF
0030 
0031 #define NCI_PARAM_ID_FW_RAM_VERSION     0xA0
0032 #define NCI_PARAM_ID_FW_OTP_VERSION     0xA1
0033 #define NCI_PARAM_ID_OTP_LIMITED_VERSION    0xC5
0034 #define NCI_PARAM_ID_KEY_INDEX_ID       0xC6
0035 
0036 #define NCI_GID_PROP                0x0F
0037 #define NCI_OP_PROP_PATCH_OID           0x08
0038 #define NCI_OP_PROP_SET_PDATA_OID       0x23
0039 
0040 struct fdp_nci_info {
0041     const struct nfc_phy_ops *phy_ops;
0042     struct fdp_i2c_phy *phy;
0043     struct nci_dev *ndev;
0044 
0045     const struct firmware *otp_patch;
0046     const struct firmware *ram_patch;
0047     u32 otp_patch_version;
0048     u32 ram_patch_version;
0049 
0050     u32 otp_version;
0051     u32 ram_version;
0052     u32 limited_otp_version;
0053     u8 key_index;
0054 
0055     const u8 *fw_vsc_cfg;
0056     u8 clock_type;
0057     u32 clock_freq;
0058 
0059     atomic_t data_pkt_counter;
0060     void (*data_pkt_counter_cb)(struct nci_dev *ndev);
0061     u8 setup_patch_sent;
0062     u8 setup_patch_ntf;
0063     u8 setup_patch_status;
0064     u8 setup_reset_ntf;
0065     wait_queue_head_t setup_wq;
0066 };
0067 
0068 static const u8 nci_core_get_config_otp_ram_version[5] = {
0069     0x04,
0070     NCI_PARAM_ID_FW_RAM_VERSION,
0071     NCI_PARAM_ID_FW_OTP_VERSION,
0072     NCI_PARAM_ID_OTP_LIMITED_VERSION,
0073     NCI_PARAM_ID_KEY_INDEX_ID
0074 };
0075 
0076 struct nci_core_get_config_rsp {
0077     u8 status;
0078     u8 count;
0079     u8 data[];
0080 };
0081 
0082 static int fdp_nci_create_conn(struct nci_dev *ndev)
0083 {
0084     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0085     struct core_conn_create_dest_spec_params param;
0086     int r;
0087 
0088     /* proprietary destination specific paramerer without value */
0089     param.type = FDP_PATCH_CONN_PARAM_TYPE;
0090     param.length = 0x00;
0091 
0092     r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1,
0093                  sizeof(param), &param);
0094     if (r)
0095         return r;
0096 
0097     return nci_get_conn_info_by_dest_type_params(ndev,
0098                              FDP_PATCH_CONN_DEST, NULL);
0099 }
0100 
0101 static inline int fdp_nci_get_versions(struct nci_dev *ndev)
0102 {
0103     return nci_core_cmd(ndev, NCI_OP_CORE_GET_CONFIG_CMD,
0104                 sizeof(nci_core_get_config_otp_ram_version),
0105                 (__u8 *) &nci_core_get_config_otp_ram_version);
0106 }
0107 
0108 static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type)
0109 {
0110     return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type);
0111 }
0112 
0113 static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len,
0114                           const char *data)
0115 {
0116     return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data);
0117 }
0118 
0119 static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type,
0120                  u32 clock_freq)
0121 {
0122     u32 fc = 13560;
0123     u32 nd, num, delta;
0124     char data[9];
0125 
0126     nd = (24 * fc) / clock_freq;
0127     delta = 24 * fc - nd * clock_freq;
0128     num = (32768 * delta) / clock_freq;
0129 
0130     data[0] = 0x00;
0131     data[1] = 0x00;
0132     data[2] = 0x00;
0133 
0134     data[3] = 0x10;
0135     data[4] = 0x04;
0136     data[5] = num & 0xFF;
0137     data[6] = (num >> 8) & 0xff;
0138     data[7] = nd;
0139     data[8] = clock_type;
0140 
0141     return fdp_nci_set_production_data(ndev, 9, data);
0142 }
0143 
0144 static void fdp_nci_send_patch_cb(struct nci_dev *ndev)
0145 {
0146     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0147 
0148     info->setup_patch_sent = 1;
0149     wake_up(&info->setup_wq);
0150 }
0151 
0152 /*
0153  * Register a packet sent counter and a callback
0154  *
0155  * We have no other way of knowing when all firmware packets were sent out
0156  * on the i2c bus. We need to know that in order to close the connection and
0157  * send the patch end message.
0158  */
0159 static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev,
0160                   void (*cb)(struct nci_dev *ndev), int count)
0161 {
0162     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0163     struct device *dev = &info->phy->i2c_dev->dev;
0164 
0165     dev_dbg(dev, "NCI data pkt counter %d\n", count);
0166     atomic_set(&info->data_pkt_counter, count);
0167     info->data_pkt_counter_cb = cb;
0168 }
0169 
0170 /*
0171  * The device is expecting a stream of packets. All packets need to
0172  * have the PBF flag set to 0x0 (last packet) even if the firmware
0173  * file is segmented and there are multiple packets. If we give the
0174  * whole firmware to nci_send_data it will segment it and it will set
0175  * the PBF flag to 0x01 so we need to do the segmentation here.
0176  *
0177  * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD
0178  * command with NCI_PATCH_TYPE_EOT parameter. The device will send a
0179  * NFCC_PATCH_NTF packet and a NCI_OP_CORE_RESET_NTF packet.
0180  */
0181 static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type)
0182 {
0183     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0184     const struct firmware *fw;
0185     struct sk_buff *skb;
0186     unsigned long len;
0187     int max_size, payload_size;
0188     int rc = 0;
0189 
0190     if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) ||
0191         (type == NCI_PATCH_TYPE_RAM && !info->ram_patch))
0192         return -EINVAL;
0193 
0194     if (type == NCI_PATCH_TYPE_OTP)
0195         fw = info->otp_patch;
0196     else
0197         fw = info->ram_patch;
0198 
0199     max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id);
0200     if (max_size <= 0)
0201         return -EINVAL;
0202 
0203     len = fw->size;
0204 
0205     fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb,
0206                      DIV_ROUND_UP(fw->size, max_size));
0207 
0208     while (len) {
0209 
0210         payload_size = min_t(unsigned long, max_size, len);
0211 
0212         skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size),
0213                     GFP_KERNEL);
0214         if (!skb) {
0215             fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
0216             return -ENOMEM;
0217         }
0218 
0219 
0220         skb_reserve(skb, NCI_CTRL_HDR_SIZE);
0221 
0222         skb_put_data(skb, fw->data + (fw->size - len), payload_size);
0223 
0224         rc = nci_send_data(ndev, conn_id, skb);
0225 
0226         if (rc) {
0227             fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
0228             return rc;
0229         }
0230 
0231         len -= payload_size;
0232     }
0233 
0234     return rc;
0235 }
0236 
0237 static int fdp_nci_open(struct nci_dev *ndev)
0238 {
0239     const struct fdp_nci_info *info = nci_get_drvdata(ndev);
0240 
0241     return info->phy_ops->enable(info->phy);
0242 }
0243 
0244 static int fdp_nci_close(struct nci_dev *ndev)
0245 {
0246     return 0;
0247 }
0248 
0249 static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
0250 {
0251     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0252 
0253     if (atomic_dec_and_test(&info->data_pkt_counter))
0254         info->data_pkt_counter_cb(ndev);
0255 
0256     return info->phy_ops->write(info->phy, skb);
0257 }
0258 
0259 static int fdp_nci_request_firmware(struct nci_dev *ndev)
0260 {
0261     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0262     struct device *dev = &info->phy->i2c_dev->dev;
0263     const u8 *data;
0264     int r;
0265 
0266     r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
0267     if (r < 0) {
0268         nfc_err(dev, "RAM patch request error\n");
0269         return r;
0270     }
0271 
0272     data = info->ram_patch->data;
0273     info->ram_patch_version =
0274         data[FDP_FW_HEADER_SIZE] |
0275         (data[FDP_FW_HEADER_SIZE + 1] << 8) |
0276         (data[FDP_FW_HEADER_SIZE + 2] << 16) |
0277         (data[FDP_FW_HEADER_SIZE + 3] << 24);
0278 
0279     dev_dbg(dev, "RAM patch version: %d, size: %zu\n",
0280           info->ram_patch_version, info->ram_patch->size);
0281 
0282 
0283     r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
0284     if (r < 0) {
0285         nfc_err(dev, "OTP patch request error\n");
0286         return 0;
0287     }
0288 
0289     data = (u8 *) info->otp_patch->data;
0290     info->otp_patch_version =
0291         data[FDP_FW_HEADER_SIZE] |
0292         (data[FDP_FW_HEADER_SIZE + 1] << 8) |
0293         (data[FDP_FW_HEADER_SIZE+2] << 16) |
0294         (data[FDP_FW_HEADER_SIZE+3] << 24);
0295 
0296     dev_dbg(dev, "OTP patch version: %d, size: %zu\n",
0297          info->otp_patch_version, info->otp_patch->size);
0298     return 0;
0299 }
0300 
0301 static void fdp_nci_release_firmware(struct nci_dev *ndev)
0302 {
0303     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0304 
0305     if (info->otp_patch) {
0306         release_firmware(info->otp_patch);
0307         info->otp_patch = NULL;
0308     }
0309 
0310     if (info->ram_patch) {
0311         release_firmware(info->ram_patch);
0312         info->ram_patch = NULL;
0313     }
0314 }
0315 
0316 static int fdp_nci_patch_otp(struct nci_dev *ndev)
0317 {
0318     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0319     struct device *dev = &info->phy->i2c_dev->dev;
0320     int conn_id;
0321     int r = 0;
0322 
0323     if (info->otp_version >= info->otp_patch_version)
0324         return r;
0325 
0326     info->setup_patch_sent = 0;
0327     info->setup_reset_ntf = 0;
0328     info->setup_patch_ntf = 0;
0329 
0330     /* Patch init request */
0331     r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
0332     if (r)
0333         return r;
0334 
0335     /* Patch data connection creation */
0336     conn_id = fdp_nci_create_conn(ndev);
0337     if (conn_id < 0)
0338         return conn_id;
0339 
0340     /* Send the patch over the data connection */
0341     r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
0342     if (r)
0343         return r;
0344 
0345     /* Wait for all the packets to be send over i2c */
0346     wait_event_interruptible(info->setup_wq,
0347                  info->setup_patch_sent == 1);
0348 
0349     /* make sure that the NFCC processed the last data packet */
0350     msleep(FDP_FW_UPDATE_SLEEP);
0351 
0352     /* Close the data connection */
0353     r = nci_core_conn_close(info->ndev, conn_id);
0354     if (r)
0355         return r;
0356 
0357     /* Patch finish message */
0358     if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
0359         nfc_err(dev, "OTP patch error 0x%x\n", r);
0360         return -EINVAL;
0361     }
0362 
0363     /* If the patch notification didn't arrive yet, wait for it */
0364     wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
0365 
0366     /* Check if the patching was successful */
0367     r = info->setup_patch_status;
0368     if (r) {
0369         nfc_err(dev, "OTP patch error 0x%x\n", r);
0370         return -EINVAL;
0371     }
0372 
0373     /*
0374      * We need to wait for the reset notification before we
0375      * can continue
0376      */
0377     wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
0378 
0379     return r;
0380 }
0381 
0382 static int fdp_nci_patch_ram(struct nci_dev *ndev)
0383 {
0384     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0385     struct device *dev = &info->phy->i2c_dev->dev;
0386     int conn_id;
0387     int r = 0;
0388 
0389     if (info->ram_version >= info->ram_patch_version)
0390         return r;
0391 
0392     info->setup_patch_sent = 0;
0393     info->setup_reset_ntf = 0;
0394     info->setup_patch_ntf = 0;
0395 
0396     /* Patch init request */
0397     r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
0398     if (r)
0399         return r;
0400 
0401     /* Patch data connection creation */
0402     conn_id = fdp_nci_create_conn(ndev);
0403     if (conn_id < 0)
0404         return conn_id;
0405 
0406     /* Send the patch over the data connection */
0407     r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
0408     if (r)
0409         return r;
0410 
0411     /* Wait for all the packets to be send over i2c */
0412     wait_event_interruptible(info->setup_wq,
0413                  info->setup_patch_sent == 1);
0414 
0415     /* make sure that the NFCC processed the last data packet */
0416     msleep(FDP_FW_UPDATE_SLEEP);
0417 
0418     /* Close the data connection */
0419     r = nci_core_conn_close(info->ndev, conn_id);
0420     if (r)
0421         return r;
0422 
0423     /* Patch finish message */
0424     if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
0425         nfc_err(dev, "RAM patch error 0x%x\n", r);
0426         return -EINVAL;
0427     }
0428 
0429     /* If the patch notification didn't arrive yet, wait for it */
0430     wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
0431 
0432     /* Check if the patching was successful */
0433     r = info->setup_patch_status;
0434     if (r) {
0435         nfc_err(dev, "RAM patch error 0x%x\n", r);
0436         return -EINVAL;
0437     }
0438 
0439     /*
0440      * We need to wait for the reset notification before we
0441      * can continue
0442      */
0443     wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
0444 
0445     return r;
0446 }
0447 
0448 static int fdp_nci_setup(struct nci_dev *ndev)
0449 {
0450     /* Format: total length followed by an NCI packet */
0451     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0452     struct device *dev = &info->phy->i2c_dev->dev;
0453     int r;
0454     u8 patched = 0;
0455 
0456     r = nci_core_init(ndev);
0457     if (r)
0458         goto error;
0459 
0460     /* Get RAM and OTP version */
0461     r = fdp_nci_get_versions(ndev);
0462     if (r)
0463         goto error;
0464 
0465     /* Load firmware from disk */
0466     r = fdp_nci_request_firmware(ndev);
0467     if (r)
0468         goto error;
0469 
0470     /* Update OTP */
0471     if (info->otp_version < info->otp_patch_version) {
0472         r = fdp_nci_patch_otp(ndev);
0473         if (r)
0474             goto error;
0475         patched = 1;
0476     }
0477 
0478     /* Update RAM */
0479     if (info->ram_version < info->ram_patch_version) {
0480         r = fdp_nci_patch_ram(ndev);
0481         if (r)
0482             goto error;
0483         patched = 1;
0484     }
0485 
0486     /* Release the firmware buffers */
0487     fdp_nci_release_firmware(ndev);
0488 
0489     /* If a patch was applied the new version is checked */
0490     if (patched) {
0491         r = nci_core_init(ndev);
0492         if (r)
0493             goto error;
0494 
0495         r = fdp_nci_get_versions(ndev);
0496         if (r)
0497             goto error;
0498 
0499         if (info->otp_version != info->otp_patch_version ||
0500             info->ram_version != info->ram_patch_version) {
0501             nfc_err(dev, "Firmware update failed");
0502             r = -EINVAL;
0503             goto error;
0504         }
0505     }
0506 
0507     /*
0508      * We initialized the devices but the NFC subsystem expects
0509      * it to not be initialized.
0510      */
0511     return nci_core_reset(ndev);
0512 
0513 error:
0514     fdp_nci_release_firmware(ndev);
0515     nfc_err(dev, "Setup error %d\n", r);
0516     return r;
0517 }
0518 
0519 static int fdp_nci_post_setup(struct nci_dev *ndev)
0520 {
0521     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0522     struct device *dev = &info->phy->i2c_dev->dev;
0523     int r;
0524 
0525     /* Check if the device has VSC */
0526     if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
0527 
0528         /* Set the vendor specific configuration */
0529         r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
0530                         &info->fw_vsc_cfg[4]);
0531         if (r) {
0532             nfc_err(dev, "Vendor specific config set error %d\n",
0533                 r);
0534             return r;
0535         }
0536     }
0537 
0538     /* Set clock type and frequency */
0539     r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
0540     if (r) {
0541         nfc_err(dev, "Clock set error %d\n", r);
0542         return r;
0543     }
0544 
0545     /*
0546      * In order to apply the VSC FDP needs a reset
0547      */
0548     r = nci_core_reset(ndev);
0549     if (r)
0550         return r;
0551 
0552     /**
0553      * The nci core was initialized when post setup was called
0554      * so we leave it like that
0555      */
0556     return nci_core_init(ndev);
0557 }
0558 
0559 static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
0560                       struct sk_buff *skb)
0561 {
0562     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0563 
0564     info->setup_reset_ntf = 1;
0565     wake_up(&info->setup_wq);
0566 
0567     return 0;
0568 }
0569 
0570 static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
0571                       struct sk_buff *skb)
0572 {
0573     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0574 
0575     info->setup_patch_ntf = 1;
0576     info->setup_patch_status = skb->data[0];
0577     wake_up(&info->setup_wq);
0578 
0579     return 0;
0580 }
0581 
0582 static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
0583                       struct sk_buff *skb)
0584 {
0585     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0586     struct device *dev = &info->phy->i2c_dev->dev;
0587     u8 status = skb->data[0];
0588 
0589     dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
0590     nci_req_complete(ndev, status);
0591 
0592     return 0;
0593 }
0594 
0595 static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
0596                             struct sk_buff *skb)
0597 {
0598     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0599     struct device *dev = &info->phy->i2c_dev->dev;
0600     u8 status = skb->data[0];
0601 
0602     dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
0603     nci_req_complete(ndev, status);
0604 
0605     return 0;
0606 }
0607 
0608 static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
0609                         struct sk_buff *skb)
0610 {
0611     struct fdp_nci_info *info = nci_get_drvdata(ndev);
0612     struct device *dev = &info->phy->i2c_dev->dev;
0613     const struct nci_core_get_config_rsp *rsp = (void *) skb->data;
0614     unsigned int i;
0615     const u8 *p;
0616 
0617     if (rsp->status == NCI_STATUS_OK) {
0618 
0619         p = rsp->data;
0620         for (i = 0; i < 4; i++) {
0621 
0622             switch (*p++) {
0623             case NCI_PARAM_ID_FW_RAM_VERSION:
0624                 p++;
0625                 info->ram_version = le32_to_cpup((__le32 *) p);
0626                 p += 4;
0627                 break;
0628             case NCI_PARAM_ID_FW_OTP_VERSION:
0629                 p++;
0630                 info->otp_version = le32_to_cpup((__le32 *) p);
0631                 p += 4;
0632                 break;
0633             case NCI_PARAM_ID_OTP_LIMITED_VERSION:
0634                 p++;
0635                 info->otp_version = le32_to_cpup((__le32 *) p);
0636                 p += 4;
0637                 break;
0638             case NCI_PARAM_ID_KEY_INDEX_ID:
0639                 p++;
0640                 info->key_index = *p++;
0641             }
0642         }
0643     }
0644 
0645     dev_dbg(dev, "OTP version %d\n", info->otp_version);
0646     dev_dbg(dev, "RAM version %d\n", info->ram_version);
0647     dev_dbg(dev, "key index %d\n", info->key_index);
0648     dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
0649 
0650     nci_req_complete(ndev, rsp->status);
0651 
0652     return 0;
0653 }
0654 
0655 static const struct nci_driver_ops fdp_core_ops[] = {
0656     {
0657         .opcode = NCI_OP_CORE_GET_CONFIG_RSP,
0658         .rsp = fdp_nci_core_get_config_rsp_packet,
0659     },
0660     {
0661         .opcode = NCI_OP_CORE_RESET_NTF,
0662         .ntf = fdp_nci_core_reset_ntf_packet,
0663     },
0664 };
0665 
0666 static const struct nci_driver_ops fdp_prop_ops[] = {
0667     {
0668         .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
0669         .rsp = fdp_nci_prop_patch_rsp_packet,
0670         .ntf = fdp_nci_prop_patch_ntf_packet,
0671     },
0672     {
0673         .opcode = nci_opcode_pack(NCI_GID_PROP,
0674                       NCI_OP_PROP_SET_PDATA_OID),
0675         .rsp = fdp_nci_prop_set_production_data_rsp_packet,
0676     },
0677 };
0678 
0679 static const struct nci_ops nci_ops = {
0680     .open = fdp_nci_open,
0681     .close = fdp_nci_close,
0682     .send = fdp_nci_send,
0683     .setup = fdp_nci_setup,
0684     .post_setup = fdp_nci_post_setup,
0685     .prop_ops = fdp_prop_ops,
0686     .n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
0687     .core_ops = fdp_core_ops,
0688     .n_core_ops = ARRAY_SIZE(fdp_core_ops),
0689 };
0690 
0691 int fdp_nci_probe(struct fdp_i2c_phy *phy, const struct nfc_phy_ops *phy_ops,
0692             struct nci_dev **ndevp, int tx_headroom,
0693             int tx_tailroom, u8 clock_type, u32 clock_freq,
0694             const u8 *fw_vsc_cfg)
0695 {
0696     struct device *dev = &phy->i2c_dev->dev;
0697     struct fdp_nci_info *info;
0698     struct nci_dev *ndev;
0699     u32 protocols;
0700     int r;
0701 
0702     info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL);
0703     if (!info)
0704         return -ENOMEM;
0705 
0706     info->phy = phy;
0707     info->phy_ops = phy_ops;
0708     info->clock_type = clock_type;
0709     info->clock_freq = clock_freq;
0710     info->fw_vsc_cfg = fw_vsc_cfg;
0711 
0712     init_waitqueue_head(&info->setup_wq);
0713 
0714     protocols = NFC_PROTO_JEWEL_MASK |
0715             NFC_PROTO_MIFARE_MASK |
0716             NFC_PROTO_FELICA_MASK |
0717             NFC_PROTO_ISO14443_MASK |
0718             NFC_PROTO_ISO14443_B_MASK |
0719             NFC_PROTO_NFC_DEP_MASK |
0720             NFC_PROTO_ISO15693_MASK;
0721 
0722     BUILD_BUG_ON(ARRAY_SIZE(fdp_prop_ops) > NCI_MAX_PROPRIETARY_CMD);
0723     ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
0724                    tx_tailroom);
0725     if (!ndev) {
0726         nfc_err(dev, "Cannot allocate nfc ndev\n");
0727         return -ENOMEM;
0728     }
0729 
0730     r = nci_register_device(ndev);
0731     if (r)
0732         goto err_regdev;
0733 
0734     *ndevp = ndev;
0735     info->ndev = ndev;
0736 
0737     nci_set_drvdata(ndev, info);
0738 
0739     return 0;
0740 
0741 err_regdev:
0742     nci_free_device(ndev);
0743     return r;
0744 }
0745 EXPORT_SYMBOL(fdp_nci_probe);
0746 
0747 void fdp_nci_remove(struct nci_dev *ndev)
0748 {
0749     nci_unregister_device(ndev);
0750     nci_free_device(ndev);
0751 }
0752 EXPORT_SYMBOL(fdp_nci_remove);
0753 
0754 MODULE_LICENSE("GPL");
0755 MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
0756 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");