Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 1 | /* |
| 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 |
| 16 | * along with this program; if not, write to the |
| 17 | * Free Software Foundation, Inc., |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/export.h> |
| 22 | #include <net/nfc/llc.h> |
| 23 | #include "llc.h" |
| 24 | |
| 25 | static struct list_head llc_engines; |
| 26 | |
| 27 | int nfc_llc_init(void) |
| 28 | { |
Eric Lapuyade | 4a61cd6 | 2012-09-13 17:11:37 +0200 | [diff] [blame^] | 29 | int r; |
| 30 | |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 31 | INIT_LIST_HEAD(&llc_engines); |
| 32 | |
Eric Lapuyade | 4a61cd6 | 2012-09-13 17:11:37 +0200 | [diff] [blame^] | 33 | r = nfc_llc_nop_register(); |
| 34 | if (r) |
| 35 | goto exit; |
| 36 | |
| 37 | r = nfc_llc_shdlc_register(); |
| 38 | if (r) |
| 39 | goto exit; |
| 40 | |
| 41 | return 0; |
| 42 | |
| 43 | exit: |
| 44 | nfc_llc_exit(); |
| 45 | return r; |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 46 | } |
| 47 | EXPORT_SYMBOL(nfc_llc_init); |
| 48 | |
| 49 | void nfc_llc_exit(void) |
| 50 | { |
| 51 | struct nfc_llc_engine *llc_engine, *n; |
| 52 | |
| 53 | list_for_each_entry_safe(llc_engine, n, &llc_engines, entry) { |
| 54 | list_del(&llc_engine->entry); |
| 55 | kfree(llc_engine->name); |
| 56 | kfree(llc_engine); |
| 57 | } |
| 58 | } |
| 59 | EXPORT_SYMBOL(nfc_llc_exit); |
| 60 | |
| 61 | int nfc_llc_register(const char *name, struct nfc_llc_ops *ops) |
| 62 | { |
| 63 | struct nfc_llc_engine *llc_engine; |
| 64 | |
| 65 | llc_engine = kzalloc(sizeof(struct nfc_llc_engine), GFP_KERNEL); |
| 66 | if (llc_engine == NULL) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | llc_engine->name = kstrdup(name, GFP_KERNEL); |
| 70 | if (llc_engine->name == NULL) { |
| 71 | kfree(llc_engine); |
| 72 | return -ENOMEM; |
| 73 | } |
| 74 | llc_engine->ops = ops; |
| 75 | |
| 76 | INIT_LIST_HEAD(&llc_engine->entry); |
| 77 | list_add_tail (&llc_engine->entry, &llc_engines); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | EXPORT_SYMBOL(nfc_llc_register); |
| 82 | |
| 83 | static struct nfc_llc_engine *nfc_llc_name_to_engine(const char *name) |
| 84 | { |
| 85 | struct nfc_llc_engine *llc_engine; |
| 86 | |
| 87 | list_for_each_entry(llc_engine, &llc_engines, entry) { |
| 88 | if (strcmp(llc_engine->name, name) == 0) |
| 89 | return llc_engine; |
| 90 | } |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | void nfc_llc_unregister(const char *name) |
| 96 | { |
| 97 | struct nfc_llc_engine *llc_engine; |
| 98 | |
| 99 | llc_engine = nfc_llc_name_to_engine(name); |
| 100 | if (llc_engine == NULL) |
| 101 | return; |
| 102 | |
| 103 | list_del(&llc_engine->entry); |
| 104 | kfree(llc_engine->name); |
| 105 | kfree(llc_engine); |
| 106 | } |
| 107 | EXPORT_SYMBOL(nfc_llc_unregister); |
| 108 | |
| 109 | struct nfc_llc *nfc_llc_allocate(const char *name, struct nfc_hci_dev *hdev, |
| 110 | xmit_to_drv_t xmit_to_drv, |
| 111 | rcv_to_hci_t rcv_to_hci, int tx_headroom, |
| 112 | int tx_tailroom, llc_failure_t llc_failure) |
| 113 | { |
| 114 | struct nfc_llc_engine *llc_engine; |
| 115 | struct nfc_llc *llc; |
| 116 | |
| 117 | llc_engine = nfc_llc_name_to_engine(name); |
| 118 | if (llc_engine == NULL) |
| 119 | return NULL; |
| 120 | |
| 121 | llc = kzalloc(sizeof(struct nfc_llc), GFP_KERNEL); |
| 122 | if (llc == NULL) |
| 123 | return NULL; |
| 124 | |
| 125 | llc->data = llc_engine->ops->init(hdev, xmit_to_drv, rcv_to_hci, |
| 126 | tx_headroom, tx_tailroom, |
| 127 | &llc->rx_headroom, &llc->rx_tailroom, |
| 128 | llc_failure); |
| 129 | if (llc->data == NULL) { |
| 130 | kfree(llc); |
| 131 | return NULL; |
| 132 | } |
| 133 | llc->ops = llc_engine->ops; |
| 134 | |
| 135 | return llc; |
| 136 | } |
| 137 | EXPORT_SYMBOL(nfc_llc_allocate); |
| 138 | |
| 139 | void nfc_llc_free(struct nfc_llc *llc) |
| 140 | { |
| 141 | llc->ops->deinit(llc); |
| 142 | kfree(llc); |
| 143 | } |
| 144 | EXPORT_SYMBOL(nfc_llc_free); |
| 145 | |
| 146 | inline void nfc_llc_get_rx_head_tail_room(struct nfc_llc *llc, int *rx_headroom, |
| 147 | int *rx_tailroom) |
| 148 | { |
| 149 | *rx_headroom = llc->rx_headroom; |
| 150 | *rx_tailroom = llc->rx_tailroom; |
| 151 | } |
| 152 | EXPORT_SYMBOL(nfc_llc_get_rx_head_tail_room); |
| 153 | |
| 154 | inline int nfc_llc_start(struct nfc_llc *llc) |
| 155 | { |
| 156 | return llc->ops->start(llc); |
| 157 | } |
| 158 | EXPORT_SYMBOL(nfc_llc_start); |
| 159 | |
| 160 | inline int nfc_llc_stop(struct nfc_llc *llc) |
| 161 | { |
| 162 | return llc->ops->stop(llc); |
| 163 | } |
| 164 | EXPORT_SYMBOL(nfc_llc_stop); |
| 165 | |
| 166 | inline void nfc_llc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb) |
| 167 | { |
| 168 | llc->ops->rcv_from_drv(llc, skb); |
| 169 | } |
| 170 | EXPORT_SYMBOL(nfc_llc_rcv_from_drv); |
| 171 | |
| 172 | inline int nfc_llc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb) |
| 173 | { |
| 174 | return llc->ops->xmit_from_hci(llc, skb); |
| 175 | } |
| 176 | EXPORT_SYMBOL(nfc_llc_xmit_from_hci); |
| 177 | |
| 178 | inline void *nfc_llc_get_data(struct nfc_llc *llc) |
| 179 | { |
| 180 | return llc->data; |
| 181 | } |
| 182 | EXPORT_SYMBOL(nfc_llc_get_data); |