xref: /openbmc/linux/drivers/nfc/st-nci/core.c (revision 93df8a1e)
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 #include "st-nci_se.h"
28 
29 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
30 
31 #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
32 
33 static int st_nci_init(struct nci_dev *ndev)
34 {
35 	struct nci_mode_set_cmd cmd;
36 
37 	cmd.cmd_type = ST_NCI_SET_NFC_MODE;
38 	cmd.mode = 1;
39 
40 	return nci_prop_cmd(ndev, ST_NCI_CORE_PROP,
41 			sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
42 }
43 
44 static int st_nci_open(struct nci_dev *ndev)
45 {
46 	struct st_nci_info *info = nci_get_drvdata(ndev);
47 	int r;
48 
49 	if (test_and_set_bit(ST_NCI_RUNNING, &info->flags))
50 		return 0;
51 
52 	r = ndlc_open(info->ndlc);
53 	if (r)
54 		clear_bit(ST_NCI_RUNNING, &info->flags);
55 
56 	return r;
57 }
58 
59 static int st_nci_close(struct nci_dev *ndev)
60 {
61 	struct st_nci_info *info = nci_get_drvdata(ndev);
62 
63 	if (!test_bit(ST_NCI_RUNNING, &info->flags))
64 		return 0;
65 
66 	ndlc_close(info->ndlc);
67 
68 	clear_bit(ST_NCI_RUNNING, &info->flags);
69 
70 	return 0;
71 }
72 
73 static int st_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
74 {
75 	struct st_nci_info *info = nci_get_drvdata(ndev);
76 
77 	skb->dev = (void *)ndev;
78 
79 	if (!test_bit(ST_NCI_RUNNING, &info->flags))
80 		return -EBUSY;
81 
82 	return ndlc_send(info->ndlc, skb);
83 }
84 
85 static __u32 st_nci_get_rfprotocol(struct nci_dev *ndev,
86 					 __u8 rf_protocol)
87 {
88 	return rf_protocol == ST_NCI1_X_PROPRIETARY_ISO15693 ?
89 		NFC_PROTO_ISO15693_MASK : 0;
90 }
91 
92 static int st_nci_prop_rsp_packet(struct nci_dev *ndev,
93 					struct sk_buff *skb)
94 {
95 	__u8 status = skb->data[0];
96 
97 	nci_req_complete(ndev, status);
98 	return 0;
99 }
100 
101 static struct nci_prop_ops st_nci_prop_ops[] = {
102 	{
103 		.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
104 					  ST_NCI_CORE_PROP),
105 		.rsp = st_nci_prop_rsp_packet,
106 	},
107 };
108 
109 static struct nci_ops st_nci_ops = {
110 	.init = st_nci_init,
111 	.open = st_nci_open,
112 	.close = st_nci_close,
113 	.send = st_nci_send,
114 	.get_rfprotocol = st_nci_get_rfprotocol,
115 	.discover_se = st_nci_discover_se,
116 	.enable_se = st_nci_enable_se,
117 	.disable_se = st_nci_disable_se,
118 	.se_io = st_nci_se_io,
119 	.hci_load_session = st_nci_hci_load_session,
120 	.hci_event_received = st_nci_hci_event_received,
121 	.hci_cmd_received = st_nci_hci_cmd_received,
122 	.prop_ops = st_nci_prop_ops,
123 	.n_prop_ops = ARRAY_SIZE(st_nci_prop_ops),
124 };
125 
126 int st_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
127 		       int phy_tailroom)
128 {
129 	struct st_nci_info *info;
130 	int r;
131 	u32 protocols;
132 
133 	info = devm_kzalloc(ndlc->dev,
134 			sizeof(struct st_nci_info), GFP_KERNEL);
135 	if (!info)
136 		return -ENOMEM;
137 
138 	protocols = NFC_PROTO_JEWEL_MASK
139 		| NFC_PROTO_MIFARE_MASK
140 		| NFC_PROTO_FELICA_MASK
141 		| NFC_PROTO_ISO14443_MASK
142 		| NFC_PROTO_ISO14443_B_MASK
143 		| NFC_PROTO_ISO15693_MASK
144 		| NFC_PROTO_NFC_DEP_MASK;
145 
146 	ndlc->ndev = nci_allocate_device(&st_nci_ops, protocols,
147 					phy_headroom, phy_tailroom);
148 	if (!ndlc->ndev) {
149 		pr_err("Cannot allocate nfc ndev\n");
150 		return -ENOMEM;
151 	}
152 	info->ndlc = ndlc;
153 
154 	nci_set_drvdata(ndlc->ndev, info);
155 
156 	r = nci_register_device(ndlc->ndev);
157 	if (r) {
158 		pr_err("Cannot register nfc device to nci core\n");
159 		nci_free_device(ndlc->ndev);
160 		return r;
161 	}
162 
163 	return st_nci_se_init(ndlc->ndev);
164 }
165 EXPORT_SYMBOL_GPL(st_nci_probe);
166 
167 void st_nci_remove(struct nci_dev *ndev)
168 {
169 	struct st_nci_info *info = nci_get_drvdata(ndev);
170 
171 	ndlc_close(info->ndlc);
172 
173 	nci_unregister_device(ndev);
174 	nci_free_device(ndev);
175 }
176 EXPORT_SYMBOL_GPL(st_nci_remove);
177 
178 MODULE_LICENSE("GPL");
179 MODULE_DESCRIPTION(DRIVER_DESC);
180