seq_fifo.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA sequencer FIFO
  4. * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
  5. */
  6. #include <sound/core.h>
  7. #include <linux/slab.h>
  8. #include <linux/sched/signal.h>
  9. #include "seq_fifo.h"
  10. #include "seq_lock.h"
  11. /* FIFO */
  12. /* create new fifo */
  13. struct snd_seq_fifo *snd_seq_fifo_new(int poolsize)
  14. {
  15. struct snd_seq_fifo *f;
  16. f = kzalloc(sizeof(*f), GFP_KERNEL);
  17. if (!f)
  18. return NULL;
  19. f->pool = snd_seq_pool_new(poolsize);
  20. if (f->pool == NULL) {
  21. kfree(f);
  22. return NULL;
  23. }
  24. if (snd_seq_pool_init(f->pool) < 0) {
  25. snd_seq_pool_delete(&f->pool);
  26. kfree(f);
  27. return NULL;
  28. }
  29. spin_lock_init(&f->lock);
  30. snd_use_lock_init(&f->use_lock);
  31. init_waitqueue_head(&f->input_sleep);
  32. atomic_set(&f->overflow, 0);
  33. f->head = NULL;
  34. f->tail = NULL;
  35. f->cells = 0;
  36. return f;
  37. }
  38. void snd_seq_fifo_delete(struct snd_seq_fifo **fifo)
  39. {
  40. struct snd_seq_fifo *f;
  41. if (snd_BUG_ON(!fifo))
  42. return;
  43. f = *fifo;
  44. if (snd_BUG_ON(!f))
  45. return;
  46. *fifo = NULL;
  47. if (f->pool)
  48. snd_seq_pool_mark_closing(f->pool);
  49. snd_seq_fifo_clear(f);
  50. /* wake up clients if any */
  51. if (waitqueue_active(&f->input_sleep))
  52. wake_up(&f->input_sleep);
  53. /* release resources...*/
  54. /*....................*/
  55. if (f->pool) {
  56. snd_seq_pool_done(f->pool);
  57. snd_seq_pool_delete(&f->pool);
  58. }
  59. kfree(f);
  60. }
  61. static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f);
  62. /* clear queue */
  63. void snd_seq_fifo_clear(struct snd_seq_fifo *f)
  64. {
  65. struct snd_seq_event_cell *cell;
  66. /* clear overflow flag */
  67. atomic_set(&f->overflow, 0);
  68. snd_use_lock_sync(&f->use_lock);
  69. guard(spinlock_irq)(&f->lock);
  70. /* drain the fifo */
  71. while ((cell = fifo_cell_out(f)) != NULL) {
  72. snd_seq_cell_free(cell);
  73. }
  74. }
  75. /* enqueue event to fifo */
  76. int snd_seq_fifo_event_in(struct snd_seq_fifo *f,
  77. struct snd_seq_event *event)
  78. {
  79. struct snd_seq_event_cell *cell;
  80. int err;
  81. if (snd_BUG_ON(!f))
  82. return -EINVAL;
  83. snd_use_lock_use(&f->use_lock);
  84. err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL, NULL); /* always non-blocking */
  85. if (err < 0) {
  86. if ((err == -ENOMEM) || (err == -EAGAIN))
  87. atomic_inc(&f->overflow);
  88. snd_use_lock_free(&f->use_lock);
  89. return err;
  90. }
  91. /* append new cells to fifo */
  92. scoped_guard(spinlock_irqsave, &f->lock) {
  93. if (f->tail != NULL)
  94. f->tail->next = cell;
  95. f->tail = cell;
  96. if (f->head == NULL)
  97. f->head = cell;
  98. cell->next = NULL;
  99. f->cells++;
  100. }
  101. /* wakeup client */
  102. if (waitqueue_active(&f->input_sleep))
  103. wake_up(&f->input_sleep);
  104. snd_use_lock_free(&f->use_lock);
  105. return 0; /* success */
  106. }
  107. /* dequeue cell from fifo */
  108. static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
  109. {
  110. struct snd_seq_event_cell *cell;
  111. cell = f->head;
  112. if (cell) {
  113. f->head = cell->next;
  114. /* reset tail if this was the last element */
  115. if (f->tail == cell)
  116. f->tail = NULL;
  117. cell->next = NULL;
  118. f->cells--;
  119. }
  120. return cell;
  121. }
  122. /* dequeue cell from fifo and copy on user space */
  123. int snd_seq_fifo_cell_out(struct snd_seq_fifo *f,
  124. struct snd_seq_event_cell **cellp, int nonblock)
  125. {
  126. struct snd_seq_event_cell *cell;
  127. unsigned long flags;
  128. wait_queue_entry_t wait;
  129. if (snd_BUG_ON(!f))
  130. return -EINVAL;
  131. *cellp = NULL;
  132. init_waitqueue_entry(&wait, current);
  133. spin_lock_irqsave(&f->lock, flags);
  134. while ((cell = fifo_cell_out(f)) == NULL) {
  135. if (nonblock) {
  136. /* non-blocking - return immediately */
  137. spin_unlock_irqrestore(&f->lock, flags);
  138. return -EAGAIN;
  139. }
  140. set_current_state(TASK_INTERRUPTIBLE);
  141. add_wait_queue(&f->input_sleep, &wait);
  142. spin_unlock_irqrestore(&f->lock, flags);
  143. schedule();
  144. spin_lock_irqsave(&f->lock, flags);
  145. remove_wait_queue(&f->input_sleep, &wait);
  146. if (signal_pending(current)) {
  147. spin_unlock_irqrestore(&f->lock, flags);
  148. return -ERESTARTSYS;
  149. }
  150. }
  151. spin_unlock_irqrestore(&f->lock, flags);
  152. *cellp = cell;
  153. return 0;
  154. }
  155. void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f,
  156. struct snd_seq_event_cell *cell)
  157. {
  158. if (cell) {
  159. guard(spinlock_irqsave)(&f->lock);
  160. cell->next = f->head;
  161. f->head = cell;
  162. if (!f->tail)
  163. f->tail = cell;
  164. f->cells++;
  165. }
  166. }
  167. /* polling; return non-zero if queue is available */
  168. int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
  169. poll_table *wait)
  170. {
  171. poll_wait(file, &f->input_sleep, wait);
  172. return (f->cells > 0);
  173. }
  174. /* change the size of pool; all old events are removed */
  175. int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize)
  176. {
  177. struct snd_seq_pool *newpool, *oldpool;
  178. struct snd_seq_event_cell *cell, *next, *oldhead;
  179. if (snd_BUG_ON(!f || !f->pool))
  180. return -EINVAL;
  181. /* allocate new pool */
  182. newpool = snd_seq_pool_new(poolsize);
  183. if (newpool == NULL)
  184. return -ENOMEM;
  185. if (snd_seq_pool_init(newpool) < 0) {
  186. snd_seq_pool_delete(&newpool);
  187. return -ENOMEM;
  188. }
  189. scoped_guard(spinlock_irq, &f->lock) {
  190. /* remember old pool */
  191. oldpool = f->pool;
  192. oldhead = f->head;
  193. /* exchange pools */
  194. f->pool = newpool;
  195. f->head = NULL;
  196. f->tail = NULL;
  197. f->cells = 0;
  198. /* NOTE: overflow flag is not cleared */
  199. }
  200. /* close the old pool and wait until all users are gone */
  201. snd_seq_pool_mark_closing(oldpool);
  202. snd_use_lock_sync(&f->use_lock);
  203. /* release cells in old pool */
  204. for (cell = oldhead; cell; cell = next) {
  205. next = cell->next;
  206. snd_seq_cell_free(cell);
  207. }
  208. snd_seq_pool_delete(&oldpool);
  209. return 0;
  210. }
  211. /* get the number of unused cells safely */
  212. int snd_seq_fifo_unused_cells(struct snd_seq_fifo *f)
  213. {
  214. int cells;
  215. if (!f)
  216. return 0;
  217. snd_use_lock_use(&f->use_lock);
  218. scoped_guard(spinlock_irqsave, &f->lock)
  219. cells = snd_seq_unused_cells(f->pool);
  220. snd_use_lock_free(&f->use_lock);
  221. return cells;
  222. }