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