xref: /linux/sound/core/seq/seq_prioq.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   ALSA sequencer Priority Queue
4  *   Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
5  */
6 
7 #include <linux/time.h>
8 #include <linux/slab.h>
9 #include <sound/core.h>
10 #include "seq_timer.h"
11 #include "seq_prioq.h"
12 
13 
14 /* Implementation is a simple linked list for now...
15 
16    This priority queue orders the events on timestamp. For events with an
17    equeal timestamp the queue behaves as a FIFO.
18 
19    *
20    *           +-------+
21    *  Head --> | first |
22    *           +-------+
23    *                 |next
24    *           +-----v-+
25    *           |       |
26    *           +-------+
27    *                 |
28    *           +-----v-+
29    *           |       |
30    *           +-------+
31    *                 |
32    *           +-----v-+
33    *  Tail --> | last  |
34    *           +-------+
35    *
36 
37  */
38 
39 
40 
41 /* create new prioq (constructor) */
42 struct snd_seq_prioq *snd_seq_prioq_new(void)
43 {
44 	struct snd_seq_prioq *f;
45 
46 	f = kzalloc_obj(*f);
47 	if (!f)
48 		return NULL;
49 
50 	spin_lock_init(&f->lock);
51 	f->head = NULL;
52 	f->tail = NULL;
53 	f->cells = 0;
54 
55 	return f;
56 }
57 
58 /* delete prioq (destructor) */
59 void snd_seq_prioq_delete(struct snd_seq_prioq **fifo)
60 {
61 	struct snd_seq_prioq *f = *fifo;
62 	*fifo = NULL;
63 
64 	if (f == NULL) {
65 		pr_debug("ALSA: seq: snd_seq_prioq_delete() called with NULL prioq\n");
66 		return;
67 	}
68 
69 	/* release resources...*/
70 	/*....................*/
71 
72 	if (f->cells > 0) {
73 		/* drain prioQ */
74 		while (f->cells > 0)
75 			snd_seq_cell_free(snd_seq_prioq_cell_out(f, NULL));
76 	}
77 
78 	kfree(f);
79 }
80 
81 
82 
83 
84 /* compare timestamp between events */
85 /* return 1 if a >= b; 0 */
86 static inline int compare_timestamp(struct snd_seq_event *a,
87 				    struct snd_seq_event *b)
88 {
89 	if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
90 		/* compare ticks */
91 		return (snd_seq_compare_tick_time(&a->time.tick, &b->time.tick));
92 	} else {
93 		/* compare real time */
94 		return (snd_seq_compare_real_time(&a->time.time, &b->time.time));
95 	}
96 }
97 
98 /* compare timestamp between events */
99 /* return negative if a < b;
100  *        zero     if a = b;
101  *        positive if a > b;
102  */
103 static inline int compare_timestamp_rel(struct snd_seq_event *a,
104 					struct snd_seq_event *b)
105 {
106 	if ((a->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK) {
107 		/* compare ticks */
108 		if (a->time.tick > b->time.tick)
109 			return 1;
110 		else if (a->time.tick == b->time.tick)
111 			return 0;
112 		else
113 			return -1;
114 	} else {
115 		/* compare real time */
116 		if (a->time.time.tv_sec > b->time.time.tv_sec)
117 			return 1;
118 		else if (a->time.time.tv_sec == b->time.time.tv_sec) {
119 			if (a->time.time.tv_nsec > b->time.time.tv_nsec)
120 				return 1;
121 			else if (a->time.time.tv_nsec == b->time.time.tv_nsec)
122 				return 0;
123 			else
124 				return -1;
125 		} else
126 			return -1;
127 	}
128 }
129 
130 /* enqueue cell to prioq */
131 int snd_seq_prioq_cell_in(struct snd_seq_prioq * f,
132 			  struct snd_seq_event_cell * cell)
133 {
134 	struct snd_seq_event_cell *cur, *prev;
135 	int remaining;
136 	int prior;
137 
138 	if (snd_BUG_ON(!f || !cell))
139 		return -EINVAL;
140 
141 	/* check flags */
142 	prior = (cell->event.flags & SNDRV_SEQ_PRIORITY_MASK);
143 
144 	guard(spinlock_irqsave)(&f->lock);
145 
146 	/* check if this element needs to inserted at the end (ie. ordered
147 	   data is inserted) This will be very likeley if a sequencer
148 	   application or midi file player is feeding us (sequential) data */
149 	if (f->tail && !prior) {
150 		if (compare_timestamp(&cell->event, &f->tail->event)) {
151 			/* add new cell to tail of the fifo */
152 			f->tail->next = cell;
153 			f->tail = cell;
154 			cell->next = NULL;
155 			f->cells++;
156 			return 0;
157 		}
158 	}
159 	/* traverse list of elements to find the place where the new cell is
160 	   to be inserted... Note that this is a order n process ! */
161 
162 	prev = NULL;		/* previous cell */
163 	cur = f->head;		/* cursor */
164 
165 	remaining = f->cells;
166 	while (cur != NULL) {
167 		/* compare timestamps */
168 		int rel = compare_timestamp_rel(&cell->event, &cur->event);
169 
170 		if (remaining-- <= 0) {
171 			pr_err("ALSA: seq: inconsistent prioq cell count\n");
172 			return -EINVAL;
173 		}
174 
175 		if (rel < 0)
176 			/* new cell has earlier schedule time, */
177 			break;
178 		else if (rel == 0 && prior)
179 			/* equal schedule time and prior to others */
180 			break;
181 		/* new cell has equal or larger schedule time, */
182 		/* move cursor to next cell */
183 		prev = cur;
184 		cur = cur->next;
185 	}
186 
187 	/* insert it before cursor */
188 	if (prev != NULL)
189 		prev->next = cell;
190 	cell->next = cur;
191 
192 	if (f->head == cur) /* this is the first cell, set head to it */
193 		f->head = cell;
194 	if (cur == NULL) /* reached end of the list */
195 		f->tail = cell;
196 	f->cells++;
197 	return 0;
198 }
199 
200 /* return 1 if the current time >= event timestamp */
201 static int event_is_ready(struct snd_seq_event *ev, void *current_time)
202 {
203 	if ((ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) == SNDRV_SEQ_TIME_STAMP_TICK)
204 		return snd_seq_compare_tick_time(current_time, &ev->time.tick);
205 	else
206 		return snd_seq_compare_real_time(current_time, &ev->time.time);
207 }
208 
209 /* dequeue cell from prioq */
210 struct snd_seq_event_cell *snd_seq_prioq_cell_out(struct snd_seq_prioq *f,
211 						  void *current_time)
212 {
213 	struct snd_seq_event_cell *cell;
214 
215 	if (f == NULL) {
216 		pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
217 		return NULL;
218 	}
219 
220 	guard(spinlock_irqsave)(&f->lock);
221 	cell = f->head;
222 	if (cell && current_time && !event_is_ready(&cell->event, current_time))
223 		cell = NULL;
224 	if (cell) {
225 		f->head = cell->next;
226 
227 		/* reset tail if this was the last element */
228 		if (f->tail == cell)
229 			f->tail = NULL;
230 
231 		cell->next = NULL;
232 		f->cells--;
233 	}
234 
235 	return cell;
236 }
237 
238 /* return number of events available in prioq */
239 int snd_seq_prioq_avail(struct snd_seq_prioq * f)
240 {
241 	if (f == NULL) {
242 		pr_debug("ALSA: seq: snd_seq_prioq_cell_in() called with NULL prioq\n");
243 		return 0;
244 	}
245 	return f->cells;
246 }
247 
248 /* remove cells matching with the condition */
249 static void prioq_remove_cells(struct snd_seq_prioq *f,
250 			       bool (*match)(struct snd_seq_event_cell *cell,
251 					     void *arg),
252 			       void *arg)
253 {
254 	register struct snd_seq_event_cell *cell, *next;
255 	struct snd_seq_event_cell *prev = NULL;
256 	struct snd_seq_event_cell *freefirst = NULL, *freeprev = NULL, *freenext;
257 
258 	/* collect all removed cells */
259 	scoped_guard(spinlock_irqsave, &f->lock) {
260 		for (cell = f->head; cell; cell = next) {
261 			next = cell->next;
262 			if (!match(cell, arg)) {
263 				prev = cell;
264 				continue;
265 			}
266 
267 			/* remove cell from prioq */
268 			if (cell == f->head)
269 				f->head = cell->next;
270 			else
271 				prev->next = cell->next;
272 			if (cell == f->tail)
273 				f->tail = cell->next;
274 			f->cells--;
275 
276 			/* add cell to free list */
277 			cell->next = NULL;
278 			if (freefirst == NULL)
279 				freefirst = cell;
280 			else
281 				freeprev->next = cell;
282 			freeprev = cell;
283 		}
284 	}
285 
286 	/* remove selected cells */
287 	while (freefirst) {
288 		freenext = freefirst->next;
289 		snd_seq_cell_free(freefirst);
290 		freefirst = freenext;
291 	}
292 }
293 
294 struct prioq_match_arg {
295 	int client;
296 	int timestamp;
297 };
298 
299 static inline bool prioq_match(struct snd_seq_event_cell *cell, void *arg)
300 {
301 	struct prioq_match_arg *v = arg;
302 
303 	if (cell->event.source.client == v->client ||
304 	    cell->event.dest.client == v->client)
305 		return true;
306 	if (!v->timestamp)
307 		return false;
308 	switch (cell->event.flags & SNDRV_SEQ_TIME_STAMP_MASK) {
309 	case SNDRV_SEQ_TIME_STAMP_TICK:
310 		if (cell->event.time.tick)
311 			return true;
312 		break;
313 	case SNDRV_SEQ_TIME_STAMP_REAL:
314 		if (cell->event.time.time.tv_sec ||
315 		    cell->event.time.time.tv_nsec)
316 			return true;
317 		break;
318 	}
319 	return false;
320 }
321 
322 /* remove cells for left client */
323 void snd_seq_prioq_leave(struct snd_seq_prioq *f, int client, int timestamp)
324 {
325 	struct prioq_match_arg arg = { client, timestamp };
326 
327 	return prioq_remove_cells(f, prioq_match, &arg);
328 }
329 
330 struct prioq_remove_match_arg {
331 	int client;
332 	struct snd_seq_remove_events *info;
333 };
334 
335 static bool prioq_remove_match(struct snd_seq_event_cell *cell, void *arg)
336 {
337 	struct prioq_remove_match_arg *v = arg;
338 	struct snd_seq_event *ev = &cell->event;
339 	struct snd_seq_remove_events *info = v->info;
340 	int res;
341 
342 	if (ev->source.client != v->client)
343 		return false;
344 
345 	if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST) {
346 		if (ev->dest.client != info->dest.client ||
347 				ev->dest.port != info->dest.port)
348 			return false;
349 	}
350 	if (info->remove_mode & SNDRV_SEQ_REMOVE_DEST_CHANNEL) {
351 		if (! snd_seq_ev_is_channel_type(ev))
352 			return false;
353 		/* data.note.channel and data.control.channel are identical */
354 		if (ev->data.note.channel != info->channel)
355 			return false;
356 	}
357 	if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_AFTER) {
358 		if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
359 			res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
360 		else
361 			res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
362 		if (!res)
363 			return false;
364 	}
365 	if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_BEFORE) {
366 		if (info->remove_mode & SNDRV_SEQ_REMOVE_TIME_TICK)
367 			res = snd_seq_compare_tick_time(&ev->time.tick, &info->time.tick);
368 		else
369 			res = snd_seq_compare_real_time(&ev->time.time, &info->time.time);
370 		if (res)
371 			return false;
372 	}
373 	if (info->remove_mode & SNDRV_SEQ_REMOVE_EVENT_TYPE) {
374 		if (ev->type != info->type)
375 			return false;
376 	}
377 	if (info->remove_mode & SNDRV_SEQ_REMOVE_IGNORE_OFF) {
378 		/* Do not remove off events */
379 		switch (ev->type) {
380 		case SNDRV_SEQ_EVENT_NOTEOFF:
381 		/* case SNDRV_SEQ_EVENT_SAMPLE_STOP: */
382 			return false;
383 		default:
384 			break;
385 		}
386 	}
387 	if (info->remove_mode & SNDRV_SEQ_REMOVE_TAG_MATCH) {
388 		if (info->tag != ev->tag)
389 			return false;
390 	}
391 
392 	return true;
393 }
394 
395 /* remove cells matching remove criteria */
396 void snd_seq_prioq_remove_events(struct snd_seq_prioq * f, int client,
397 				 struct snd_seq_remove_events *info)
398 {
399 	struct prioq_remove_match_arg arg = { client, info };
400 
401 	return prioq_remove_cells(f, prioq_remove_match, &arg);
402 }
403