seq_fifo.c 5.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
/*
 *   ALSA sequencer FIFO
 *   Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
 */

#include <sound/core.h>
#include <linux/slab.h>
9 10
#include <linux/sched/signal.h>

L
Linus Torvalds 已提交
11 12 13 14 15 16 17
#include "seq_fifo.h"
#include "seq_lock.h"


/* FIFO */

/* create new fifo */
18
struct snd_seq_fifo *snd_seq_fifo_new(int poolsize)
L
Linus Torvalds 已提交
19
{
20
	struct snd_seq_fifo *f;
L
Linus Torvalds 已提交
21

22
	f = kzalloc(sizeof(*f), GFP_KERNEL);
23
	if (!f)
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		return NULL;

	f->pool = snd_seq_pool_new(poolsize);
	if (f->pool == NULL) {
		kfree(f);
		return NULL;
	}
	if (snd_seq_pool_init(f->pool) < 0) {
		snd_seq_pool_delete(&f->pool);
		kfree(f);
		return NULL;
	}

	spin_lock_init(&f->lock);
	snd_use_lock_init(&f->use_lock);
	init_waitqueue_head(&f->input_sleep);
	atomic_set(&f->overflow, 0);

	f->head = NULL;
	f->tail = NULL;
	f->cells = 0;
	
	return f;
}

49
void snd_seq_fifo_delete(struct snd_seq_fifo **fifo)
L
Linus Torvalds 已提交
50
{
51
	struct snd_seq_fifo *f;
L
Linus Torvalds 已提交
52

53 54
	if (snd_BUG_ON(!fifo))
		return;
L
Linus Torvalds 已提交
55
	f = *fifo;
56 57
	if (snd_BUG_ON(!f))
		return;
L
Linus Torvalds 已提交
58 59
	*fifo = NULL;

60 61 62
	if (f->pool)
		snd_seq_pool_mark_closing(f->pool);

L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	snd_seq_fifo_clear(f);

	/* wake up clients if any */
	if (waitqueue_active(&f->input_sleep))
		wake_up(&f->input_sleep);

	/* release resources...*/
	/*....................*/

	if (f->pool) {
		snd_seq_pool_done(f->pool);
		snd_seq_pool_delete(&f->pool);
	}
	
	kfree(f);
}

80
static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f);
L
Linus Torvalds 已提交
81 82

/* clear queue */
83
void snd_seq_fifo_clear(struct snd_seq_fifo *f)
L
Linus Torvalds 已提交
84
{
85
	struct snd_seq_event_cell *cell;
L
Linus Torvalds 已提交
86 87 88 89 90

	/* clear overflow flag */
	atomic_set(&f->overflow, 0);

	snd_use_lock_sync(&f->use_lock);
91
	spin_lock_irq(&f->lock);
L
Linus Torvalds 已提交
92 93 94 95
	/* drain the fifo */
	while ((cell = fifo_cell_out(f)) != NULL) {
		snd_seq_cell_free(cell);
	}
96
	spin_unlock_irq(&f->lock);
L
Linus Torvalds 已提交
97 98 99 100
}


/* enqueue event to fifo */
101 102
int snd_seq_fifo_event_in(struct snd_seq_fifo *f,
			  struct snd_seq_event *event)
L
Linus Torvalds 已提交
103
{
104
	struct snd_seq_event_cell *cell;
L
Linus Torvalds 已提交
105 106 107
	unsigned long flags;
	int err;

108 109
	if (snd_BUG_ON(!f))
		return -EINVAL;
L
Linus Torvalds 已提交
110 111

	snd_use_lock_use(&f->use_lock);
112
	err = snd_seq_event_dup(f->pool, event, &cell, 1, NULL, NULL); /* always non-blocking */
L
Linus Torvalds 已提交
113
	if (err < 0) {
114
		if ((err == -ENOMEM) || (err == -EAGAIN))
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126
			atomic_inc(&f->overflow);
		snd_use_lock_free(&f->use_lock);
		return err;
	}
		
	/* append new cells to fifo */
	spin_lock_irqsave(&f->lock, flags);
	if (f->tail != NULL)
		f->tail->next = cell;
	f->tail = cell;
	if (f->head == NULL)
		f->head = cell;
127
	cell->next = NULL;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
	f->cells++;
	spin_unlock_irqrestore(&f->lock, flags);

	/* wakeup client */
	if (waitqueue_active(&f->input_sleep))
		wake_up(&f->input_sleep);

	snd_use_lock_free(&f->use_lock);

	return 0; /* success */

}

/* dequeue cell from fifo */
142
static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
L
Linus Torvalds 已提交
143
{
144
	struct snd_seq_event_cell *cell;
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	if ((cell = f->head) != NULL) {
		f->head = cell->next;

		/* reset tail if this was the last element */
		if (f->tail == cell)
			f->tail = NULL;

		cell->next = NULL;
		f->cells--;
	}

	return cell;
}

/* dequeue cell from fifo and copy on user space */
161 162
int snd_seq_fifo_cell_out(struct snd_seq_fifo *f,
			  struct snd_seq_event_cell **cellp, int nonblock)
L
Linus Torvalds 已提交
163
{
164
	struct snd_seq_event_cell *cell;
L
Linus Torvalds 已提交
165
	unsigned long flags;
166
	wait_queue_entry_t wait;
L
Linus Torvalds 已提交
167

168 169
	if (snd_BUG_ON(!f))
		return -EINVAL;
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181

	*cellp = NULL;
	init_waitqueue_entry(&wait, current);
	spin_lock_irqsave(&f->lock, flags);
	while ((cell = fifo_cell_out(f)) == NULL) {
		if (nonblock) {
			/* non-blocking - return immediately */
			spin_unlock_irqrestore(&f->lock, flags);
			return -EAGAIN;
		}
		set_current_state(TASK_INTERRUPTIBLE);
		add_wait_queue(&f->input_sleep, &wait);
182
		spin_unlock_irqrestore(&f->lock, flags);
L
Linus Torvalds 已提交
183
		schedule();
184
		spin_lock_irqsave(&f->lock, flags);
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
		remove_wait_queue(&f->input_sleep, &wait);
		if (signal_pending(current)) {
			spin_unlock_irqrestore(&f->lock, flags);
			return -ERESTARTSYS;
		}
	}
	spin_unlock_irqrestore(&f->lock, flags);
	*cellp = cell;

	return 0;
}


198 199
void snd_seq_fifo_cell_putback(struct snd_seq_fifo *f,
			       struct snd_seq_event_cell *cell)
L
Linus Torvalds 已提交
200 201 202 203 204 205 206
{
	unsigned long flags;

	if (cell) {
		spin_lock_irqsave(&f->lock, flags);
		cell->next = f->head;
		f->head = cell;
207 208
		if (!f->tail)
			f->tail = cell;
L
Linus Torvalds 已提交
209 210 211 212 213 214 215
		f->cells++;
		spin_unlock_irqrestore(&f->lock, flags);
	}
}


/* polling; return non-zero if queue is available */
216 217
int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
			   poll_table *wait)
L
Linus Torvalds 已提交
218 219 220 221 222 223
{
	poll_wait(file, &f->input_sleep, wait);
	return (f->cells > 0);
}

/* change the size of pool; all old events are removed */
224
int snd_seq_fifo_resize(struct snd_seq_fifo *f, int poolsize)
L
Linus Torvalds 已提交
225
{
226 227
	struct snd_seq_pool *newpool, *oldpool;
	struct snd_seq_event_cell *cell, *next, *oldhead;
L
Linus Torvalds 已提交
228

229 230
	if (snd_BUG_ON(!f || !f->pool))
		return -EINVAL;
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240

	/* allocate new pool */
	newpool = snd_seq_pool_new(poolsize);
	if (newpool == NULL)
		return -ENOMEM;
	if (snd_seq_pool_init(newpool) < 0) {
		snd_seq_pool_delete(&newpool);
		return -ENOMEM;
	}

241
	spin_lock_irq(&f->lock);
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250
	/* remember old pool */
	oldpool = f->pool;
	oldhead = f->head;
	/* exchange pools */
	f->pool = newpool;
	f->head = NULL;
	f->tail = NULL;
	f->cells = 0;
	/* NOTE: overflow flag is not cleared */
251
	spin_unlock_irq(&f->lock);
L
Linus Torvalds 已提交
252

253 254 255 256
	/* close the old pool and wait until all users are gone */
	snd_seq_pool_mark_closing(oldpool);
	snd_use_lock_sync(&f->use_lock);

L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265
	/* release cells in old pool */
	for (cell = oldhead; cell; cell = next) {
		next = cell->next;
		snd_seq_cell_free(cell);
	}
	snd_seq_pool_delete(&oldpool);

	return 0;
}
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

/* get the number of unused cells safely */
int snd_seq_fifo_unused_cells(struct snd_seq_fifo *f)
{
	unsigned long flags;
	int cells;

	if (!f)
		return 0;

	snd_use_lock_use(&f->use_lock);
	spin_lock_irqsave(&f->lock, flags);
	cells = snd_seq_unused_cells(f->pool);
	spin_unlock_irqrestore(&f->lock, flags);
	snd_use_lock_free(&f->use_lock);
	return cells;
}