Thomas Gleixner | 46fe777 | 2019-05-31 01:09:57 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Link Layer Control manager |
| 4 | * |
| 5 | * Copyright (C) 2012 Intel Corporation. All rights reserved. |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 8 | #include <net/nfc/llc.h> |
Samuel Ortiz | f4f20d0 | 2012-09-18 19:17:33 +0200 | [diff] [blame] | 9 | |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 10 | #include "llc.h" |
| 11 | |
Axel Lin | 0b51fc5 | 2014-02-22 22:14:18 +0800 | [diff] [blame] | 12 | static LIST_HEAD(llc_engines); |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 13 | |
| 14 | int nfc_llc_init(void) |
| 15 | { |
Eric Lapuyade | 4a61cd6 | 2012-09-13 17:11:37 +0200 | [diff] [blame] | 16 | int r; |
| 17 | |
Eric Lapuyade | 4a61cd6 | 2012-09-13 17:11:37 +0200 | [diff] [blame] | 18 | r = nfc_llc_nop_register(); |
| 19 | if (r) |
| 20 | goto exit; |
| 21 | |
| 22 | r = nfc_llc_shdlc_register(); |
| 23 | if (r) |
| 24 | goto exit; |
| 25 | |
| 26 | return 0; |
| 27 | |
| 28 | exit: |
| 29 | nfc_llc_exit(); |
| 30 | return r; |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 31 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 32 | |
| 33 | void nfc_llc_exit(void) |
| 34 | { |
| 35 | struct nfc_llc_engine *llc_engine, *n; |
| 36 | |
| 37 | list_for_each_entry_safe(llc_engine, n, &llc_engines, entry) { |
| 38 | list_del(&llc_engine->entry); |
| 39 | kfree(llc_engine->name); |
| 40 | kfree(llc_engine); |
| 41 | } |
| 42 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 43 | |
| 44 | int nfc_llc_register(const char *name, struct nfc_llc_ops *ops) |
| 45 | { |
| 46 | struct nfc_llc_engine *llc_engine; |
| 47 | |
| 48 | llc_engine = kzalloc(sizeof(struct nfc_llc_engine), GFP_KERNEL); |
| 49 | if (llc_engine == NULL) |
| 50 | return -ENOMEM; |
| 51 | |
| 52 | llc_engine->name = kstrdup(name, GFP_KERNEL); |
| 53 | if (llc_engine->name == NULL) { |
| 54 | kfree(llc_engine); |
| 55 | return -ENOMEM; |
| 56 | } |
| 57 | llc_engine->ops = ops; |
| 58 | |
| 59 | INIT_LIST_HEAD(&llc_engine->entry); |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 60 | list_add_tail(&llc_engine->entry, &llc_engines); |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 64 | |
| 65 | static struct nfc_llc_engine *nfc_llc_name_to_engine(const char *name) |
| 66 | { |
| 67 | struct nfc_llc_engine *llc_engine; |
| 68 | |
| 69 | list_for_each_entry(llc_engine, &llc_engines, entry) { |
| 70 | if (strcmp(llc_engine->name, name) == 0) |
| 71 | return llc_engine; |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | void nfc_llc_unregister(const char *name) |
| 78 | { |
| 79 | struct nfc_llc_engine *llc_engine; |
| 80 | |
| 81 | llc_engine = nfc_llc_name_to_engine(name); |
| 82 | if (llc_engine == NULL) |
| 83 | return; |
| 84 | |
| 85 | list_del(&llc_engine->entry); |
| 86 | kfree(llc_engine->name); |
| 87 | kfree(llc_engine); |
| 88 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 89 | |
| 90 | struct nfc_llc *nfc_llc_allocate(const char *name, struct nfc_hci_dev *hdev, |
| 91 | xmit_to_drv_t xmit_to_drv, |
| 92 | rcv_to_hci_t rcv_to_hci, int tx_headroom, |
| 93 | int tx_tailroom, llc_failure_t llc_failure) |
| 94 | { |
| 95 | struct nfc_llc_engine *llc_engine; |
| 96 | struct nfc_llc *llc; |
| 97 | |
| 98 | llc_engine = nfc_llc_name_to_engine(name); |
| 99 | if (llc_engine == NULL) |
| 100 | return NULL; |
| 101 | |
| 102 | llc = kzalloc(sizeof(struct nfc_llc), GFP_KERNEL); |
| 103 | if (llc == NULL) |
| 104 | return NULL; |
| 105 | |
| 106 | llc->data = llc_engine->ops->init(hdev, xmit_to_drv, rcv_to_hci, |
| 107 | tx_headroom, tx_tailroom, |
| 108 | &llc->rx_headroom, &llc->rx_tailroom, |
| 109 | llc_failure); |
| 110 | if (llc->data == NULL) { |
| 111 | kfree(llc); |
| 112 | return NULL; |
| 113 | } |
| 114 | llc->ops = llc_engine->ops; |
| 115 | |
| 116 | return llc; |
| 117 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 118 | |
| 119 | void nfc_llc_free(struct nfc_llc *llc) |
| 120 | { |
| 121 | llc->ops->deinit(llc); |
| 122 | kfree(llc); |
| 123 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 124 | |
Denys Vlasenko | f86dec9 | 2016-04-15 18:14:25 +0200 | [diff] [blame] | 125 | int nfc_llc_start(struct nfc_llc *llc) |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 126 | { |
| 127 | return llc->ops->start(llc); |
| 128 | } |
Christophe Ricard | 15d1717 | 2015-10-26 07:50:11 +0100 | [diff] [blame] | 129 | EXPORT_SYMBOL(nfc_llc_start); |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 130 | |
Denys Vlasenko | f86dec9 | 2016-04-15 18:14:25 +0200 | [diff] [blame] | 131 | int nfc_llc_stop(struct nfc_llc *llc) |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 132 | { |
| 133 | return llc->ops->stop(llc); |
| 134 | } |
Christophe Ricard | 15d1717 | 2015-10-26 07:50:11 +0100 | [diff] [blame] | 135 | EXPORT_SYMBOL(nfc_llc_stop); |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 136 | |
Denys Vlasenko | f86dec9 | 2016-04-15 18:14:25 +0200 | [diff] [blame] | 137 | void nfc_llc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb) |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 138 | { |
| 139 | llc->ops->rcv_from_drv(llc, skb); |
| 140 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 141 | |
Denys Vlasenko | f86dec9 | 2016-04-15 18:14:25 +0200 | [diff] [blame] | 142 | int nfc_llc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb) |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 143 | { |
| 144 | return llc->ops->xmit_from_hci(llc, skb); |
| 145 | } |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 146 | |
Denys Vlasenko | f86dec9 | 2016-04-15 18:14:25 +0200 | [diff] [blame] | 147 | void *nfc_llc_get_data(struct nfc_llc *llc) |
Eric Lapuyade | 67cccfe | 2012-09-13 17:10:00 +0200 | [diff] [blame] | 148 | { |
| 149 | return llc->data; |
| 150 | } |