Linux 4.19.133
[linux/fpc-iii.git] / drivers / nfc / st-nci / core.c
blobc693128ee6fb90825abcecee9a436f31dafe0a9a
1 /*
2 * NCI based Driver for STMicroelectronics NFC Chip
4 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
20 #include <linux/nfc.h>
21 #include <net/nfc/nci.h>
22 #include <net/nfc/nci_core.h>
23 #include <linux/gpio.h>
24 #include <linux/delay.h>
26 #include "st-nci.h"
28 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
30 #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
32 static int st_nci_init(struct nci_dev *ndev)
34 struct nci_mode_set_cmd cmd;
36 cmd.cmd_type = ST_NCI_SET_NFC_MODE;
37 cmd.mode = 1;
39 return nci_prop_cmd(ndev, ST_NCI_CORE_PROP,
40 sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
43 static int st_nci_open(struct nci_dev *ndev)
45 struct st_nci_info *info = nci_get_drvdata(ndev);
46 int r;
48 if (test_and_set_bit(ST_NCI_RUNNING, &info->flags))
49 return 0;
51 r = ndlc_open(info->ndlc);
52 if (r)
53 clear_bit(ST_NCI_RUNNING, &info->flags);
55 return r;
58 static int st_nci_close(struct nci_dev *ndev)
60 struct st_nci_info *info = nci_get_drvdata(ndev);
62 if (!test_bit(ST_NCI_RUNNING, &info->flags))
63 return 0;
65 ndlc_close(info->ndlc);
67 clear_bit(ST_NCI_RUNNING, &info->flags);
69 return 0;
72 static int st_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
74 struct st_nci_info *info = nci_get_drvdata(ndev);
76 skb->dev = (void *)ndev;
78 if (!test_bit(ST_NCI_RUNNING, &info->flags))
79 return -EBUSY;
81 return ndlc_send(info->ndlc, skb);
84 static __u32 st_nci_get_rfprotocol(struct nci_dev *ndev,
85 __u8 rf_protocol)
87 return rf_protocol == ST_NCI1_X_PROPRIETARY_ISO15693 ?
88 NFC_PROTO_ISO15693_MASK : 0;
91 static int st_nci_prop_rsp_packet(struct nci_dev *ndev,
92 struct sk_buff *skb)
94 __u8 status = skb->data[0];
96 nci_req_complete(ndev, status);
97 return 0;
100 static struct nci_driver_ops st_nci_prop_ops[] = {
102 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
103 ST_NCI_CORE_PROP),
104 .rsp = st_nci_prop_rsp_packet,
108 static struct nci_ops st_nci_ops = {
109 .init = st_nci_init,
110 .open = st_nci_open,
111 .close = st_nci_close,
112 .send = st_nci_send,
113 .get_rfprotocol = st_nci_get_rfprotocol,
114 .discover_se = st_nci_discover_se,
115 .enable_se = st_nci_enable_se,
116 .disable_se = st_nci_disable_se,
117 .se_io = st_nci_se_io,
118 .hci_load_session = st_nci_hci_load_session,
119 .hci_event_received = st_nci_hci_event_received,
120 .hci_cmd_received = st_nci_hci_cmd_received,
121 .prop_ops = st_nci_prop_ops,
122 .n_prop_ops = ARRAY_SIZE(st_nci_prop_ops),
125 int st_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
126 int phy_tailroom, struct st_nci_se_status *se_status)
128 struct st_nci_info *info;
129 int r;
130 u32 protocols;
132 info = devm_kzalloc(ndlc->dev,
133 sizeof(struct st_nci_info), GFP_KERNEL);
134 if (!info)
135 return -ENOMEM;
137 protocols = NFC_PROTO_JEWEL_MASK
138 | NFC_PROTO_MIFARE_MASK
139 | NFC_PROTO_FELICA_MASK
140 | NFC_PROTO_ISO14443_MASK
141 | NFC_PROTO_ISO14443_B_MASK
142 | NFC_PROTO_ISO15693_MASK
143 | NFC_PROTO_NFC_DEP_MASK;
145 ndlc->ndev = nci_allocate_device(&st_nci_ops, protocols,
146 phy_headroom, phy_tailroom);
147 if (!ndlc->ndev) {
148 pr_err("Cannot allocate nfc ndev\n");
149 return -ENOMEM;
151 info->ndlc = ndlc;
153 nci_set_drvdata(ndlc->ndev, info);
155 r = st_nci_vendor_cmds_init(ndlc->ndev);
156 if (r) {
157 pr_err("Cannot register proprietary vendor cmds\n");
158 goto err_reg_dev;
161 r = nci_register_device(ndlc->ndev);
162 if (r) {
163 pr_err("Cannot register nfc device to nci core\n");
164 goto err_reg_dev;
167 return st_nci_se_init(ndlc->ndev, se_status);
169 err_reg_dev:
170 nci_free_device(ndlc->ndev);
171 return r;
173 EXPORT_SYMBOL_GPL(st_nci_probe);
175 void st_nci_remove(struct nci_dev *ndev)
177 struct st_nci_info *info = nci_get_drvdata(ndev);
179 ndlc_close(info->ndlc);
181 nci_unregister_device(ndev);
182 nci_free_device(ndev);
184 EXPORT_SYMBOL_GPL(st_nci_remove);
186 MODULE_LICENSE("GPL");
187 MODULE_DESCRIPTION(DRIVER_DESC);