Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / nfc / s3fwrn5 / core.c
blob9d9c8d57a042d45084082e10b30f686d82b7edb4
1 /*
2 * NCI based driver for Samsung S3FWRN5 NFC chip
4 * Copyright (C) 2015 Samsung Electrnoics
5 * Robert Baldyga <r.baldyga@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/module.h>
21 #include <net/nfc/nci_core.h>
23 #include "s3fwrn5.h"
24 #include "firmware.h"
25 #include "nci.h"
27 #define S3FWRN5_NFC_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
28 NFC_PROTO_MIFARE_MASK | \
29 NFC_PROTO_FELICA_MASK | \
30 NFC_PROTO_ISO14443_MASK | \
31 NFC_PROTO_ISO14443_B_MASK | \
32 NFC_PROTO_ISO15693_MASK)
34 static int s3fwrn5_firmware_update(struct s3fwrn5_info *info)
36 bool need_update;
37 int ret;
39 s3fwrn5_fw_init(&info->fw_info, "sec_s3fwrn5_firmware.bin");
41 /* Update firmware */
43 s3fwrn5_set_wake(info, false);
44 s3fwrn5_set_mode(info, S3FWRN5_MODE_FW);
46 ret = s3fwrn5_fw_setup(&info->fw_info);
47 if (ret < 0)
48 return ret;
50 need_update = s3fwrn5_fw_check_version(&info->fw_info,
51 info->ndev->manufact_specific_info);
52 if (!need_update)
53 goto out;
55 dev_info(&info->ndev->nfc_dev->dev, "Detected new firmware version\n");
57 ret = s3fwrn5_fw_download(&info->fw_info);
58 if (ret < 0)
59 goto out;
61 /* Update RF configuration */
63 s3fwrn5_set_mode(info, S3FWRN5_MODE_NCI);
65 s3fwrn5_set_wake(info, true);
66 ret = s3fwrn5_nci_rf_configure(info, "sec_s3fwrn5_rfreg.bin");
67 s3fwrn5_set_wake(info, false);
69 out:
70 s3fwrn5_set_mode(info, S3FWRN5_MODE_COLD);
71 s3fwrn5_fw_cleanup(&info->fw_info);
72 return ret;
75 static int s3fwrn5_nci_open(struct nci_dev *ndev)
77 struct s3fwrn5_info *info = nci_get_drvdata(ndev);
79 if (s3fwrn5_get_mode(info) != S3FWRN5_MODE_COLD)
80 return -EBUSY;
82 s3fwrn5_set_mode(info, S3FWRN5_MODE_NCI);
83 s3fwrn5_set_wake(info, true);
85 return 0;
88 static int s3fwrn5_nci_close(struct nci_dev *ndev)
90 struct s3fwrn5_info *info = nci_get_drvdata(ndev);
92 s3fwrn5_set_wake(info, false);
93 s3fwrn5_set_mode(info, S3FWRN5_MODE_COLD);
95 return 0;
98 static int s3fwrn5_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
100 struct s3fwrn5_info *info = nci_get_drvdata(ndev);
101 int ret;
103 mutex_lock(&info->mutex);
105 if (s3fwrn5_get_mode(info) != S3FWRN5_MODE_NCI) {
106 mutex_unlock(&info->mutex);
107 return -EINVAL;
110 ret = s3fwrn5_write(info, skb);
111 if (ret < 0)
112 kfree_skb(skb);
114 mutex_unlock(&info->mutex);
115 return ret;
118 static int s3fwrn5_nci_post_setup(struct nci_dev *ndev)
120 struct s3fwrn5_info *info = nci_get_drvdata(ndev);
121 int ret;
123 ret = s3fwrn5_firmware_update(info);
124 if (ret < 0)
125 goto out;
127 /* NCI core reset */
129 s3fwrn5_set_mode(info, S3FWRN5_MODE_NCI);
130 s3fwrn5_set_wake(info, true);
132 ret = nci_core_reset(info->ndev);
133 if (ret < 0)
134 goto out;
136 ret = nci_core_init(info->ndev);
138 out:
139 return ret;
142 static struct nci_ops s3fwrn5_nci_ops = {
143 .open = s3fwrn5_nci_open,
144 .close = s3fwrn5_nci_close,
145 .send = s3fwrn5_nci_send,
146 .post_setup = s3fwrn5_nci_post_setup,
149 int s3fwrn5_probe(struct nci_dev **ndev, void *phy_id, struct device *pdev,
150 const struct s3fwrn5_phy_ops *phy_ops, unsigned int max_payload)
152 struct s3fwrn5_info *info;
153 int ret;
155 info = devm_kzalloc(pdev, sizeof(*info), GFP_KERNEL);
156 if (!info)
157 return -ENOMEM;
159 info->phy_id = phy_id;
160 info->pdev = pdev;
161 info->phy_ops = phy_ops;
162 info->max_payload = max_payload;
163 mutex_init(&info->mutex);
165 s3fwrn5_set_mode(info, S3FWRN5_MODE_COLD);
167 s3fwrn5_nci_get_prop_ops(&s3fwrn5_nci_ops.prop_ops,
168 &s3fwrn5_nci_ops.n_prop_ops);
170 info->ndev = nci_allocate_device(&s3fwrn5_nci_ops,
171 S3FWRN5_NFC_PROTOCOLS, 0, 0);
172 if (!info->ndev)
173 return -ENOMEM;
175 nci_set_parent_dev(info->ndev, pdev);
176 nci_set_drvdata(info->ndev, info);
178 ret = nci_register_device(info->ndev);
179 if (ret < 0) {
180 nci_free_device(info->ndev);
181 return ret;
184 info->fw_info.ndev = info->ndev;
186 *ndev = info->ndev;
188 return ret;
190 EXPORT_SYMBOL(s3fwrn5_probe);
192 void s3fwrn5_remove(struct nci_dev *ndev)
194 struct s3fwrn5_info *info = nci_get_drvdata(ndev);
196 s3fwrn5_set_mode(info, S3FWRN5_MODE_COLD);
198 nci_unregister_device(ndev);
199 nci_free_device(ndev);
201 EXPORT_SYMBOL(s3fwrn5_remove);
203 int s3fwrn5_recv_frame(struct nci_dev *ndev, struct sk_buff *skb,
204 enum s3fwrn5_mode mode)
206 switch (mode) {
207 case S3FWRN5_MODE_NCI:
208 return nci_recv_frame(ndev, skb);
209 case S3FWRN5_MODE_FW:
210 return s3fwrn5_fw_recv_frame(ndev, skb);
211 default:
212 return -ENODEV;
215 EXPORT_SYMBOL(s3fwrn5_recv_frame);
217 MODULE_LICENSE("GPL");
218 MODULE_DESCRIPTION("Samsung S3FWRN5 NFC driver");
219 MODULE_AUTHOR("Robert Baldyga <r.baldyga@samsung.com>");