cn_queue.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /*
  33. * This job is sent to the kevent workqueue.
  34. * While no event is once sent to any callback, the connector workqueue
  35. * is not created to avoid a useless waiting kernel task.
  36. * Once the first event is received, we create this dedicated workqueue which
  37. * is necessary because the flow of data can be high and we don't want
  38. * to encumber keventd with that.
  39. */
  40. static void cn_queue_create(struct work_struct *work)
  41. {
  42. struct cn_queue_dev *dev;
  43. dev = container_of(work, struct cn_queue_dev, wq_creation);
  44. dev->cn_queue = create_singlethread_workqueue(dev->name);
  45. /* If we fail, we will use keventd for all following connector jobs */
  46. WARN_ON(!dev->cn_queue);
  47. }
  48. /*
  49. * Queue a data sent to a callback.
  50. * If the connector workqueue is already created, we queue the job on it.
  51. * Otherwise, we queue the job to kevent and queue the connector workqueue
  52. * creation too.
  53. */
  54. int queue_cn_work(struct cn_callback_entry *cbq, struct work_struct *work)
  55. {
  56. struct cn_queue_dev *pdev = cbq->pdev;
  57. if (likely(pdev->cn_queue))
  58. return queue_work(pdev->cn_queue, work);
  59. /* Don't create the connector workqueue twice */
  60. if (atomic_inc_return(&pdev->wq_requested) == 1)
  61. schedule_work(&pdev->wq_creation);
  62. else
  63. atomic_dec(&pdev->wq_requested);
  64. return schedule_work(work);
  65. }
  66. void cn_queue_wrapper(struct work_struct *work)
  67. {
  68. struct cn_callback_entry *cbq =
  69. container_of(work, struct cn_callback_entry, work);
  70. struct cn_callback_data *d = &cbq->data;
  71. d->callback(d->callback_priv);
  72. d->destruct_data(d->ddata);
  73. d->ddata = NULL;
  74. kfree(d->free);
  75. }
  76. static struct cn_callback_entry *
  77. cn_queue_alloc_callback_entry(char *name, struct cb_id *id,
  78. void (*callback)(struct cn_msg *))
  79. {
  80. struct cn_callback_entry *cbq;
  81. cbq = kzalloc(sizeof(*cbq), GFP_KERNEL);
  82. if (!cbq) {
  83. printk(KERN_ERR "Failed to create new callback queue.\n");
  84. return NULL;
  85. }
  86. snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
  87. memcpy(&cbq->id.id, id, sizeof(struct cb_id));
  88. cbq->data.callback = callback;
  89. INIT_WORK(&cbq->work, &cn_queue_wrapper);
  90. return cbq;
  91. }
  92. static void cn_queue_free_callback(struct cn_callback_entry *cbq)
  93. {
  94. /* The first jobs have been sent to kevent, flush them too */
  95. flush_scheduled_work();
  96. if (cbq->pdev->cn_queue)
  97. flush_workqueue(cbq->pdev->cn_queue);
  98. kfree(cbq);
  99. }
  100. int cn_cb_equal(struct cb_id *i1, struct cb_id *i2)
  101. {
  102. return ((i1->idx == i2->idx) && (i1->val == i2->val));
  103. }
  104. int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id,
  105. void (*callback)(struct cn_msg *))
  106. {
  107. struct cn_callback_entry *cbq, *__cbq;
  108. int found = 0;
  109. cbq = cn_queue_alloc_callback_entry(name, id, callback);
  110. if (!cbq)
  111. return -ENOMEM;
  112. atomic_inc(&dev->refcnt);
  113. cbq->pdev = dev;
  114. spin_lock_bh(&dev->queue_lock);
  115. list_for_each_entry(__cbq, &dev->queue_list, callback_entry) {
  116. if (cn_cb_equal(&__cbq->id.id, id)) {
  117. found = 1;
  118. break;
  119. }
  120. }
  121. if (!found)
  122. list_add_tail(&cbq->callback_entry, &dev->queue_list);
  123. spin_unlock_bh(&dev->queue_lock);
  124. if (found) {
  125. cn_queue_free_callback(cbq);
  126. atomic_dec(&dev->refcnt);
  127. return -EINVAL;
  128. }
  129. cbq->seq = 0;
  130. cbq->group = cbq->id.id.idx;
  131. return 0;
  132. }
  133. void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id)
  134. {
  135. struct cn_callback_entry *cbq, *n;
  136. int found = 0;
  137. spin_lock_bh(&dev->queue_lock);
  138. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) {
  139. if (cn_cb_equal(&cbq->id.id, id)) {
  140. list_del(&cbq->callback_entry);
  141. found = 1;
  142. break;
  143. }
  144. }
  145. spin_unlock_bh(&dev->queue_lock);
  146. if (found) {
  147. cn_queue_free_callback(cbq);
  148. atomic_dec(&dev->refcnt);
  149. }
  150. }
  151. struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls)
  152. {
  153. struct cn_queue_dev *dev;
  154. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  155. if (!dev)
  156. return NULL;
  157. snprintf(dev->name, sizeof(dev->name), "%s", name);
  158. atomic_set(&dev->refcnt, 0);
  159. INIT_LIST_HEAD(&dev->queue_list);
  160. spin_lock_init(&dev->queue_lock);
  161. init_waitqueue_head(&dev->wq_created);
  162. dev->nls = nls;
  163. INIT_WORK(&dev->wq_creation, cn_queue_create);
  164. return dev;
  165. }
  166. void cn_queue_free_dev(struct cn_queue_dev *dev)
  167. {
  168. struct cn_callback_entry *cbq, *n;
  169. long timeout;
  170. DEFINE_WAIT(wait);
  171. /* Flush the first pending jobs queued on kevent */
  172. flush_scheduled_work();
  173. /* If the connector workqueue creation is still pending, wait for it */
  174. prepare_to_wait(&dev->wq_created, &wait, TASK_UNINTERRUPTIBLE);
  175. if (atomic_read(&dev->wq_requested) && !dev->cn_queue) {
  176. timeout = schedule_timeout(HZ * 2);
  177. if (!timeout && !dev->cn_queue)
  178. WARN_ON(1);
  179. }
  180. finish_wait(&dev->wq_created, &wait);
  181. if (dev->cn_queue) {
  182. flush_workqueue(dev->cn_queue);
  183. destroy_workqueue(dev->cn_queue);
  184. }
  185. spin_lock_bh(&dev->queue_lock);
  186. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)
  187. list_del(&cbq->callback_entry);
  188. spin_unlock_bh(&dev->queue_lock);
  189. while (atomic_read(&dev->refcnt)) {
  190. printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
  191. dev->name, atomic_read(&dev->refcnt));
  192. msleep(1000);
  193. }
  194. kfree(dev);
  195. dev = NULL;
  196. }