cn_queue.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 *cn_queue_alloc_callback_entry(char *name, struct cb_id *id, void (*callback)(void *))
  77. {
  78. struct cn_callback_entry *cbq;
  79. cbq = kzalloc(sizeof(*cbq), GFP_KERNEL);
  80. if (!cbq) {
  81. printk(KERN_ERR "Failed to create new callback queue.\n");
  82. return NULL;
  83. }
  84. snprintf(cbq->id.name, sizeof(cbq->id.name), "%s", name);
  85. memcpy(&cbq->id.id, id, sizeof(struct cb_id));
  86. cbq->data.callback = callback;
  87. INIT_WORK(&cbq->work, &cn_queue_wrapper);
  88. return cbq;
  89. }
  90. static void cn_queue_free_callback(struct cn_callback_entry *cbq)
  91. {
  92. /* The first jobs have been sent to kevent, flush them too */
  93. flush_scheduled_work();
  94. if (cbq->pdev->cn_queue)
  95. flush_workqueue(cbq->pdev->cn_queue);
  96. kfree(cbq);
  97. }
  98. int cn_cb_equal(struct cb_id *i1, struct cb_id *i2)
  99. {
  100. return ((i1->idx == i2->idx) && (i1->val == i2->val));
  101. }
  102. int cn_queue_add_callback(struct cn_queue_dev *dev, char *name, struct cb_id *id, void (*callback)(void *))
  103. {
  104. struct cn_callback_entry *cbq, *__cbq;
  105. int found = 0;
  106. cbq = cn_queue_alloc_callback_entry(name, id, callback);
  107. if (!cbq)
  108. return -ENOMEM;
  109. atomic_inc(&dev->refcnt);
  110. cbq->pdev = dev;
  111. spin_lock_bh(&dev->queue_lock);
  112. list_for_each_entry(__cbq, &dev->queue_list, callback_entry) {
  113. if (cn_cb_equal(&__cbq->id.id, id)) {
  114. found = 1;
  115. break;
  116. }
  117. }
  118. if (!found)
  119. list_add_tail(&cbq->callback_entry, &dev->queue_list);
  120. spin_unlock_bh(&dev->queue_lock);
  121. if (found) {
  122. cn_queue_free_callback(cbq);
  123. atomic_dec(&dev->refcnt);
  124. return -EINVAL;
  125. }
  126. cbq->seq = 0;
  127. cbq->group = cbq->id.id.idx;
  128. return 0;
  129. }
  130. void cn_queue_del_callback(struct cn_queue_dev *dev, struct cb_id *id)
  131. {
  132. struct cn_callback_entry *cbq, *n;
  133. int found = 0;
  134. spin_lock_bh(&dev->queue_lock);
  135. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry) {
  136. if (cn_cb_equal(&cbq->id.id, id)) {
  137. list_del(&cbq->callback_entry);
  138. found = 1;
  139. break;
  140. }
  141. }
  142. spin_unlock_bh(&dev->queue_lock);
  143. if (found) {
  144. cn_queue_free_callback(cbq);
  145. atomic_dec(&dev->refcnt);
  146. }
  147. }
  148. struct cn_queue_dev *cn_queue_alloc_dev(char *name, struct sock *nls)
  149. {
  150. struct cn_queue_dev *dev;
  151. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  152. if (!dev)
  153. return NULL;
  154. snprintf(dev->name, sizeof(dev->name), "%s", name);
  155. atomic_set(&dev->refcnt, 0);
  156. INIT_LIST_HEAD(&dev->queue_list);
  157. spin_lock_init(&dev->queue_lock);
  158. init_waitqueue_head(&dev->wq_created);
  159. dev->nls = nls;
  160. INIT_WORK(&dev->wq_creation, cn_queue_create);
  161. return dev;
  162. }
  163. void cn_queue_free_dev(struct cn_queue_dev *dev)
  164. {
  165. struct cn_callback_entry *cbq, *n;
  166. long timeout;
  167. DEFINE_WAIT(wait);
  168. /* Flush the first pending jobs queued on kevent */
  169. flush_scheduled_work();
  170. /* If the connector workqueue creation is still pending, wait for it */
  171. prepare_to_wait(&dev->wq_created, &wait, TASK_UNINTERRUPTIBLE);
  172. if (atomic_read(&dev->wq_requested) && !dev->cn_queue) {
  173. timeout = schedule_timeout(HZ * 2);
  174. if (!timeout && !dev->cn_queue)
  175. WARN_ON(1);
  176. }
  177. finish_wait(&dev->wq_created, &wait);
  178. if (dev->cn_queue) {
  179. flush_workqueue(dev->cn_queue);
  180. destroy_workqueue(dev->cn_queue);
  181. }
  182. spin_lock_bh(&dev->queue_lock);
  183. list_for_each_entry_safe(cbq, n, &dev->queue_list, callback_entry)
  184. list_del(&cbq->callback_entry);
  185. spin_unlock_bh(&dev->queue_lock);
  186. while (atomic_read(&dev->refcnt)) {
  187. printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
  188. dev->name, atomic_read(&dev->refcnt));
  189. msleep(1000);
  190. }
  191. kfree(dev);
  192. dev = NULL;
  193. }