6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/core.h>
23 #include <linux/slab.h>
24 #include <linux/sched/signal.h>
33 struct snd_seq_fifo *snd_seq_fifo_new(int poolsize)
35 struct snd_seq_fifo *f;
37 f = kzalloc(sizeof(*f), GFP_KERNEL);
41 f->pool = snd_seq_pool_new(poolsize);
42 if (f->pool == NULL) {
46 if (snd_seq_pool_init(f->pool) < 0) {
47 snd_seq_pool_delete(&f->pool);
52 spin_lock_init(&f->lock);
53 snd_use_lock_init(&f->use_lock);
54 init_waitqueue_head(&f->input_sleep);
55 atomic_set(&f->overflow, 0);
64 void snd_seq_fifo_delete(struct snd_seq_fifo **fifo)
66 struct snd_seq_fifo *f;
68 if (snd_BUG_ON(!fifo))
76 snd_seq_pool_mark_closing(f->pool);
78 snd_seq_fifo_clear(f);
80 /* wake up clients if any */
81 if (waitqueue_active(&f->input_sleep))
82 wake_up(&f->input_sleep);
84 /* release resources...*/
85 /*....................*/
88 snd_seq_pool_done(f->pool);
89 snd_seq_pool_delete(&f->pool);
95 static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f);
98 void snd_seq_fifo_clear(struct snd_seq_fifo *f)
100 struct snd_seq_event_cell *cell;
103 /* clear overflow flag */
104 atomic_set(&f->overflow, 0);
106 snd_use_lock_sync(&f->use_lock);
107 spin_lock_irqsave(&f->lock, flags);
109 while ((cell = fifo_cell_out(f)) != NULL) {
110 snd_seq_cell_free(cell);
112 spin_unlock_irqrestore(&f->lock, flags);
116 /* enqueue event to fifo */
117 int snd_seq_fifo_event_in(struct snd_seq_fifo *f,
118 struct snd_seq_event *event)
120 struct snd_seq_event_cell *cell;
127 snd_use_lock_use(&f->use_lock);
128 err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL); /* always non-blocking */
130 if ((err == -ENOMEM) || (err == -EAGAIN))
131 atomic_inc(&f->overflow);
132 snd_use_lock_free(&f->use_lock);
136 /* append new cells to fifo */
137 spin_lock_irqsave(&f->lock, flags);
139 f->tail->next = cell;
145 spin_unlock_irqrestore(&f->lock, flags);
148 if (waitqueue_active(&f->input_sleep))
149 wake_up(&f->input_sleep);
151 snd_use_lock_free(&f->use_lock);
153 return 0; /* success */
157 /* dequeue cell from fifo */
158 static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
160 struct snd_seq_event_cell *cell;
162 if ((cell = f->head) != NULL) {
163 f->head = cell->next;
165 /* reset tail if this was the last element */
176 /* dequeue cell from fifo and copy on user space */
177 int snd_seq_fifo_cell_out(struct snd_seq_fifo *f,
178 struct snd_seq_event_cell **cellp, int nonblock)
180 struct snd_seq_event_cell *cell;
182 wait_queue_entry_t wait;
188 init_waitqueue_entry(&wait, current);
189 spin_lock_irqsave(&f->lock, flags);
190 while ((cell = fifo_cell_out(f)) == NULL) {
192 /* non-blocking - return immediately */
193 spin_unlock_irqrestore(&f->lock, flags);
196 set_current_state(TASK_INTERRUPTIBLE);
197 add_wait_queue(&f->input_sleep, &wait);
198 spin_unlock_irq(&f->lock);
200 spin_lock_irq(&f->lock);
201 remove_wait_queue(&f->input_sleep, &wait);
202 if (signal_pending(current)) {
203 spin_unlock_irqrestore(&f->lock, flags);
207 spin_unlock_irqrestore(&f->lock, flags);
214 void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f,
215 struct snd_seq_event_cell *cell)
220 spin_lock_irqsave(&f->lock, flags);
221 cell->next = f->head;
226 spin_unlock_irqrestore(&f->lock, flags);
231 /* polling; return non-zero if queue is available */
232 int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
235 poll_wait(file, &f->input_sleep, wait);
236 return (f->cells > 0);
239 /* change the size of pool; all old events are removed */
240 int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize)
243 struct snd_seq_pool *newpool, *oldpool;
244 struct snd_seq_event_cell *cell, *next, *oldhead;
246 if (snd_BUG_ON(!f || !f->pool))
249 /* allocate new pool */
250 newpool = snd_seq_pool_new(poolsize);
253 if (snd_seq_pool_init(newpool) < 0) {
254 snd_seq_pool_delete(&newpool);
258 spin_lock_irqsave(&f->lock, flags);
259 /* remember old pool */
267 /* NOTE: overflow flag is not cleared */
268 spin_unlock_irqrestore(&f->lock, flags);
270 /* close the old pool and wait until all users are gone */
271 snd_seq_pool_mark_closing(oldpool);
272 snd_use_lock_sync(&f->use_lock);
274 /* release cells in old pool */
275 for (cell = oldhead; cell; cell = next) {
277 snd_seq_cell_free(cell);
279 snd_seq_pool_delete(&oldpool);