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