blob: 0d75afa786bc6ffebe8ffd8dc5bde065397c00a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA sequencer FIFO
3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <sound/core.h>
23#include <linux/slab.h>
24#include "seq_fifo.h"
25#include "seq_lock.h"
26
27
28/* FIFO */
29
30/* create new fifo */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010031struct snd_seq_fifo *snd_seq_fifo_new(int poolsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010033 struct snd_seq_fifo *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Takashi Iwaiecca82b2005-09-09 14:20:49 +020035 f = kzalloc(sizeof(*f), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (f == NULL) {
37 snd_printd("malloc failed for snd_seq_fifo_new() \n");
38 return NULL;
39 }
40
41 f->pool = snd_seq_pool_new(poolsize);
42 if (f->pool == NULL) {
43 kfree(f);
44 return NULL;
45 }
46 if (snd_seq_pool_init(f->pool) < 0) {
47 snd_seq_pool_delete(&f->pool);
48 kfree(f);
49 return NULL;
50 }
51
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);
56
57 f->head = NULL;
58 f->tail = NULL;
59 f->cells = 0;
60
61 return f;
62}
63
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010064void snd_seq_fifo_delete(struct snd_seq_fifo **fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010066 struct snd_seq_fifo *f;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Takashi Iwai7eaa9432008-08-08 17:09:09 +020068 if (snd_BUG_ON(!fifo))
69 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 f = *fifo;
Takashi Iwai7eaa9432008-08-08 17:09:09 +020071 if (snd_BUG_ON(!f))
72 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *fifo = NULL;
74
75 snd_seq_fifo_clear(f);
76
77 /* wake up clients if any */
78 if (waitqueue_active(&f->input_sleep))
79 wake_up(&f->input_sleep);
80
81 /* release resources...*/
82 /*....................*/
83
84 if (f->pool) {
85 snd_seq_pool_done(f->pool);
86 snd_seq_pool_delete(&f->pool);
87 }
88
89 kfree(f);
90}
91
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010092static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* clear queue */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010095void snd_seq_fifo_clear(struct snd_seq_fifo *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +010097 struct snd_seq_event_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned long flags;
99
100 /* clear overflow flag */
101 atomic_set(&f->overflow, 0);
102
103 snd_use_lock_sync(&f->use_lock);
104 spin_lock_irqsave(&f->lock, flags);
105 /* drain the fifo */
106 while ((cell = fifo_cell_out(f)) != NULL) {
107 snd_seq_cell_free(cell);
108 }
109 spin_unlock_irqrestore(&f->lock, flags);
110}
111
112
113/* enqueue event to fifo */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100114int snd_seq_fifo_event_in(struct snd_seq_fifo *f,
115 struct snd_seq_event *event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100117 struct snd_seq_event_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned long flags;
119 int err;
120
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200121 if (snd_BUG_ON(!f))
122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 snd_use_lock_use(&f->use_lock);
125 err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL); /* always non-blocking */
126 if (err < 0) {
127 if (err == -ENOMEM)
128 atomic_inc(&f->overflow);
129 snd_use_lock_free(&f->use_lock);
130 return err;
131 }
132
133 /* append new cells to fifo */
134 spin_lock_irqsave(&f->lock, flags);
135 if (f->tail != NULL)
136 f->tail->next = cell;
137 f->tail = cell;
138 if (f->head == NULL)
139 f->head = cell;
140 f->cells++;
141 spin_unlock_irqrestore(&f->lock, flags);
142
143 /* wakeup client */
144 if (waitqueue_active(&f->input_sleep))
145 wake_up(&f->input_sleep);
146
147 snd_use_lock_free(&f->use_lock);
148
149 return 0; /* success */
150
151}
152
153/* dequeue cell from fifo */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100154static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100156 struct snd_seq_event_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if ((cell = f->head) != NULL) {
159 f->head = cell->next;
160
161 /* reset tail if this was the last element */
162 if (f->tail == cell)
163 f->tail = NULL;
164
165 cell->next = NULL;
166 f->cells--;
167 }
168
169 return cell;
170}
171
172/* dequeue cell from fifo and copy on user space */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100173int snd_seq_fifo_cell_out(struct snd_seq_fifo *f,
174 struct snd_seq_event_cell **cellp, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100176 struct snd_seq_event_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned long flags;
178 wait_queue_t wait;
179
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200180 if (snd_BUG_ON(!f))
181 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 *cellp = NULL;
184 init_waitqueue_entry(&wait, current);
185 spin_lock_irqsave(&f->lock, flags);
186 while ((cell = fifo_cell_out(f)) == NULL) {
187 if (nonblock) {
188 /* non-blocking - return immediately */
189 spin_unlock_irqrestore(&f->lock, flags);
190 return -EAGAIN;
191 }
192 set_current_state(TASK_INTERRUPTIBLE);
193 add_wait_queue(&f->input_sleep, &wait);
194 spin_unlock_irq(&f->lock);
195 schedule();
196 spin_lock_irq(&f->lock);
197 remove_wait_queue(&f->input_sleep, &wait);
198 if (signal_pending(current)) {
199 spin_unlock_irqrestore(&f->lock, flags);
200 return -ERESTARTSYS;
201 }
202 }
203 spin_unlock_irqrestore(&f->lock, flags);
204 *cellp = cell;
205
206 return 0;
207}
208
209
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100210void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f,
211 struct snd_seq_event_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned long flags;
214
215 if (cell) {
216 spin_lock_irqsave(&f->lock, flags);
217 cell->next = f->head;
218 f->head = cell;
219 f->cells++;
220 spin_unlock_irqrestore(&f->lock, flags);
221 }
222}
223
224
225/* polling; return non-zero if queue is available */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100226int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
227 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 poll_wait(file, &f->input_sleep, wait);
230 return (f->cells > 0);
231}
232
233/* change the size of pool; all old events are removed */
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100234int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 unsigned long flags;
Takashi Iwaic7e0b5b2005-11-17 14:04:02 +0100237 struct snd_seq_pool *newpool, *oldpool;
238 struct snd_seq_event_cell *cell, *next, *oldhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200240 if (snd_BUG_ON(!f || !f->pool))
241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* allocate new pool */
244 newpool = snd_seq_pool_new(poolsize);
245 if (newpool == NULL)
246 return -ENOMEM;
247 if (snd_seq_pool_init(newpool) < 0) {
248 snd_seq_pool_delete(&newpool);
249 return -ENOMEM;
250 }
251
252 spin_lock_irqsave(&f->lock, flags);
253 /* remember old pool */
254 oldpool = f->pool;
255 oldhead = f->head;
256 /* exchange pools */
257 f->pool = newpool;
258 f->head = NULL;
259 f->tail = NULL;
260 f->cells = 0;
261 /* NOTE: overflow flag is not cleared */
262 spin_unlock_irqrestore(&f->lock, flags);
263
264 /* release cells in old pool */
265 for (cell = oldhead; cell; cell = next) {
266 next = cell->next;
267 snd_seq_cell_free(cell);
268 }
269 snd_seq_pool_delete(&oldpool);
270
271 return 0;
272}