[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / sound / core / seq / seq_fifo.c
blob3b7647ca7ad9e7a35bdccc2dcdc52e2bfe56cd16
1 /*
2 * ALSA sequencer FIFO
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>
25 #include "seq_fifo.h"
26 #include "seq_lock.h"
29 /* FIFO */
31 /* create new fifo */
32 fifo_t *snd_seq_fifo_new(int poolsize)
34 fifo_t *f;
36 f = kcalloc(1, sizeof(*f), GFP_KERNEL);
37 if (f == NULL) {
38 snd_printd("malloc failed for snd_seq_fifo_new() \n");
39 return NULL;
42 f->pool = snd_seq_pool_new(poolsize);
43 if (f->pool == NULL) {
44 kfree(f);
45 return NULL;
47 if (snd_seq_pool_init(f->pool) < 0) {
48 snd_seq_pool_delete(&f->pool);
49 kfree(f);
50 return NULL;
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);
58 f->head = NULL;
59 f->tail = NULL;
60 f->cells = 0;
62 return f;
65 void snd_seq_fifo_delete(fifo_t **fifo)
67 fifo_t *f;
69 snd_assert(fifo != NULL, return);
70 f = *fifo;
71 snd_assert(f != NULL, return);
72 *fifo = NULL;
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 /*....................*/
83 if (f->pool) {
84 snd_seq_pool_done(f->pool);
85 snd_seq_pool_delete(&f->pool);
88 kfree(f);
91 static snd_seq_event_cell_t *fifo_cell_out(fifo_t *f);
93 /* clear queue */
94 void snd_seq_fifo_clear(fifo_t *f)
96 snd_seq_event_cell_t *cell;
97 unsigned long flags;
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);
104 /* drain the fifo */
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;
116 unsigned long flags;
117 int err;
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 */
123 if (err < 0) {
124 if (err == -ENOMEM)
125 atomic_inc(&f->overflow);
126 snd_use_lock_free(&f->use_lock);
127 return err;
130 /* append new cells to fifo */
131 spin_lock_irqsave(&f->lock, flags);
132 if (f->tail != NULL)
133 f->tail->next = cell;
134 f->tail = cell;
135 if (f->head == NULL)
136 f->head = cell;
137 f->cells++;
138 spin_unlock_irqrestore(&f->lock, flags);
140 /* wakeup client */
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 */
159 if (f->tail == cell)
160 f->tail = NULL;
162 cell->next = NULL;
163 f->cells--;
166 return cell;
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;
173 unsigned long flags;
174 wait_queue_t wait;
176 snd_assert(f != NULL, return -EINVAL);
178 *cellp = NULL;
179 init_waitqueue_entry(&wait, current);
180 spin_lock_irqsave(&f->lock, flags);
181 while ((cell = fifo_cell_out(f)) == NULL) {
182 if (nonblock) {
183 /* non-blocking - return immediately */
184 spin_unlock_irqrestore(&f->lock, flags);
185 return -EAGAIN;
187 set_current_state(TASK_INTERRUPTIBLE);
188 add_wait_queue(&f->input_sleep, &wait);
189 spin_unlock_irq(&f->lock);
190 schedule();
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);
195 return -ERESTARTSYS;
198 spin_unlock_irqrestore(&f->lock, flags);
199 *cellp = cell;
201 return 0;
205 void snd_seq_fifo_cell_putback(fifo_t *f, snd_seq_event_cell_t *cell)
207 unsigned long flags;
209 if (cell) {
210 spin_lock_irqsave(&f->lock, flags);
211 cell->next = f->head;
212 f->head = cell;
213 f->cells++;
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)
229 unsigned long flags;
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);
237 if (newpool == NULL)
238 return -ENOMEM;
239 if (snd_seq_pool_init(newpool) < 0) {
240 snd_seq_pool_delete(&newpool);
241 return -ENOMEM;
244 spin_lock_irqsave(&f->lock, flags);
245 /* remember old pool */
246 oldpool = f->pool;
247 oldhead = f->head;
248 /* exchange pools */
249 f->pool = newpool;
250 f->head = NULL;
251 f->tail = NULL;
252 f->cells = 0;
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) {
258 next = cell->next;
259 snd_seq_cell_free(cell);
261 snd_seq_pool_delete(&oldpool);
263 return 0;