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 | { |
| 29 | INIT_LIST_HEAD(&llc_engines); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | EXPORT_SYMBOL(nfc_llc_init); |
| 34 | |
| 35 | void nfc_llc_exit(void) |
| 36 | { |
| 37 | struct nfc_llc_engine *llc_engine, *n; |
| 38 | |
| 39 | list_for_each_entry_safe(llc_engine, n, &llc_engines, entry) { |
| 40 | list_del(&llc_engine->entry); |
| 41 | kfree(llc_engine->name); |
| 42 | kfree(llc_engine); |
| 43 | } |
| 44 | } |
| 45 | EXPORT_SYMBOL(nfc_llc_exit); |
| 46 | |
| 47 | int nfc_llc_register(const char *name, struct nfc_llc_ops *ops) |
| 48 | { |
| 49 | struct nfc_llc_engine *llc_engine; |
| 50 | |
| 51 | llc_engine = kzalloc(sizeof(struct nfc_llc_engine), GFP_KERNEL); |
| 52 | if (llc_engine == NULL) |
| 53 | return -ENOMEM; |
| 54 | |
| 55 | llc_engine->name = kstrdup(name, GFP_KERNEL); |
| 56 | if (llc_engine->name == NULL) { |
| 57 | kfree(llc_engine); |
| 58 | return -ENOMEM; |
| 59 | } |
| 60 | llc_engine->ops = ops; |
| 61 | |
| 62 | INIT_LIST_HEAD(&llc_engine->entry); |
| 63 | list_add_tail (&llc_engine->entry, &llc_engines); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | EXPORT_SYMBOL(nfc_llc_register); |
| 68 | |
| 69 | static struct nfc_llc_engine *nfc_llc_name_to_engine(const char *name) |
| 70 | { |
| 71 | struct nfc_llc_engine *llc_engine; |
| 72 | |
| 73 | list_for_each_entry(llc_engine, &llc_engines, entry) { |
| 74 | if (strcmp(llc_engine->name, name) == 0) |
| 75 | return llc_engine; |
| 76 | } |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | void nfc_llc_unregister(const char *name) |
| 82 | { |
| 83 | struct nfc_llc_engine *llc_engine; |
| 84 | |
| 85 | llc_engine = nfc_llc_name_to_engine(name); |
| 86 | if (llc_engine == NULL) |
| 87 | return; |
| 88 | |
| 89 | list_del(&llc_engine->entry); |
| 90 | kfree(llc_engine->name); |
| 91 | kfree(llc_engine); |
| 92 | } |
| 93 | EXPORT_SYMBOL(nfc_llc_unregister); |
| 94 | |
| 95 | struct nfc_llc *nfc_llc_allocate(const char *name, struct nfc_hci_dev *hdev, |
| 96 | xmit_to_drv_t xmit_to_drv, |
| 97 | rcv_to_hci_t rcv_to_hci, int tx_headroom, |
| 98 | int tx_tailroom, llc_failure_t llc_failure) |
| 99 | { |
| 100 | struct nfc_llc_engine *llc_engine; |
| 101 | struct nfc_llc *llc; |
| 102 | |
| 103 | llc_engine = nfc_llc_name_to_engine(name); |
| 104 | if (llc_engine == NULL) |
| 105 | return NULL; |
| 106 | |
| 107 | llc = kzalloc(sizeof(struct nfc_llc), GFP_KERNEL); |
| 108 | if (llc == NULL) |
| 109 | return NULL; |
| 110 | |
| 111 | llc->data = llc_engine->ops->init(hdev, xmit_to_drv, rcv_to_hci, |
| 112 | tx_headroom, tx_tailroom, |
| 113 | &llc->rx_headroom, &llc->rx_tailroom, |
| 114 | llc_failure); |
| 115 | if (llc->data == NULL) { |
| 116 | kfree(llc); |
| 117 | return NULL; |
| 118 | } |
| 119 | llc->ops = llc_engine->ops; |
| 120 | |
| 121 | return llc; |
| 122 | } |
| 123 | EXPORT_SYMBOL(nfc_llc_allocate); |
| 124 | |
| 125 | void nfc_llc_free(struct nfc_llc *llc) |
| 126 | { |
| 127 | llc->ops->deinit(llc); |
| 128 | kfree(llc); |
| 129 | } |
| 130 | EXPORT_SYMBOL(nfc_llc_free); |
| 131 | |
| 132 | inline void nfc_llc_get_rx_head_tail_room(struct nfc_llc *llc, int *rx_headroom, |
| 133 | int *rx_tailroom) |
| 134 | { |
| 135 | *rx_headroom = llc->rx_headroom; |
| 136 | *rx_tailroom = llc->rx_tailroom; |
| 137 | } |
| 138 | EXPORT_SYMBOL(nfc_llc_get_rx_head_tail_room); |
| 139 | |
| 140 | inline int nfc_llc_start(struct nfc_llc *llc) |
| 141 | { |
| 142 | return llc->ops->start(llc); |
| 143 | } |
| 144 | EXPORT_SYMBOL(nfc_llc_start); |
| 145 | |
| 146 | inline int nfc_llc_stop(struct nfc_llc *llc) |
| 147 | { |
| 148 | return llc->ops->stop(llc); |
| 149 | } |
| 150 | EXPORT_SYMBOL(nfc_llc_stop); |
| 151 | |
| 152 | inline void nfc_llc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb) |
| 153 | { |
| 154 | llc->ops->rcv_from_drv(llc, skb); |
| 155 | } |
| 156 | EXPORT_SYMBOL(nfc_llc_rcv_from_drv); |
| 157 | |
| 158 | inline int nfc_llc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb) |
| 159 | { |
| 160 | return llc->ops->xmit_from_hci(llc, skb); |
| 161 | } |
| 162 | EXPORT_SYMBOL(nfc_llc_xmit_from_hci); |
| 163 | |
| 164 | inline void *nfc_llc_get_data(struct nfc_llc *llc) |
| 165 | { |
| 166 | return llc->data; |
| 167 | } |
| 168 | EXPORT_SYMBOL(nfc_llc_get_data); |