]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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/driver.h> | |
23 | #include <sound/core.h> | |
24 | #include <linux/slab.h> | |
25 | #include "seq_fifo.h" | |
26 | #include "seq_lock.h" | |
27 | ||
28 | ||
29 | /* FIFO */ | |
30 | ||
31 | /* create new fifo */ | |
32 | fifo_t *snd_seq_fifo_new(int poolsize) | |
33 | { | |
34 | fifo_t *f; | |
35 | ||
ecca82b4 | 36 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
1da177e4 LT |
37 | if (f == NULL) { |
38 | snd_printd("malloc failed for snd_seq_fifo_new() \n"); | |
39 | return NULL; | |
40 | } | |
41 | ||
42 | f->pool = snd_seq_pool_new(poolsize); | |
43 | if (f->pool == NULL) { | |
44 | kfree(f); | |
45 | return NULL; | |
46 | } | |
47 | if (snd_seq_pool_init(f->pool) < 0) { | |
48 | snd_seq_pool_delete(&f->pool); | |
49 | kfree(f); | |
50 | return NULL; | |
51 | } | |
52 | ||
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); | |
57 | ||
58 | f->head = NULL; | |
59 | f->tail = NULL; | |
60 | f->cells = 0; | |
61 | ||
62 | return f; | |
63 | } | |
64 | ||
65 | void snd_seq_fifo_delete(fifo_t **fifo) | |
66 | { | |
67 | fifo_t *f; | |
68 | ||
69 | snd_assert(fifo != NULL, return); | |
70 | f = *fifo; | |
71 | snd_assert(f != NULL, return); | |
72 | *fifo = NULL; | |
73 | ||
74 | snd_seq_fifo_clear(f); | |
75 | ||
76 | /* wake up clients if any */ | |
77 | if (waitqueue_active(&f->input_sleep)) | |
78 | wake_up(&f->input_sleep); | |
79 | ||
80 | /* release resources...*/ | |
81 | /*....................*/ | |
82 | ||
83 | if (f->pool) { | |
84 | snd_seq_pool_done(f->pool); | |
85 | snd_seq_pool_delete(&f->pool); | |
86 | } | |
87 | ||
88 | kfree(f); | |
89 | } | |
90 | ||
91 | static snd_seq_event_cell_t *fifo_cell_out(fifo_t *f); | |
92 | ||
93 | /* clear queue */ | |
94 | void snd_seq_fifo_clear(fifo_t *f) | |
95 | { | |
96 | snd_seq_event_cell_t *cell; | |
97 | unsigned long flags; | |
98 | ||
99 | /* clear overflow flag */ | |
100 | atomic_set(&f->overflow, 0); | |
101 | ||
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); | |
107 | } | |
108 | spin_unlock_irqrestore(&f->lock, flags); | |
109 | } | |
110 | ||
111 | ||
112 | /* enqueue event to fifo */ | |
113 | int snd_seq_fifo_event_in(fifo_t *f, snd_seq_event_t *event) | |
114 | { | |
115 | snd_seq_event_cell_t *cell; | |
116 | unsigned long flags; | |
117 | int err; | |
118 | ||
119 | snd_assert(f != NULL, return -EINVAL); | |
120 | ||
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; | |
128 | } | |
129 | ||
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); | |
139 | ||
140 | /* wakeup client */ | |
141 | if (waitqueue_active(&f->input_sleep)) | |
142 | wake_up(&f->input_sleep); | |
143 | ||
144 | snd_use_lock_free(&f->use_lock); | |
145 | ||
146 | return 0; /* success */ | |
147 | ||
148 | } | |
149 | ||
150 | /* dequeue cell from fifo */ | |
151 | static snd_seq_event_cell_t *fifo_cell_out(fifo_t *f) | |
152 | { | |
153 | snd_seq_event_cell_t *cell; | |
154 | ||
155 | if ((cell = f->head) != NULL) { | |
156 | f->head = cell->next; | |
157 | ||
158 | /* reset tail if this was the last element */ | |
159 | if (f->tail == cell) | |
160 | f->tail = NULL; | |
161 | ||
162 | cell->next = NULL; | |
163 | f->cells--; | |
164 | } | |
165 | ||
166 | return cell; | |
167 | } | |
168 | ||
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) | |
171 | { | |
172 | snd_seq_event_cell_t *cell; | |
173 | unsigned long flags; | |
174 | wait_queue_t wait; | |
175 | ||
176 | snd_assert(f != NULL, return -EINVAL); | |
177 | ||
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; | |
186 | } | |
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; | |
196 | } | |
197 | } | |
198 | spin_unlock_irqrestore(&f->lock, flags); | |
199 | *cellp = cell; | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | ||
205 | void snd_seq_fifo_cell_putback(fifo_t *f, snd_seq_event_cell_t *cell) | |
206 | { | |
207 | unsigned long flags; | |
208 | ||
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); | |
215 | } | |
216 | } | |
217 | ||
218 | ||
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) | |
221 | { | |
222 | poll_wait(file, &f->input_sleep, wait); | |
223 | return (f->cells > 0); | |
224 | } | |
225 | ||
226 | /* change the size of pool; all old events are removed */ | |
227 | int snd_seq_fifo_resize(fifo_t *f, int poolsize) | |
228 | { | |
229 | unsigned long flags; | |
230 | pool_t *newpool, *oldpool; | |
231 | snd_seq_event_cell_t *cell, *next, *oldhead; | |
232 | ||
233 | snd_assert(f != NULL && f->pool != NULL, return -EINVAL); | |
234 | ||
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; | |
242 | } | |
243 | ||
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); | |
255 | ||
256 | /* release cells in old pool */ | |
257 | for (cell = oldhead; cell; cell = next) { | |
258 | next = cell->next; | |
259 | snd_seq_cell_free(cell); | |
260 | } | |
261 | snd_seq_pool_delete(&oldpool); | |
262 | ||
263 | return 0; | |
264 | } |