blob: 3d699cbc74354a2dcd9e9fe9107960554fadff2e [file] [log] [blame]
Eric Lapuyade67cccfe2012-09-13 17:10:00 +02001/*
2 * Link Layer Control manager
3 *
4 * Copyright (C) 2012 Intel Corporation. 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
Jeff Kirsher98b32de2013-12-06 08:56:16 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020017 */
18
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020019#include <net/nfc/llc.h>
Samuel Ortizf4f20d02012-09-18 19:17:33 +020020
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020021#include "llc.h"
22
Axel Lin0b51fc52014-02-22 22:14:18 +080023static LIST_HEAD(llc_engines);
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020024
25int nfc_llc_init(void)
26{
Eric Lapuyade4a61cd62012-09-13 17:11:37 +020027 int r;
28
Eric Lapuyade4a61cd62012-09-13 17:11:37 +020029 r = nfc_llc_nop_register();
30 if (r)
31 goto exit;
32
33 r = nfc_llc_shdlc_register();
34 if (r)
35 goto exit;
36
37 return 0;
38
39exit:
40 nfc_llc_exit();
41 return r;
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020042}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020043
44void nfc_llc_exit(void)
45{
46 struct nfc_llc_engine *llc_engine, *n;
47
48 list_for_each_entry_safe(llc_engine, n, &llc_engines, entry) {
49 list_del(&llc_engine->entry);
50 kfree(llc_engine->name);
51 kfree(llc_engine);
52 }
53}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020054
55int nfc_llc_register(const char *name, struct nfc_llc_ops *ops)
56{
57 struct nfc_llc_engine *llc_engine;
58
59 llc_engine = kzalloc(sizeof(struct nfc_llc_engine), GFP_KERNEL);
60 if (llc_engine == NULL)
61 return -ENOMEM;
62
63 llc_engine->name = kstrdup(name, GFP_KERNEL);
64 if (llc_engine->name == NULL) {
65 kfree(llc_engine);
66 return -ENOMEM;
67 }
68 llc_engine->ops = ops;
69
70 INIT_LIST_HEAD(&llc_engine->entry);
Szymon Janc0f450772012-10-17 15:23:39 +020071 list_add_tail(&llc_engine->entry, &llc_engines);
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020072
73 return 0;
74}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +020075
76static struct nfc_llc_engine *nfc_llc_name_to_engine(const char *name)
77{
78 struct nfc_llc_engine *llc_engine;
79
80 list_for_each_entry(llc_engine, &llc_engines, entry) {
81 if (strcmp(llc_engine->name, name) == 0)
82 return llc_engine;
83 }
84
85 return NULL;
86}
87
88void nfc_llc_unregister(const char *name)
89{
90 struct nfc_llc_engine *llc_engine;
91
92 llc_engine = nfc_llc_name_to_engine(name);
93 if (llc_engine == NULL)
94 return;
95
96 list_del(&llc_engine->entry);
97 kfree(llc_engine->name);
98 kfree(llc_engine);
99}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200100
101struct nfc_llc *nfc_llc_allocate(const char *name, struct nfc_hci_dev *hdev,
102 xmit_to_drv_t xmit_to_drv,
103 rcv_to_hci_t rcv_to_hci, int tx_headroom,
104 int tx_tailroom, llc_failure_t llc_failure)
105{
106 struct nfc_llc_engine *llc_engine;
107 struct nfc_llc *llc;
108
109 llc_engine = nfc_llc_name_to_engine(name);
110 if (llc_engine == NULL)
111 return NULL;
112
113 llc = kzalloc(sizeof(struct nfc_llc), GFP_KERNEL);
114 if (llc == NULL)
115 return NULL;
116
117 llc->data = llc_engine->ops->init(hdev, xmit_to_drv, rcv_to_hci,
118 tx_headroom, tx_tailroom,
119 &llc->rx_headroom, &llc->rx_tailroom,
120 llc_failure);
121 if (llc->data == NULL) {
122 kfree(llc);
123 return NULL;
124 }
125 llc->ops = llc_engine->ops;
126
127 return llc;
128}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200129
130void nfc_llc_free(struct nfc_llc *llc)
131{
132 llc->ops->deinit(llc);
133 kfree(llc);
134}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200135
Denys Vlasenkof86dec92016-04-15 18:14:25 +0200136int nfc_llc_start(struct nfc_llc *llc)
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200137{
138 return llc->ops->start(llc);
139}
Christophe Ricard15d17172015-10-26 07:50:11 +0100140EXPORT_SYMBOL(nfc_llc_start);
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200141
Denys Vlasenkof86dec92016-04-15 18:14:25 +0200142int nfc_llc_stop(struct nfc_llc *llc)
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200143{
144 return llc->ops->stop(llc);
145}
Christophe Ricard15d17172015-10-26 07:50:11 +0100146EXPORT_SYMBOL(nfc_llc_stop);
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200147
Denys Vlasenkof86dec92016-04-15 18:14:25 +0200148void nfc_llc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200149{
150 llc->ops->rcv_from_drv(llc, skb);
151}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200152
Denys Vlasenkof86dec92016-04-15 18:14:25 +0200153int nfc_llc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200154{
155 return llc->ops->xmit_from_hci(llc, skb);
156}
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200157
Denys Vlasenkof86dec92016-04-15 18:14:25 +0200158void *nfc_llc_get_data(struct nfc_llc *llc)
Eric Lapuyade67cccfe2012-09-13 17:10:00 +0200159{
160 return llc->data;
161}