llc.c 3.6 KB

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