blob: ca9871ab3fb3c7040188d6ee8c6404bb26d876e5 [file] [log] [blame]
Christophe Ricard35630df2014-05-25 22:35:38 +02001/*
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"
Christophe Ricard8ae01f72015-02-01 22:26:15 +010025#include "st21nfcb_se.h"
Christophe Ricard35630df2014-05-25 22:35:38 +020026
27#define DRIVER_DESC "NCI NFC driver for ST21NFCB"
28
Christophe Ricard9e87f9a2014-09-13 10:28:49 +020029#define ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 0x83
30
Christophe Ricard35630df2014-05-25 22:35:38 +020031static 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
Christophe Ricard9e87f9a2014-09-13 10:28:49 +020070static __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
Christophe Ricard35630df2014-05-25 22:35:38 +020077static struct nci_ops st21nfcb_nci_ops = {
78 .open = st21nfcb_nci_open,
79 .close = st21nfcb_nci_close,
80 .send = st21nfcb_nci_send,
Christophe Ricard9e87f9a2014-09-13 10:28:49 +020081 .get_rfprotocol = st21nfcb_nci_get_rfprotocol,
Christophe Ricard8ae01f72015-02-01 22:26:15 +010082 .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,
Christophe Ricard35630df2014-05-25 22:35:38 +020089};
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
Christophe Ricard941ec5c2014-09-13 10:28:50 +0200108 | NFC_PROTO_ISO15693_MASK
Christophe Ricard35630df2014-05-25 22:35:38 +0200109 | 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");
Christophe Ricarddf2566f2014-07-28 18:11:36 +0200115 return -ENOMEM;
Christophe Ricard35630df2014-05-25 22:35:38 +0200116 }
117 info->ndlc = ndlc;
118
119 nci_set_drvdata(ndlc->ndev, info);
120
121 r = nci_register_device(ndlc->ndev);
Christophe Ricarddf2566f2014-07-28 18:11:36 +0200122 if (r) {
123 pr_err("Cannot register nfc device to nci core\n");
124 nci_free_device(ndlc->ndev);
Christophe Ricard8ae01f72015-02-01 22:26:15 +0100125 return r;
Christophe Ricarddf2566f2014-07-28 18:11:36 +0200126 }
Christophe Ricard35630df2014-05-25 22:35:38 +0200127
Christophe Ricard8ae01f72015-02-01 22:26:15 +0100128 return st21nfcb_se_init(ndlc->ndev);
Christophe Ricard35630df2014-05-25 22:35:38 +0200129}
130EXPORT_SYMBOL_GPL(st21nfcb_nci_probe);
131
132void st21nfcb_nci_remove(struct nci_dev *ndev)
133{
134 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
135
136 nci_unregister_device(ndev);
137 nci_free_device(ndev);
138 kfree(info);
139}
140EXPORT_SYMBOL_GPL(st21nfcb_nci_remove);
141
142MODULE_LICENSE("GPL");
143MODULE_DESCRIPTION(DRIVER_DESC);