1 /*
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
22 #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 */
snd_seq_fifo_new(int poolsize)31 struct snd_seq_fifo *snd_seq_fifo_new(int poolsize)
32 {
33 struct snd_seq_fifo *f;
34
35 f = kzalloc(sizeof(*f), GFP_KERNEL);
36 if (!f)
37 return NULL;
38
39 f->pool = snd_seq_pool_new(poolsize);
40 if (f->pool == NULL) {
41 kfree(f);
42 return NULL;
43 }
44 if (snd_seq_pool_init(f->pool) < 0) {
45 snd_seq_pool_delete(&f->pool);
46 kfree(f);
47 return NULL;
48 }
49
50 spin_lock_init(&f->lock);
51 snd_use_lock_init(&f->use_lock);
52 init_waitqueue_head(&f->input_sleep);
53 atomic_set(&f->overflow, 0);
54
55 f->head = NULL;
56 f->tail = NULL;
57 f->cells = 0;
58
59 return f;
60 }
61
snd_seq_fifo_delete(struct snd_seq_fifo ** fifo)62 void snd_seq_fifo_delete(struct snd_seq_fifo **fifo)
63 {
64 struct snd_seq_fifo *f;
65
66 if (snd_BUG_ON(!fifo))
67 return;
68 f = *fifo;
69 if (snd_BUG_ON(!f))
70 return;
71 *fifo = NULL;
72
73 if (f->pool)
74 snd_seq_pool_mark_closing(f->pool);
75
76 snd_seq_fifo_clear(f);
77
78 /* wake up clients if any */
79 if (waitqueue_active(&f->input_sleep))
80 wake_up(&f->input_sleep);
81
82 /* release resources...*/
83 /*....................*/
84
85 if (f->pool) {
86 snd_seq_pool_done(f->pool);
87 snd_seq_pool_delete(&f->pool);
88 }
89
90 kfree(f);
91 }
92
93 static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f);
94
95 /* clear queue */
snd_seq_fifo_clear(struct snd_seq_fifo * f)96 void snd_seq_fifo_clear(struct snd_seq_fifo *f)
97 {
98 struct snd_seq_event_cell *cell;
99 unsigned long flags;
100
101 /* clear overflow flag */
102 atomic_set(&f->overflow, 0);
103
104 snd_use_lock_sync(&f->use_lock);
105 spin_lock_irqsave(&f->lock, flags);
106 /* drain the fifo */
107 while ((cell = fifo_cell_out(f)) != NULL) {
108 snd_seq_cell_free(cell);
109 }
110 spin_unlock_irqrestore(&f->lock, flags);
111 }
112
113
114 /* enqueue event to fifo */
snd_seq_fifo_event_in(struct snd_seq_fifo * f,struct snd_seq_event * event)115 int snd_seq_fifo_event_in(struct snd_seq_fifo *f,
116 struct snd_seq_event *event)
117 {
118 struct snd_seq_event_cell *cell;
119 unsigned long flags;
120 int err;
121
122 if (snd_BUG_ON(!f))
123 return -EINVAL;
124
125 snd_use_lock_use(&f->use_lock);
126 err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL); /* always non-blocking */
127 if (err < 0) {
128 if ((err == -ENOMEM) || (err == -EAGAIN))
129 atomic_inc(&f->overflow);
130 snd_use_lock_free(&f->use_lock);
131 return err;
132 }
133
134 /* append new cells to fifo */
135 spin_lock_irqsave(&f->lock, flags);
136 if (f->tail != NULL)
137 f->tail->next = cell;
138 f->tail = cell;
139 if (f->head == NULL)
140 f->head = cell;
141 f->cells++;
142 spin_unlock_irqrestore(&f->lock, flags);
143
144 /* wakeup client */
145 if (waitqueue_active(&f->input_sleep))
146 wake_up(&f->input_sleep);
147
148 snd_use_lock_free(&f->use_lock);
149
150 return 0; /* success */
151
152 }
153
154 /* dequeue cell from fifo */
fifo_cell_out(struct snd_seq_fifo * f)155 static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
156 {
157 struct snd_seq_event_cell *cell;
158
159 if ((cell = f->head) != NULL) {
160 f->head = cell->next;
161
162 /* reset tail if this was the last element */
163 if (f->tail == cell)
164 f->tail = NULL;
165
166 cell->next = NULL;
167 f->cells--;
168 }
169
170 return cell;
171 }
172
173 /* dequeue cell from fifo and copy on user space */
snd_seq_fifo_cell_out(struct snd_seq_fifo * f,struct snd_seq_event_cell ** cellp,int nonblock)174 int snd_seq_fifo_cell_out(struct snd_seq_fifo *f,
175 struct snd_seq_event_cell **cellp, int nonblock)
176 {
177 struct snd_seq_event_cell *cell;
178 unsigned long flags;
179 wait_queue_t wait;
180
181 if (snd_BUG_ON(!f))
182 return -EINVAL;
183
184 *cellp = NULL;
185 init_waitqueue_entry(&wait, current);
186 spin_lock_irqsave(&f->lock, flags);
187 while ((cell = fifo_cell_out(f)) == NULL) {
188 if (nonblock) {
189 /* non-blocking - return immediately */
190 spin_unlock_irqrestore(&f->lock, flags);
191 return -EAGAIN;
192 }
193 set_current_state(TASK_INTERRUPTIBLE);
194 add_wait_queue(&f->input_sleep, &wait);
195 spin_unlock_irq(&f->lock);
196 schedule();
197 spin_lock_irq(&f->lock);
198 remove_wait_queue(&f->input_sleep, &wait);
199 if (signal_pending(current)) {
200 spin_unlock_irqrestore(&f->lock, flags);
201 return -ERESTARTSYS;
202 }
203 }
204 spin_unlock_irqrestore(&f->lock, flags);
205 *cellp = cell;
206
207 return 0;
208 }
209
210
snd_seq_fifo_cell_putback(struct snd_seq_fifo * f,struct snd_seq_event_cell * cell)211 void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f,
212 struct snd_seq_event_cell *cell)
213 {
214 unsigned long flags;
215
216 if (cell) {
217 spin_lock_irqsave(&f->lock, flags);
218 cell->next = f->head;
219 f->head = cell;
220 f->cells++;
221 spin_unlock_irqrestore(&f->lock, flags);
222 }
223 }
224
225
226 /* polling; return non-zero if queue is available */
snd_seq_fifo_poll_wait(struct snd_seq_fifo * f,struct file * file,poll_table * wait)227 int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
228 poll_table *wait)
229 {
230 poll_wait(file, &f->input_sleep, wait);
231 return (f->cells > 0);
232 }
233
234 /* change the size of pool; all old events are removed */
snd_seq_fifo_resize(struct snd_seq_fifo * f,int poolsize)235 int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize)
236 {
237 unsigned long flags;
238 struct snd_seq_pool *newpool, *oldpool;
239 struct snd_seq_event_cell *cell, *next, *oldhead;
240
241 if (snd_BUG_ON(!f || !f->pool))
242 return -EINVAL;
243
244 /* allocate new pool */
245 newpool = snd_seq_pool_new(poolsize);
246 if (newpool == NULL)
247 return -ENOMEM;
248 if (snd_seq_pool_init(newpool) < 0) {
249 snd_seq_pool_delete(&newpool);
250 return -ENOMEM;
251 }
252
253 spin_lock_irqsave(&f->lock, flags);
254 /* remember old pool */
255 oldpool = f->pool;
256 oldhead = f->head;
257 /* exchange pools */
258 f->pool = newpool;
259 f->head = NULL;
260 f->tail = NULL;
261 f->cells = 0;
262 /* NOTE: overflow flag is not cleared */
263 spin_unlock_irqrestore(&f->lock, flags);
264
265 /* close the old pool and wait until all users are gone */
266 snd_seq_pool_mark_closing(oldpool);
267 snd_use_lock_sync(&f->use_lock);
268
269 /* release cells in old pool */
270 for (cell = oldhead; cell; cell = next) {
271 next = cell->next;
272 snd_seq_cell_free(cell);
273 }
274 snd_seq_pool_delete(&oldpool);
275
276 return 0;
277 }
278