~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/seq/seq_fifo.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *   ALSA sequencer FIFO
  3  *   Copyright (c) 1998 by Frank van de Pol <fvdpol@home.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 
 36         f = snd_kcalloc(sizeof(fifo_t), GFP_KERNEL);
 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 
175         snd_assert(f != NULL, return -EINVAL);
176 
177         *cellp = NULL;
178         spin_lock_irqsave(&f->lock, flags);
179         while ((cell = fifo_cell_out(f)) == NULL) {
180                 if (nonblock) {
181                         /* non-blocking - return immediately */
182                         spin_unlock_irqrestore(&f->lock, flags);
183                         return -EAGAIN;
184                 }
185                 spin_unlock(&f->lock);
186                 interruptible_sleep_on(&f->input_sleep);
187                 spin_lock(&f->lock);
188 
189                 if (signal_pending(current)) {
190                         spin_unlock_irqrestore(&f->lock, flags);
191                         return -ERESTARTSYS;
192                 }
193         }
194         *cellp = cell;
195         spin_unlock_irqrestore(&f->lock, flags);
196 
197         return 0;
198 }
199 
200 
201 void snd_seq_fifo_cell_putback(fifo_t *f, snd_seq_event_cell_t *cell)
202 {
203         unsigned long flags;
204 
205         if (cell) {
206                 spin_lock_irqsave(&f->lock, flags);
207                 cell->next = f->head;
208                 f->head = cell;
209                 f->cells++;
210                 spin_unlock_irqrestore(&f->lock, flags);
211         }
212 }
213 
214 
215 /* polling; return non-zero if queue is available */
216 int snd_seq_fifo_poll_wait(fifo_t *f, struct file *file, poll_table *wait)
217 {
218         poll_wait(file, &f->input_sleep, wait);
219         return (f->cells > 0);
220 }
221 
222 /* change the size of pool; all old events are removed */
223 int snd_seq_fifo_resize(fifo_t *f, int poolsize)
224 {
225         unsigned long flags;
226         pool_t *newpool, *oldpool;
227         snd_seq_event_cell_t *cell, *next, *oldhead;
228 
229         snd_assert(f != NULL && f->pool != NULL, return -EINVAL);
230 
231         /* allocate new pool */
232         newpool = snd_seq_pool_new(poolsize);
233         if (newpool == NULL)
234                 return -ENOMEM;
235         if (snd_seq_pool_init(newpool) < 0) {
236                 snd_seq_pool_delete(&newpool);
237                 return -ENOMEM;
238         }
239 
240         spin_lock_irqsave(&f->lock, flags);
241         /* remember old pool */
242         oldpool = f->pool;
243         oldhead = f->head;
244         /* exchange pools */
245         f->pool = newpool;
246         f->head = NULL;
247         f->tail = NULL;
248         f->cells = 0;
249         /* NOTE: overflow flag is not cleared */
250         spin_unlock_irqrestore(&f->lock, flags);
251 
252         /* release cells in old pool */
253         for (cell = oldhead; cell; cell = next) {
254                 next = cell->next;
255                 snd_seq_cell_free(cell);
256         }
257         snd_seq_pool_delete(&oldpool);
258 
259         return 0;
260 }
261 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp