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 fifo_t *snd_seq_fifo_new(int poolsize)
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(fifo_t **fifo)
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 snd_seq_event_cell_t *fifo_cell_out(fifo_t *f);
94 void snd_seq_fifo_clear(fifo_t *f)
96 snd_seq_event_cell_t *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(fifo_t *f, snd_seq_event_t *event)
115 snd_seq_event_cell_t *cell;
119 snd_assert(f != NULL, return -EINVAL);
121 snd_use_lock_use(&f->use_lock);
122 err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL); /* always non-blocking */
125 atomic_inc(&f->overflow);
126 snd_use_lock_free(&f->use_lock);
130 /* append new cells to fifo */
131 spin_lock_irqsave(&f->lock, flags);
133 f->tail->next = cell;
138 spin_unlock_irqrestore(&f->lock, flags);
141 if (waitqueue_active(&f->input_sleep))
142 wake_up(&f->input_sleep);
144 snd_use_lock_free(&f->use_lock);
146 return 0; /* success */
150 /* dequeue cell from fifo */
151 static snd_seq_event_cell_t *fifo_cell_out(fifo_t *f)
153 snd_seq_event_cell_t *cell;
155 if ((cell = f->head) != NULL) {
156 f->head = cell->next;
158 /* reset tail if this was the last element */
169 /* dequeue cell from fifo and copy on user space */
170 int snd_seq_fifo_cell_out(fifo_t *f, snd_seq_event_cell_t **cellp, int nonblock)
172 snd_seq_event_cell_t *cell;
176 snd_assert(f != NULL, return -EINVAL);
179 init_waitqueue_entry(&wait, current);
180 spin_lock_irqsave(&f->lock, flags);
181 while ((cell = fifo_cell_out(f)) == NULL) {
183 /* non-blocking - return immediately */
184 spin_unlock_irqrestore(&f->lock, flags);
187 set_current_state(TASK_INTERRUPTIBLE);
188 add_wait_queue(&f->input_sleep, &wait);
189 spin_unlock_irq(&f->lock);
191 spin_lock_irq(&f->lock);
192 remove_wait_queue(&f->input_sleep, &wait);
193 if (signal_pending(current)) {
194 spin_unlock_irqrestore(&f->lock, flags);
198 spin_unlock_irqrestore(&f->lock, flags);
205 void snd_seq_fifo_cell_putback(fifo_t *f, snd_seq_event_cell_t *cell)
210 spin_lock_irqsave(&f->lock, flags);
211 cell->next = f->head;
214 spin_unlock_irqrestore(&f->lock, flags);
219 /* polling; return non-zero if queue is available */
220 int snd_seq_fifo_poll_wait(fifo_t *f, struct file *file, poll_table *wait)
222 poll_wait(file, &f->input_sleep, wait);
223 return (f->cells > 0);
226 /* change the size of pool; all old events are removed */
227 int snd_seq_fifo_resize(fifo_t *f, int poolsize)
230 pool_t *newpool, *oldpool;
231 snd_seq_event_cell_t *cell, *next, *oldhead;
233 snd_assert(f != NULL && f->pool != NULL, return -EINVAL);
235 /* allocate new pool */
236 newpool = snd_seq_pool_new(poolsize);
239 if (snd_seq_pool_init(newpool) < 0) {
240 snd_seq_pool_delete(&newpool);
244 spin_lock_irqsave(&f->lock, flags);
245 /* remember old pool */
253 /* NOTE: overflow flag is not cleared */
254 spin_unlock_irqrestore(&f->lock, flags);
256 /* release cells in old pool */
257 for (cell = oldhead; cell; cell = next) {
259 snd_seq_cell_free(cell);
261 snd_seq_pool_delete(&oldpool);