Linux 4.1.16
[linux/fpc-iii.git] / net / nfc / hci / llc.c
blob1b90c05318521c76703db7ca86607decf006cbe8
1 /*
2 * Link Layer Control manager
4 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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/>.
19 #include <net/nfc/llc.h>
21 #include "llc.h"
23 static LIST_HEAD(llc_engines);
25 int nfc_llc_init(void)
27 int r;
29 r = nfc_llc_nop_register();
30 if (r)
31 goto exit;
33 r = nfc_llc_shdlc_register();
34 if (r)
35 goto exit;
37 return 0;
39 exit:
40 nfc_llc_exit();
41 return r;
44 void nfc_llc_exit(void)
46 struct nfc_llc_engine *llc_engine, *n;
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);
55 int nfc_llc_register(const char *name, struct nfc_llc_ops *ops)
57 struct nfc_llc_engine *llc_engine;
59 llc_engine = kzalloc(sizeof(struct nfc_llc_engine), GFP_KERNEL);
60 if (llc_engine == NULL)
61 return -ENOMEM;
63 llc_engine->name = kstrdup(name, GFP_KERNEL);
64 if (llc_engine->name == NULL) {
65 kfree(llc_engine);
66 return -ENOMEM;
68 llc_engine->ops = ops;
70 INIT_LIST_HEAD(&llc_engine->entry);
71 list_add_tail(&llc_engine->entry, &llc_engines);
73 return 0;
76 static struct nfc_llc_engine *nfc_llc_name_to_engine(const char *name)
78 struct nfc_llc_engine *llc_engine;
80 list_for_each_entry(llc_engine, &llc_engines, entry) {
81 if (strcmp(llc_engine->name, name) == 0)
82 return llc_engine;
85 return NULL;
88 void nfc_llc_unregister(const char *name)
90 struct nfc_llc_engine *llc_engine;
92 llc_engine = nfc_llc_name_to_engine(name);
93 if (llc_engine == NULL)
94 return;
96 list_del(&llc_engine->entry);
97 kfree(llc_engine->name);
98 kfree(llc_engine);
101 struct 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)
106 struct nfc_llc_engine *llc_engine;
107 struct nfc_llc *llc;
109 llc_engine = nfc_llc_name_to_engine(name);
110 if (llc_engine == NULL)
111 return NULL;
113 llc = kzalloc(sizeof(struct nfc_llc), GFP_KERNEL);
114 if (llc == NULL)
115 return NULL;
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;
125 llc->ops = llc_engine->ops;
127 return llc;
130 void nfc_llc_free(struct nfc_llc *llc)
132 llc->ops->deinit(llc);
133 kfree(llc);
136 inline void nfc_llc_get_rx_head_tail_room(struct nfc_llc *llc, int *rx_headroom,
137 int *rx_tailroom)
139 *rx_headroom = llc->rx_headroom;
140 *rx_tailroom = llc->rx_tailroom;
143 inline int nfc_llc_start(struct nfc_llc *llc)
145 return llc->ops->start(llc);
148 inline int nfc_llc_stop(struct nfc_llc *llc)
150 return llc->ops->stop(llc);
153 inline void nfc_llc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
155 llc->ops->rcv_from_drv(llc, skb);
158 inline int nfc_llc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
160 return llc->ops->xmit_from_hci(llc, skb);
163 inline void *nfc_llc_get_data(struct nfc_llc *llc)
165 return llc->data;