cn_queue.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * cn_queue.c
  3. *
  4. * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/list.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/slab.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/suspend.h>
  30. #include <linux/connector.h>
  31. #include <linux/delay.h>
  32. static struct cn_callback_entry *
  33. cn_queue_alloc_callback_entry(struct cn_queue_dev *dev, const char *name,
  34. struct cb_id *id,
  35. void (*callback)(struct cn_msg *,
  36. struct netlink_skb_parms *))
  37. {
  38. struct cn_callback_entry *cbq;
  39. cbq = kzalloc(sizeof(*cbq), GFP_KERNEL);
  40. if (!cbq) {
  41. pr_err("Failed to create new callback queue.\n");
  42. return NULL;
  43. }
  44. atomic_set(&cbq->refcnt, 1);
  45. atomic_inc(&dev->refcnt);
  46. cbq->pdev = dev;
  47. snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
  48. memcpy(&cbq->id.id, id, sizeof(struct cb_id));
  49. cbq->callback = callback;
  50. return cbq;
  51. }
  52. void cn_queue_release_callback(struct cn_callback_entry *cbq)
  53. {
  54. if (!atomic_dec_and_test(&cbq->refcnt))
  55. return;
  56. atomic_dec(&cbq->pdev->refcnt);
  57. kfree(cbq);
  58. }
  59. int cn_cb_equal(struct cb_id *i1, struct cb_id *i2)
  60. {
  61. return ((i1->idx == i2->idx) && (i1->val == i2->val));
  62. }
  63. int cn_queue_add_callback(struct cn_queue_dev *dev, const char *name,
  64. struct cb_id *id,
  65. void (*callback)(struct cn_msg *,
  66. struct netlink_skb_parms *))
  67. {
  68. struct cn_callback_entry *cbq, *__cbq;
  69. int found = 0;
  70. cbq = cn_queue_alloc_callback_entry(dev, name, id, callback);
  71. if (!cbq)
  72. return -ENOMEM;
  73. spin_lock_bh(&dev->queue_lock);
  74. list_for_each_entry(__cbq, &dev->queue_list, callback_entry) {
  75. if (cn_cb_equal(&__cbq->id.id, id)) {
  76. found = 1;
  77. break;
  78. }
  79. }
  80. if (!found)
  81. list_add_tail(&cbq->callback_entry, &dev->queue_list);
  82. spin_unlock_bh(&dev->queue_lock);
  83. if (found) {
  84. cn_queue_release_callback(cbq);
  85. return -EINVAL;
  86. }
  87. cbq->seq = 0;
  88. cbq->group = cbq->id.id.idx;
  89. return 0;
  90. }
  91. void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id)
  92. {
  93. struct cn_callback_entry *cbq, *n;
  94. int found = 0;
  95. spin_lock_bh(&dev->queue_lock);
  96. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) {
  97. if (cn_cb_equal(&cbq->id.id, id)) {
  98. list_del(&cbq->callback_entry);
  99. found = 1;
  100. break;
  101. }
  102. }
  103. spin_unlock_bh(&dev->queue_lock);
  104. if (found)
  105. cn_queue_release_callback(cbq);
  106. }
  107. struct cn_queue_dev *cn_queue_alloc_dev(const char *name, struct sock *nls)
  108. {
  109. struct cn_queue_dev *dev;
  110. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  111. if (!dev)
  112. return NULL;
  113. snprintf(dev->name, sizeof(dev->name), "%s", name);
  114. atomic_set(&dev->refcnt, 0);
  115. INIT_LIST_HEAD(&dev->queue_list);
  116. spin_lock_init(&dev->queue_lock);
  117. dev->nls = nls;
  118. return dev;
  119. }
  120. void cn_queue_free_dev(struct cn_queue_dev *dev)
  121. {
  122. struct cn_callback_entry *cbq, *n;
  123. spin_lock_bh(&dev->queue_lock);
  124. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)
  125. list_del(&cbq->callback_entry);
  126. spin_unlock_bh(&dev->queue_lock);
  127. while (atomic_read(&dev->refcnt)) {
  128. pr_info("Waiting for %s to become free: refcnt=%d.\n",
  129. dev->name, atomic_read(&dev->refcnt));
  130. msleep(1000);
  131. }
  132. kfree(dev);
  133. dev = NULL;
  134. }