xref: /openbmc/linux/drivers/nfc/st-nci/core.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
146fe7771SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ed06aeefSChristophe Ricard /*
3ed06aeefSChristophe Ricard  * NCI based Driver for STMicroelectronics NFC Chip
4ed06aeefSChristophe Ricard  *
5ed06aeefSChristophe Ricard  * Copyright (C) 2014-2015  STMicroelectronics SAS. All rights reserved.
6ed06aeefSChristophe Ricard  */
7ed06aeefSChristophe Ricard 
8ed06aeefSChristophe Ricard #include <linux/module.h>
9ed06aeefSChristophe Ricard #include <linux/nfc.h>
10ed06aeefSChristophe Ricard #include <net/nfc/nci.h>
11ed06aeefSChristophe Ricard #include <net/nfc/nci_core.h>
12ed06aeefSChristophe Ricard 
13ed06aeefSChristophe Ricard #include "st-nci.h"
14ed06aeefSChristophe Ricard 
15ed06aeefSChristophe Ricard #define DRIVER_DESC "NCI NFC driver for ST_NCI"
16ed06aeefSChristophe Ricard 
17ed06aeefSChristophe Ricard #define ST_NCI1_X_PROPRIETARY_ISO15693 0x83
18ed06aeefSChristophe Ricard 
st_nci_init(struct nci_dev * ndev)19ed06aeefSChristophe Ricard static int st_nci_init(struct nci_dev *ndev)
20ed06aeefSChristophe Ricard {
21ed06aeefSChristophe Ricard 	struct nci_mode_set_cmd cmd;
22ed06aeefSChristophe Ricard 
23ed06aeefSChristophe Ricard 	cmd.cmd_type = ST_NCI_SET_NFC_MODE;
24ed06aeefSChristophe Ricard 	cmd.mode = 1;
25ed06aeefSChristophe Ricard 
26ed06aeefSChristophe Ricard 	return nci_prop_cmd(ndev, ST_NCI_CORE_PROP,
27ed06aeefSChristophe Ricard 			sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
28ed06aeefSChristophe Ricard }
29ed06aeefSChristophe Ricard 
st_nci_open(struct nci_dev * ndev)30ed06aeefSChristophe Ricard static int st_nci_open(struct nci_dev *ndev)
31ed06aeefSChristophe Ricard {
32ed06aeefSChristophe Ricard 	struct st_nci_info *info = nci_get_drvdata(ndev);
33ed06aeefSChristophe Ricard 	int r;
34ed06aeefSChristophe Ricard 
35ed06aeefSChristophe Ricard 	if (test_and_set_bit(ST_NCI_RUNNING, &info->flags))
36ed06aeefSChristophe Ricard 		return 0;
37ed06aeefSChristophe Ricard 
38ed06aeefSChristophe Ricard 	r = ndlc_open(info->ndlc);
39ed06aeefSChristophe Ricard 	if (r)
40ed06aeefSChristophe Ricard 		clear_bit(ST_NCI_RUNNING, &info->flags);
41ed06aeefSChristophe Ricard 
42ed06aeefSChristophe Ricard 	return r;
43ed06aeefSChristophe Ricard }
44ed06aeefSChristophe Ricard 
st_nci_close(struct nci_dev * ndev)45ed06aeefSChristophe Ricard static int st_nci_close(struct nci_dev *ndev)
46ed06aeefSChristophe Ricard {
47ed06aeefSChristophe Ricard 	struct st_nci_info *info = nci_get_drvdata(ndev);
48ed06aeefSChristophe Ricard 
49ed06aeefSChristophe Ricard 	if (!test_bit(ST_NCI_RUNNING, &info->flags))
50ed06aeefSChristophe Ricard 		return 0;
51ed06aeefSChristophe Ricard 
52ed06aeefSChristophe Ricard 	ndlc_close(info->ndlc);
53ed06aeefSChristophe Ricard 
54ed06aeefSChristophe Ricard 	clear_bit(ST_NCI_RUNNING, &info->flags);
55ed06aeefSChristophe Ricard 
56ed06aeefSChristophe Ricard 	return 0;
57ed06aeefSChristophe Ricard }
58ed06aeefSChristophe Ricard 
st_nci_send(struct nci_dev * ndev,struct sk_buff * skb)59ed06aeefSChristophe Ricard static int st_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
60ed06aeefSChristophe Ricard {
61ed06aeefSChristophe Ricard 	struct st_nci_info *info = nci_get_drvdata(ndev);
62ed06aeefSChristophe Ricard 
63ed06aeefSChristophe Ricard 	skb->dev = (void *)ndev;
64ed06aeefSChristophe Ricard 
65ed06aeefSChristophe Ricard 	if (!test_bit(ST_NCI_RUNNING, &info->flags))
66ed06aeefSChristophe Ricard 		return -EBUSY;
67ed06aeefSChristophe Ricard 
68ed06aeefSChristophe Ricard 	return ndlc_send(info->ndlc, skb);
69ed06aeefSChristophe Ricard }
70ed06aeefSChristophe Ricard 
st_nci_get_rfprotocol(struct nci_dev * ndev,__u8 rf_protocol)71ed06aeefSChristophe Ricard static __u32 st_nci_get_rfprotocol(struct nci_dev *ndev,
72ed06aeefSChristophe Ricard 					 __u8 rf_protocol)
73ed06aeefSChristophe Ricard {
74ed06aeefSChristophe Ricard 	return rf_protocol == ST_NCI1_X_PROPRIETARY_ISO15693 ?
75ed06aeefSChristophe Ricard 		NFC_PROTO_ISO15693_MASK : 0;
76ed06aeefSChristophe Ricard }
77ed06aeefSChristophe Ricard 
st_nci_prop_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)78ed06aeefSChristophe Ricard static int st_nci_prop_rsp_packet(struct nci_dev *ndev,
79ed06aeefSChristophe Ricard 					struct sk_buff *skb)
80ed06aeefSChristophe Ricard {
81ed06aeefSChristophe Ricard 	__u8 status = skb->data[0];
82ed06aeefSChristophe Ricard 
83ed06aeefSChristophe Ricard 	nci_req_complete(ndev, status);
84ed06aeefSChristophe Ricard 	return 0;
85ed06aeefSChristophe Ricard }
86ed06aeefSChristophe Ricard 
87*cb8caa3cSKrzysztof Kozlowski static const struct nci_driver_ops st_nci_prop_ops[] = {
88ed06aeefSChristophe Ricard 	{
89ed06aeefSChristophe Ricard 		.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
90ed06aeefSChristophe Ricard 					  ST_NCI_CORE_PROP),
91ed06aeefSChristophe Ricard 		.rsp = st_nci_prop_rsp_packet,
92ed06aeefSChristophe Ricard 	},
93ed06aeefSChristophe Ricard };
94ed06aeefSChristophe Ricard 
95b9c28286SKrzysztof Kozlowski static const struct nci_ops st_nci_ops = {
96ed06aeefSChristophe Ricard 	.init = st_nci_init,
97ed06aeefSChristophe Ricard 	.open = st_nci_open,
98ed06aeefSChristophe Ricard 	.close = st_nci_close,
99ed06aeefSChristophe Ricard 	.send = st_nci_send,
100ed06aeefSChristophe Ricard 	.get_rfprotocol = st_nci_get_rfprotocol,
101ed06aeefSChristophe Ricard 	.discover_se = st_nci_discover_se,
102ed06aeefSChristophe Ricard 	.enable_se = st_nci_enable_se,
103ed06aeefSChristophe Ricard 	.disable_se = st_nci_disable_se,
104ed06aeefSChristophe Ricard 	.se_io = st_nci_se_io,
105ed06aeefSChristophe Ricard 	.hci_load_session = st_nci_hci_load_session,
106ed06aeefSChristophe Ricard 	.hci_event_received = st_nci_hci_event_received,
107ed06aeefSChristophe Ricard 	.hci_cmd_received = st_nci_hci_cmd_received,
108ed06aeefSChristophe Ricard 	.prop_ops = st_nci_prop_ops,
109ed06aeefSChristophe Ricard 	.n_prop_ops = ARRAY_SIZE(st_nci_prop_ops),
110ed06aeefSChristophe Ricard };
111ed06aeefSChristophe Ricard 
st_nci_probe(struct llt_ndlc * ndlc,int phy_headroom,int phy_tailroom,struct st_nci_se_status * se_status)112ed06aeefSChristophe Ricard int st_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
1133648dc6dSChristophe Ricard 		 int phy_tailroom, struct st_nci_se_status *se_status)
114ed06aeefSChristophe Ricard {
115ed06aeefSChristophe Ricard 	struct st_nci_info *info;
116ed06aeefSChristophe Ricard 	int r;
117ed06aeefSChristophe Ricard 	u32 protocols;
118ed06aeefSChristophe Ricard 
119ed06aeefSChristophe Ricard 	info = devm_kzalloc(ndlc->dev,
120ed06aeefSChristophe Ricard 			sizeof(struct st_nci_info), GFP_KERNEL);
121ed06aeefSChristophe Ricard 	if (!info)
122ed06aeefSChristophe Ricard 		return -ENOMEM;
123ed06aeefSChristophe Ricard 
124ed06aeefSChristophe Ricard 	protocols = NFC_PROTO_JEWEL_MASK
125ed06aeefSChristophe Ricard 		| NFC_PROTO_MIFARE_MASK
126ed06aeefSChristophe Ricard 		| NFC_PROTO_FELICA_MASK
127ed06aeefSChristophe Ricard 		| NFC_PROTO_ISO14443_MASK
128ed06aeefSChristophe Ricard 		| NFC_PROTO_ISO14443_B_MASK
129ed06aeefSChristophe Ricard 		| NFC_PROTO_ISO15693_MASK
130ed06aeefSChristophe Ricard 		| NFC_PROTO_NFC_DEP_MASK;
131ed06aeefSChristophe Ricard 
132b9c28286SKrzysztof Kozlowski 	BUILD_BUG_ON(ARRAY_SIZE(st_nci_prop_ops) > NCI_MAX_PROPRIETARY_CMD);
133ed06aeefSChristophe Ricard 	ndlc->ndev = nci_allocate_device(&st_nci_ops, protocols,
134ed06aeefSChristophe Ricard 					phy_headroom, phy_tailroom);
135ed06aeefSChristophe Ricard 	if (!ndlc->ndev) {
136ed06aeefSChristophe Ricard 		pr_err("Cannot allocate nfc ndev\n");
137ed06aeefSChristophe Ricard 		return -ENOMEM;
138ed06aeefSChristophe Ricard 	}
139ed06aeefSChristophe Ricard 	info->ndlc = ndlc;
140ed06aeefSChristophe Ricard 
141ed06aeefSChristophe Ricard 	nci_set_drvdata(ndlc->ndev, info);
142ed06aeefSChristophe Ricard 
143b1fa4dc4SChristophe Ricard 	r = st_nci_vendor_cmds_init(ndlc->ndev);
144b1fa4dc4SChristophe Ricard 	if (r) {
145b1fa4dc4SChristophe Ricard 		pr_err("Cannot register proprietary vendor cmds\n");
146b1fa4dc4SChristophe Ricard 		goto err_reg_dev;
147b1fa4dc4SChristophe Ricard 	}
148b1fa4dc4SChristophe Ricard 
149ed06aeefSChristophe Ricard 	r = nci_register_device(ndlc->ndev);
150ed06aeefSChristophe Ricard 	if (r) {
151ed06aeefSChristophe Ricard 		pr_err("Cannot register nfc device to nci core\n");
152b1fa4dc4SChristophe Ricard 		goto err_reg_dev;
153ed06aeefSChristophe Ricard 	}
154ed06aeefSChristophe Ricard 
1553648dc6dSChristophe Ricard 	return st_nci_se_init(ndlc->ndev, se_status);
156b1fa4dc4SChristophe Ricard 
157b1fa4dc4SChristophe Ricard err_reg_dev:
158b1fa4dc4SChristophe Ricard 	nci_free_device(ndlc->ndev);
159b1fa4dc4SChristophe Ricard 	return r;
160ed06aeefSChristophe Ricard }
161ed06aeefSChristophe Ricard EXPORT_SYMBOL_GPL(st_nci_probe);
162ed06aeefSChristophe Ricard 
st_nci_remove(struct nci_dev * ndev)163ed06aeefSChristophe Ricard void st_nci_remove(struct nci_dev *ndev)
164ed06aeefSChristophe Ricard {
165ed06aeefSChristophe Ricard 	struct st_nci_info *info = nci_get_drvdata(ndev);
166ed06aeefSChristophe Ricard 
167ed06aeefSChristophe Ricard 	ndlc_close(info->ndlc);
168ed06aeefSChristophe Ricard 
169ed06aeefSChristophe Ricard 	nci_unregister_device(ndev);
170ed06aeefSChristophe Ricard 	nci_free_device(ndev);
171ed06aeefSChristophe Ricard }
172ed06aeefSChristophe Ricard EXPORT_SYMBOL_GPL(st_nci_remove);
173ed06aeefSChristophe Ricard 
174ed06aeefSChristophe Ricard MODULE_LICENSE("GPL");
175ed06aeefSChristophe Ricard MODULE_DESCRIPTION(DRIVER_DESC);
176