xref: /openbmc/linux/drivers/nfc/st-nci/core.c (revision e3b9f1e8)
1 /*
2  * NCI based Driver for STMicroelectronics NFC Chip
3  *
4  * Copyright (C) 2014-2015  STMicroelectronics SAS. All rights reserved.
5  *
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.
9  *
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.
14  *
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/>.
17  */
18 
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>
25 
26 #include "st-nci.h"
27 
28 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
29 
30 #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
31 
32 static int st_nci_init(struct nci_dev *ndev)
33 {
34 	struct nci_mode_set_cmd cmd;
35 
36 	cmd.cmd_type = ST_NCI_SET_NFC_MODE;
37 	cmd.mode = 1;
38 
39 	return nci_prop_cmd(ndev, ST_NCI_CORE_PROP,
40 			sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
41 }
42 
43 static int st_nci_open(struct nci_dev *ndev)
44 {
45 	struct st_nci_info *info = nci_get_drvdata(ndev);
46 	int r;
47 
48 	if (test_and_set_bit(ST_NCI_RUNNING, &info->flags))
49 		return 0;
50 
51 	r = ndlc_open(info->ndlc);
52 	if (r)
53 		clear_bit(ST_NCI_RUNNING, &info->flags);
54 
55 	return r;
56 }
57 
58 static int st_nci_close(struct nci_dev *ndev)
59 {
60 	struct st_nci_info *info = nci_get_drvdata(ndev);
61 
62 	if (!test_bit(ST_NCI_RUNNING, &info->flags))
63 		return 0;
64 
65 	ndlc_close(info->ndlc);
66 
67 	clear_bit(ST_NCI_RUNNING, &info->flags);
68 
69 	return 0;
70 }
71 
72 static int st_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
73 {
74 	struct st_nci_info *info = nci_get_drvdata(ndev);
75 
76 	skb->dev = (void *)ndev;
77 
78 	if (!test_bit(ST_NCI_RUNNING, &info->flags))
79 		return -EBUSY;
80 
81 	return ndlc_send(info->ndlc, skb);
82 }
83 
84 static __u32 st_nci_get_rfprotocol(struct nci_dev *ndev,
85 					 __u8 rf_protocol)
86 {
87 	return rf_protocol == ST_NCI1_X_PROPRIETARY_ISO15693 ?
88 		NFC_PROTO_ISO15693_MASK : 0;
89 }
90 
91 static int st_nci_prop_rsp_packet(struct nci_dev *ndev,
92 					struct sk_buff *skb)
93 {
94 	__u8 status = skb->data[0];
95 
96 	nci_req_complete(ndev, status);
97 	return 0;
98 }
99 
100 static struct nci_driver_ops st_nci_prop_ops[] = {
101 	{
102 		.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
103 					  ST_NCI_CORE_PROP),
104 		.rsp = st_nci_prop_rsp_packet,
105 	},
106 };
107 
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),
123 };
124 
125 int st_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
126 		 int phy_tailroom, struct st_nci_se_status *se_status)
127 {
128 	struct st_nci_info *info;
129 	int r;
130 	u32 protocols;
131 
132 	info = devm_kzalloc(ndlc->dev,
133 			sizeof(struct st_nci_info), GFP_KERNEL);
134 	if (!info)
135 		return -ENOMEM;
136 
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;
144 
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;
150 	}
151 	info->ndlc = ndlc;
152 
153 	nci_set_drvdata(ndlc->ndev, info);
154 
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;
159 	}
160 
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;
165 	}
166 
167 	return st_nci_se_init(ndlc->ndev, se_status);
168 
169 err_reg_dev:
170 	nci_free_device(ndlc->ndev);
171 	return r;
172 }
173 EXPORT_SYMBOL_GPL(st_nci_probe);
174 
175 void st_nci_remove(struct nci_dev *ndev)
176 {
177 	struct st_nci_info *info = nci_get_drvdata(ndev);
178 
179 	ndlc_close(info->ndlc);
180 
181 	nci_unregister_device(ndev);
182 	nci_free_device(ndev);
183 }
184 EXPORT_SYMBOL_GPL(st_nci_remove);
185 
186 MODULE_LICENSE("GPL");
187 MODULE_DESCRIPTION(DRIVER_DESC);
188