1/*
2 * NCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014  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
24#include "st21nfcb.h"
25#include "st21nfcb_se.h"
26
27#define DRIVER_DESC "NCI NFC driver for ST21NFCB"
28
29#define ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 0x83
30
31static int st21nfcb_nci_open(struct nci_dev *ndev)
32{
33	struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
34	int r;
35
36	if (test_and_set_bit(ST21NFCB_NCI_RUNNING, &info->flags))
37		return 0;
38
39	r = ndlc_open(info->ndlc);
40	if (r)
41		clear_bit(ST21NFCB_NCI_RUNNING, &info->flags);
42
43	return r;
44}
45
46static int st21nfcb_nci_close(struct nci_dev *ndev)
47{
48	struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
49
50	if (!test_and_clear_bit(ST21NFCB_NCI_RUNNING, &info->flags))
51		return 0;
52
53	ndlc_close(info->ndlc);
54
55	return 0;
56}
57
58static int st21nfcb_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
59{
60	struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
61
62	skb->dev = (void *)ndev;
63
64	if (!test_bit(ST21NFCB_NCI_RUNNING, &info->flags))
65		return -EBUSY;
66
67	return ndlc_send(info->ndlc, skb);
68}
69
70static __u32 st21nfcb_nci_get_rfprotocol(struct nci_dev *ndev,
71					 __u8 rf_protocol)
72{
73	return rf_protocol == ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 ?
74		NFC_PROTO_ISO15693_MASK : 0;
75}
76
77static struct nci_ops st21nfcb_nci_ops = {
78	.open = st21nfcb_nci_open,
79	.close = st21nfcb_nci_close,
80	.send = st21nfcb_nci_send,
81	.get_rfprotocol = st21nfcb_nci_get_rfprotocol,
82	.discover_se = st21nfcb_nci_discover_se,
83	.enable_se = st21nfcb_nci_enable_se,
84	.disable_se = st21nfcb_nci_disable_se,
85	.se_io = st21nfcb_nci_se_io,
86	.hci_load_session = st21nfcb_hci_load_session,
87	.hci_event_received = st21nfcb_hci_event_received,
88	.hci_cmd_received = st21nfcb_hci_cmd_received,
89};
90
91int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
92		       int phy_tailroom)
93{
94	struct st21nfcb_nci_info *info;
95	int r;
96	u32 protocols;
97
98	info = devm_kzalloc(ndlc->dev,
99			sizeof(struct st21nfcb_nci_info), GFP_KERNEL);
100	if (!info)
101		return -ENOMEM;
102
103	protocols = NFC_PROTO_JEWEL_MASK
104		| NFC_PROTO_MIFARE_MASK
105		| NFC_PROTO_FELICA_MASK
106		| NFC_PROTO_ISO14443_MASK
107		| NFC_PROTO_ISO14443_B_MASK
108		| NFC_PROTO_ISO15693_MASK
109		| NFC_PROTO_NFC_DEP_MASK;
110
111	ndlc->ndev = nci_allocate_device(&st21nfcb_nci_ops, protocols,
112					phy_headroom, phy_tailroom);
113	if (!ndlc->ndev) {
114		pr_err("Cannot allocate nfc ndev\n");
115		return -ENOMEM;
116	}
117	info->ndlc = ndlc;
118
119	nci_set_drvdata(ndlc->ndev, info);
120
121	r = nci_register_device(ndlc->ndev);
122	if (r) {
123		pr_err("Cannot register nfc device to nci core\n");
124		nci_free_device(ndlc->ndev);
125		return r;
126	}
127
128	return st21nfcb_se_init(ndlc->ndev);
129}
130EXPORT_SYMBOL_GPL(st21nfcb_nci_probe);
131
132void st21nfcb_nci_remove(struct nci_dev *ndev)
133{
134	nci_unregister_device(ndev);
135	nci_free_device(ndev);
136}
137EXPORT_SYMBOL_GPL(st21nfcb_nci_remove);
138
139MODULE_LICENSE("GPL");
140MODULE_DESCRIPTION(DRIVER_DESC);
141