llc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <net/nfc/llc.h>
  19. #include "llc.h"
  20. static LIST_HEAD(llc_engines);
  21. int nfc_llc_init(void)
  22. {
  23. int r;
  24. r = nfc_llc_nop_register();
  25. if (r)
  26. goto exit;
  27. r = nfc_llc_shdlc_register();
  28. if (r)
  29. goto exit;
  30. return 0;
  31. exit:
  32. nfc_llc_exit();
  33. return r;
  34. }
  35. void nfc_llc_exit(void)
  36. {
  37. struct nfc_llc_engine *llc_engine, *n;
  38. list_for_each_entry_safe(llc_engine, n, &llc_engines, entry) {
  39. list_del(&llc_engine->entry);
  40. kfree(llc_engine->name);
  41. kfree(llc_engine);
  42. }
  43. }
  44. int nfc_llc_register(const char *name, struct nfc_llc_ops *ops)
  45. {
  46. struct nfc_llc_engine *llc_engine;
  47. llc_engine = kzalloc(sizeof(struct nfc_llc_engine), GFP_KERNEL);
  48. if (llc_engine == NULL)
  49. return -ENOMEM;
  50. llc_engine->name = kstrdup(name, GFP_KERNEL);
  51. if (llc_engine->name == NULL) {
  52. kfree(llc_engine);
  53. return -ENOMEM;
  54. }
  55. llc_engine->ops = ops;
  56. INIT_LIST_HEAD(&llc_engine->entry);
  57. list_add_tail(&llc_engine->entry, &llc_engines);
  58. return 0;
  59. }
  60. static struct nfc_llc_engine *nfc_llc_name_to_engine(const char *name)
  61. {
  62. struct nfc_llc_engine *llc_engine;
  63. list_for_each_entry(llc_engine, &llc_engines, entry) {
  64. if (strcmp(llc_engine->name, name) == 0)
  65. return llc_engine;
  66. }
  67. return NULL;
  68. }
  69. void nfc_llc_unregister(const char *name)
  70. {
  71. struct nfc_llc_engine *llc_engine;
  72. llc_engine = nfc_llc_name_to_engine(name);
  73. if (llc_engine == NULL)
  74. return;
  75. list_del(&llc_engine->entry);
  76. kfree(llc_engine->name);
  77. kfree(llc_engine);
  78. }
  79. struct nfc_llc *nfc_llc_allocate(const char *name, struct nfc_hci_dev *hdev,
  80. xmit_to_drv_t xmit_to_drv,
  81. rcv_to_hci_t rcv_to_hci, int tx_headroom,
  82. int tx_tailroom, llc_failure_t llc_failure)
  83. {
  84. struct nfc_llc_engine *llc_engine;
  85. struct nfc_llc *llc;
  86. llc_engine = nfc_llc_name_to_engine(name);
  87. if (llc_engine == NULL)
  88. return NULL;
  89. llc = kzalloc(sizeof(struct nfc_llc), GFP_KERNEL);
  90. if (llc == NULL)
  91. return NULL;
  92. llc->data = llc_engine->ops->init(hdev, xmit_to_drv, rcv_to_hci,
  93. tx_headroom, tx_tailroom,
  94. &llc->rx_headroom, &llc->rx_tailroom,
  95. llc_failure);
  96. if (llc->data == NULL) {
  97. kfree(llc);
  98. return NULL;
  99. }
  100. llc->ops = llc_engine->ops;
  101. return llc;
  102. }
  103. void nfc_llc_free(struct nfc_llc *llc)
  104. {
  105. llc->ops->deinit(llc);
  106. kfree(llc);
  107. }
  108. inline void nfc_llc_get_rx_head_tail_room(struct nfc_llc *llc, int *rx_headroom,
  109. int *rx_tailroom)
  110. {
  111. *rx_headroom = llc->rx_headroom;
  112. *rx_tailroom = llc->rx_tailroom;
  113. }
  114. inline int nfc_llc_start(struct nfc_llc *llc)
  115. {
  116. return llc->ops->start(llc);
  117. }
  118. EXPORT_SYMBOL(nfc_llc_start);
  119. inline int nfc_llc_stop(struct nfc_llc *llc)
  120. {
  121. return llc->ops->stop(llc);
  122. }
  123. EXPORT_SYMBOL(nfc_llc_stop);
  124. inline void nfc_llc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
  125. {
  126. llc->ops->rcv_from_drv(llc, skb);
  127. }
  128. inline int nfc_llc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
  129. {
  130. return llc->ops->xmit_from_hci(llc, skb);
  131. }
  132. inline void *nfc_llc_get_data(struct nfc_llc *llc)
  133. {
  134. return llc->data;
  135. }