]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/nfc/st21nfcb/st21nfcb.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[mirror_ubuntu-bionic-kernel.git] / drivers / nfc / st21nfcb / st21nfcb.c
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
26 #define DRIVER_DESC "NCI NFC driver for ST21NFCB"
27
28 #define ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 0x83
29
30 static int st21nfcb_nci_open(struct nci_dev *ndev)
31 {
32 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
33 int r;
34
35 if (test_and_set_bit(ST21NFCB_NCI_RUNNING, &info->flags))
36 return 0;
37
38 r = ndlc_open(info->ndlc);
39 if (r)
40 clear_bit(ST21NFCB_NCI_RUNNING, &info->flags);
41
42 return r;
43 }
44
45 static int st21nfcb_nci_close(struct nci_dev *ndev)
46 {
47 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
48
49 if (!test_and_clear_bit(ST21NFCB_NCI_RUNNING, &info->flags))
50 return 0;
51
52 ndlc_close(info->ndlc);
53
54 return 0;
55 }
56
57 static int st21nfcb_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
58 {
59 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
60
61 skb->dev = (void *)ndev;
62
63 if (!test_bit(ST21NFCB_NCI_RUNNING, &info->flags))
64 return -EBUSY;
65
66 return ndlc_send(info->ndlc, skb);
67 }
68
69 static __u32 st21nfcb_nci_get_rfprotocol(struct nci_dev *ndev,
70 __u8 rf_protocol)
71 {
72 return rf_protocol == ST21NFCB_NCI1_X_PROPRIETARY_ISO15693 ?
73 NFC_PROTO_ISO15693_MASK : 0;
74 }
75
76 static struct nci_ops st21nfcb_nci_ops = {
77 .open = st21nfcb_nci_open,
78 .close = st21nfcb_nci_close,
79 .send = st21nfcb_nci_send,
80 .get_rfprotocol = st21nfcb_nci_get_rfprotocol,
81 };
82
83 int st21nfcb_nci_probe(struct llt_ndlc *ndlc, int phy_headroom,
84 int phy_tailroom)
85 {
86 struct st21nfcb_nci_info *info;
87 int r;
88 u32 protocols;
89
90 info = devm_kzalloc(ndlc->dev,
91 sizeof(struct st21nfcb_nci_info), GFP_KERNEL);
92 if (!info)
93 return -ENOMEM;
94
95 protocols = NFC_PROTO_JEWEL_MASK
96 | NFC_PROTO_MIFARE_MASK
97 | NFC_PROTO_FELICA_MASK
98 | NFC_PROTO_ISO14443_MASK
99 | NFC_PROTO_ISO14443_B_MASK
100 | NFC_PROTO_ISO15693_MASK
101 | NFC_PROTO_NFC_DEP_MASK;
102
103 ndlc->ndev = nci_allocate_device(&st21nfcb_nci_ops, protocols,
104 phy_headroom, phy_tailroom);
105 if (!ndlc->ndev) {
106 pr_err("Cannot allocate nfc ndev\n");
107 return -ENOMEM;
108 }
109 info->ndlc = ndlc;
110
111 nci_set_drvdata(ndlc->ndev, info);
112
113 r = nci_register_device(ndlc->ndev);
114 if (r) {
115 pr_err("Cannot register nfc device to nci core\n");
116 nci_free_device(ndlc->ndev);
117 }
118
119 return r;
120 }
121 EXPORT_SYMBOL_GPL(st21nfcb_nci_probe);
122
123 void st21nfcb_nci_remove(struct nci_dev *ndev)
124 {
125 struct st21nfcb_nci_info *info = nci_get_drvdata(ndev);
126
127 nci_unregister_device(ndev);
128 nci_free_device(ndev);
129 kfree(info);
130 }
131 EXPORT_SYMBOL_GPL(st21nfcb_nci_remove);
132
133 MODULE_LICENSE("GPL");
134 MODULE_DESCRIPTION(DRIVER_DESC);