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