3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
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/driver.h>
23 #include <sound/core.h>
24 #include <linux/slab.h>
32 struct snd_seq_fifo *snd_seq_fifo_new(int poolsize)
34 struct snd_seq_fifo *f;
36 f = kzalloc(sizeof(*f), GFP_KERNEL);
38 snd_printd("malloc failed for snd_seq_fifo_new() \n");
42 f->pool = snd_seq_pool_new(poolsize);
43 if (f->pool == NULL) {
47 if (snd_seq_pool_init(f->pool) < 0) {
48 snd_seq_pool_delete(&f->pool);
53 spin_lock_init(&f->lock);
54 snd_use_lock_init(&f->use_lock);
55 init_waitqueue_head(&f->input_sleep);
56 atomic_set(&f->overflow, 0);
65 void snd_seq_fifo_delete(struct snd_seq_fifo **fifo)
67 struct snd_seq_fifo *f;
69 snd_assert(fifo != NULL, return);
71 snd_assert(f != NULL, return);
74 snd_seq_fifo_clear(f);
76 /* wake up clients if any */
77 if (waitqueue_active(&f->input_sleep))
78 wake_up(&f->input_sleep);
80 /* release resources...*/
81 /*....................*/
84 snd_seq_pool_done(f->pool);
85 snd_seq_pool_delete(&f->pool);
91 static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f);
94 void snd_seq_fifo_clear(struct snd_seq_fifo *f)
96 struct snd_seq_event_cell *cell;
99 /* clear overflow flag */
100 atomic_set(&f->overflow, 0);
102 snd_use_lock_sync(&f->use_lock);
103 spin_lock_irqsave(&f->lock, flags);
105 while ((cell = fifo_cell_out(f)) != NULL) {
106 snd_seq_cell_free(cell);
108 spin_unlock_irqrestore(&f->lock, flags);
112 /* enqueue event to fifo */
113 int snd_seq_fifo_event_in(struct snd_seq_fifo *f,
114 struct snd_seq_event *event)
116 struct snd_seq_event_cell *cell;
120 snd_assert(f != NULL, return -EINVAL);
122 snd_use_lock_use(&f->use_lock);
123 err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL); /* always non-blocking */
126 atomic_inc(&f->overflow);
127 snd_use_lock_free(&f->use_lock);
131 /* append new cells to fifo */
132 spin_lock_irqsave(&f->lock, flags);
134 f->tail->next = cell;
139 spin_unlock_irqrestore(&f->lock, flags);
142 if (waitqueue_active(&f->input_sleep))
143 wake_up(&f->input_sleep);
145 snd_use_lock_free(&f->use_lock);
147 return 0; /* success */
151 /* dequeue cell from fifo */
152 static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
154 struct snd_seq_event_cell *cell;
156 if ((cell = f->head) != NULL) {
157 f->head = cell->next;
159 /* reset tail if this was the last element */
170 /* dequeue cell from fifo and copy on user space */
171 int snd_seq_fifo_cell_out(struct snd_seq_fifo *f,
172 struct snd_seq_event_cell **cellp, int nonblock)
174 struct snd_seq_event_cell *cell;
178 snd_assert(f != NULL, return -EINVAL);
181 init_waitqueue_entry(&wait, current);
182 spin_lock_irqsave(&f->lock, flags);
183 while ((cell = fifo_cell_out(f)) == NULL) {
185 /* non-blocking - return immediately */
186 spin_unlock_irqrestore(&f->lock, flags);
189 set_current_state(TASK_INTERRUPTIBLE);
190 add_wait_queue(&f->input_sleep, &wait);
191 spin_unlock_irq(&f->lock);
193 spin_lock_irq(&f->lock);
194 remove_wait_queue(&f->input_sleep, &wait);
195 if (signal_pending(current)) {
196 spin_unlock_irqrestore(&f->lock, flags);
200 spin_unlock_irqrestore(&f->lock, flags);
207 void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f,
208 struct snd_seq_event_cell *cell)
213 spin_lock_irqsave(&f->lock, flags);
214 cell->next = f->head;
217 spin_unlock_irqrestore(&f->lock, flags);
222 /* polling; return non-zero if queue is available */
223 int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
226 poll_wait(file, &f->input_sleep, wait);
227 return (f->cells > 0);
230 /* change the size of pool; all old events are removed */
231 int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize)
234 struct snd_seq_pool *newpool, *oldpool;
235 struct snd_seq_event_cell *cell, *next, *oldhead;
237 snd_assert(f != NULL && f->pool != NULL, return -EINVAL);
239 /* allocate new pool */
240 newpool = snd_seq_pool_new(poolsize);
243 if (snd_seq_pool_init(newpool) < 0) {
244 snd_seq_pool_delete(&newpool);
248 spin_lock_irqsave(&f->lock, flags);
249 /* remember old pool */
257 /* NOTE: overflow flag is not cleared */
258 spin_unlock_irqrestore(&f->lock, flags);
260 /* release cells in old pool */
261 for (cell = oldhead; cell; cell = next) {
263 snd_seq_cell_free(cell);
265 snd_seq_pool_delete(&oldpool);